Skip to content

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

Merged
mrgrain merged 3 commits into
aws:mainfrom
TORIFUKUKaiou:fix/init-project-name
Jun 22, 2026
Merged

fix(aws-cdk): correct project-name argument mapping in cdk init#1644
mrgrain merged 3 commits into
aws:mainfrom
TORIFUKUKaiou:fix/init-project-name

Conversation

@TORIFUKUKaiou

Copy link
Copy Markdown
Contributor

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

@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
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
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`.
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.
@TORIFUKUKaiou

Copy link
Copy Markdown
Contributor Author

Done! I've rebased my branch with the latest main and pushed the updates. The branch is now up to date with the current base. Thanks!

@codecov-commenter

codecov-commenter commented Jun 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 88.80%. Comparing base (55c4d9e) to head (eefd91a).

Files with missing lines Patch % Lines
packages/aws-cdk/lib/cli/cli.ts 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1644      +/-   ##
==========================================
+ Coverage   88.78%   88.80%   +0.01%     
==========================================
  Files          77       77              
  Lines       11354    11354              
  Branches     1584     1584              
==========================================
+ Hits        10081    10083       +2     
+ Misses       1243     1241       -2     
  Partials       30       30              
Flag Coverage Δ
suite.unit 88.80% <0.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mrgrain mrgrain added this pull request to the merge queue Jun 22, 2026
Merged via the queue into aws:main with commit 3ff0bdf Jun 22, 2026
41 checks passed
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

4 participants