aws / aws-sam-cli

CLI tool to build, test, debug, and deploy Serverless applications using AWS SAM
https://aws.amazon.com/serverless/sam/
Apache License 2.0
6.5k stars 1.17k forks source link

Bug: SAM init project with multiple templates - breaking change #6400

Closed dave-moser closed 10 months ago

dave-moser commented 10 months ago

Description:

Performing a sam init against a custom template location that has multiple project templates results in a cookiecutter error. This only started happening with a recent update to the sam cli. It did not occur in v 1.95.0. I only started seeing it in 1.103.0.

You can see the error by attempting to init either of these 2 projects in aws-samples

sam init --location gh:aws-samples/cookiecutter-aws-sam-rust

sam init --location gh:aws-samples/aws-sam-swift

Steps to reproduce:

Update your SAM CLI to the latest version (1.103)

Initialize a new project with either of these templates

sam init --location gh:aws-samples/cookiecutter-aws-sam-rust

sam init --location gh:aws-samples/aws-sam-swift

Observed result:

You get a cookiecutter error

Error: 'NoneType' object has no attribute 'group'
Traceback:
  File "/usr/local/opt/python-click/lib/python3.12/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/usr/local/opt/python-click/lib/python3.12/site-packages/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/opt/python-click/lib/python3.12/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/opt/python-click/lib/python3.12/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/aws-sam-cli/1.103.0/libexec/lib/python3.12/site-packages/samcli/cli/cli_config_file.py", line 347, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/aws-sam-cli/1.103.0/libexec/lib/python3.12/site-packages/samcli/commands/init/command.py", line 102, in wrapped
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/opt/python-click/lib/python3.12/site-packages/click/decorators.py", line 92, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/opt/python-click/lib/python3.12/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/aws-sam-cli/1.103.0/libexec/lib/python3.12/site-packages/samcli/lib/telemetry/metric.py", line 184, in wrapped
    raise exception  # pylint: disable=raising-bad-type
    ^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/aws-sam-cli/1.103.0/libexec/lib/python3.12/site-packages/samcli/lib/telemetry/metric.py", line 149, in wrapped
    return_value = func(*args, **kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/aws-sam-cli/1.103.0/libexec/lib/python3.12/site-packages/samcli/lib/utils/version_checker.py", line 42, in wrapped
    actual_result = func(*args, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/aws-sam-cli/1.103.0/libexec/lib/python3.12/site-packages/samcli/cli/main.py", line 95, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/aws-sam-cli/1.103.0/libexec/lib/python3.12/site-packages/samcli/commands/init/command.py", line 265, in cli
    do_cli(
  File "/usr/local/Cellar/aws-sam-cli/1.103.0/libexec/lib/python3.12/site-packages/samcli/commands/init/command.py", line 346, in do_cli
    do_generate(
  File "/usr/local/Cellar/aws-sam-cli/1.103.0/libexec/lib/python3.12/site-packages/samcli/commands/init/init_generator.py", line 24, in do_generate
    generate_project(
  File "/usr/local/Cellar/aws-sam-cli/1.103.0/libexec/lib/python3.12/site-packages/samcli/lib/init/__init__.py", line 111, in generate_project
    cookiecutter(**params)
  File "/usr/local/opt/cookiecutter/libexec/lib/python3.12/site-packages/cookiecutter/main.py", line 142, in cookiecutter
    nested_template = choose_nested_template(context, repo_dir, no_input)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/opt/cookiecutter/libexec/lib/python3.12/site-packages/cookiecutter/prompt.py", line 361, in choose_nested_template
    template = re.search(r'\((.+)\)', val).group(1)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expected result:

In prior versions of the CLI (at least 1.95.0) the project generated the folder structure without error. I'm not sure which CLI version this error started occuring. It is happening in 1.103.0

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: Mac OS Ventura 13.6
  2. sam --version: SAM CLI, version 1.103.0
  3. AWS region: us-east-1
{
  "version": "1.103.0",
  "system": {
    "python": "3.12.0",
    "os": "macOS-13.6-x86_64-i386-64bit"
  },
  "additional_dependencies": {
    "docker_engine": "24.0.6",
    "aws_cdk": "Not available",
    "terraform": "Not available"
  },
  "available_beta_feature_env_vars": [
    "SAM_CLI_BETA_FEATURES",
    "SAM_CLI_BETA_BUILD_PERFORMANCE",
    "SAM_CLI_BETA_TERRAFORM_SUPPORT",
    "SAM_CLI_BETA_RUST_CARGO_LAMBDA"
  ]
}

Add --debug flag to command you are running

dave-moser commented 10 months ago

Might it have something to do with SAM CLI upgrading to v2.5 of cookiecutter? I see a reference to Implement new style for nested templates config in the release notes for cookiecutter 2.5.0. Both of these SAM aws-samples use multiple templates for the user to choose from.

dave-moser commented 10 months ago

I determined this is due to a new way cookiecutter configures multiple templates in one repo as of v 2.2. Additnal changes were implemented in v2.5. SAM CLI 1.103.0 updates cookie cutter to v2.5. Once I reorganized my repo to the new template folder structure, it works. I suspect this will affect anyone who published a SAM Template Project prior to Juky 2023 when the new formats were introduced.

github-actions[bot] commented 10 months ago

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.