Skip to content

fix(aws-cdk): correct project-name argument mapping in cdk init#1644

Open
TORIFUKUKaiou wants to merge 2 commits into
aws:mainfrom
TORIFUKUKaiou:fix/init-project-name
Open

fix(aws-cdk): correct project-name argument mapping in cdk init#1644
TORIFUKUKaiou wants to merge 2 commits into
aws:mainfrom
TORIFUKUKaiou:fix/init-project-name

Conversation

@TORIFUKUKaiou

Copy link
Copy Markdown

The --project-name (-n) option for cdk init was silently ignored because the legacy code path in cli.ts referenced args.name (a non-existent property) instead of args.projectName.

Yargs automatically converts the kebab-case option --project-name to args.projectName via camelCase conversion, but the mapping incorrectly used args.name, causing the value to always be undefined and falling back to the directory name.

Also fixes the test description that incorrectly referred to the option as --name instead of --project-name.

Fixes aws/aws-cdk#31992

Validation

I verified the fix locally in the devcontainer environment by running the modified CLI and comparing the outputs.

  1. Verify yargs argument parsing:
    Added a new unit test in packages/aws-cdk/test/cli/cli-arguments.test.ts to verify that the --project-name option correctly maps to the projectName property in the config.

  2. Verify generated project structure and files:
    I generated two projects with the same target project name (awesome-app)—one by relying on the default fallback (directory name) and the other by explicitly specifying --project-name:

    # 1. Project without --project-name (inheriting directory name)
    mkdir /tmp/awesome-app && cd /tmp/awesome-app
    node /workspace/aws-cdk-cli/packages/aws-cdk/bin/cdk init app --language typescript --generate-only
    
    # 2. Project with --project-name in a different directory
    mkdir /tmp/cdk-verify-test && cd /tmp/cdk-verify-test
    node /workspace/aws-cdk-cli/packages/aws-cdk/bin/cdk init app --language typescript --project-name awesome-app --generate-only
    
    # 3. Compare the two generated projects
    diff -r /tmp/awesome-app /tmp/cdk-verify-test

    Result:
    The diff command yielded no differences, confirming that --project-name awesome-app was correctly respected and generated identical files (including package.json, stack file names, and class names inside lib/ and bin/) regardless of the directory name.

Checklist

  • This change contains a major version upgrade for a dependency and I confirm all breaking changes are addressed
    • Release notes for the new version:

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

The --project-name (-n) option for `cdk init` was silently ignored
because the legacy code path in cli.ts referenced `args.name`
(a non-existent property) instead of `args.projectName`.

Yargs automatically converts the kebab-case option `--project-name`
to `args.projectName` via camelCase conversion, but the mapping
incorrectly used `args.name`, causing the value to always be
undefined and falling back to the directory name.

Also fixes the test description that incorrectly referred to
the option as `--name` instead of `--project-name`.

@sai-ray sai-ray left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @TORIFUKUKaiou, nice catch and thanks for fixing this! It's been silently doing nothing since it was added, the handler was reading the wrong property, so it always fell back to the folder name.

One thing: we'll need a test that actually runs the command with option and confirms the name comes through, so this can't quietly break again.

Comment thread packages/aws-cdk/test/cli/cli-arguments.test.ts
Adds a test that runs `cdk init` with `--project-name`
and asserts that the generated package name and stack class name are
correctly updated.

This ensures the parsed project name actually reaches the init command handler.
auto-merge was automatically disabled June 18, 2026 23:24

Head branch was pushed to by a user without write access

@sai-ray

sai-ray commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Could you also update your branch with the latest main before we run CI? It's a bit behind, and I'd like CI to run against the current base. Thanks!

@sai-ray sai-ray self-assigned this Jun 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cdk: add option to specify projectName to cdk init

2 participants