Skip to content

Conversation

@rohang9000
Copy link
Contributor

Updated cdk init command to auto-detect language for single-language custom templates that are pulled from filesystem using --from-path so users do not need to specify --language in this case.

Fixes #


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

@aws-cdk-automation aws-cdk-automation requested a review from a team August 28, 2025 16:56
@github-actions github-actions bot added the p2 label Aug 28, 2025
@codecov-commenter
Copy link

codecov-commenter commented Aug 28, 2025

Codecov Report

❌ Patch coverage is 95.45455% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.23%. Comparing base (a6fa914) to head (7801fb2).

Files with missing lines Patch % Lines
packages/aws-cdk/lib/commands/init/init.ts 95.29% 3 Missing and 1 partial ⚠️
packages/aws-cdk/lib/commands/language.ts 96.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #819      +/-   ##
==========================================
+ Coverage   82.49%   83.23%   +0.74%     
==========================================
  Files          71       71              
  Lines       10333    10396      +63     
  Branches     1273     1306      +33     
==========================================
+ Hits         8524     8653     +129     
+ Misses       1767     1704      -63     
+ Partials       42       39       -3     
Flag Coverage Δ
suite.unit 83.23% <95.45%> (+0.74%) ⬆️

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

☔ View full report in Codecov by Sentry.
📢 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.

Copy link
Contributor

@iankhou iankhou left a comment

Choose a reason for hiding this comment

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

Please add unit tests for these changes. Consider separating the basePath and actualBasePath logic into a separate function, or adding some comments to explain it and its relationship with language—it's not straightforward at a glance.

const entries = await fs.readdir(basePath, { withFileTypes: true });
const languageDirs = entries.filter(entry =>
entry.isDirectory() &&
['typescript', 'javascript', 'python', 'java', 'csharp', 'fsharp', 'go'].includes(entry.name),
Copy link
Contributor

Choose a reason for hiding this comment

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

No need to name these, iterate the keys from languageExtensions earlier in the file.

Copy link
Contributor

Choose a reason for hiding this comment

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

Addressed this by implementing my suggestion. Moved language extensions to a new interface LanguageInfo.

Comment on lines 308 to 314
typescript: ['.ts', '.js'],
javascript: ['.js'],
python: ['.py'],
java: ['.java'],
csharp: ['.cs'],
fsharp: ['.fs'],
go: ['.go'],
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Moved language extensions to new interface LanguageInfo, which is the new, consolidated source of truth.

Comment on lines 371 to 375
const langDirPath = path.join(basePath, langDir);
const hasValidFiles = await hasLanguageFiles(langDirPath, getLanguageExtensions(langDir));

if (hasValidFiles) {
actualBasePath = path.join(basePath, langDir);
Copy link
Contributor

Choose a reason for hiding this comment

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

Two variables here have the same value—as long as hasLanguageFiles is a pure function (which it should be, if it's not, it should be corrected).

langDirPath and actualBasePath.

Copy link
Contributor

Choose a reason for hiding this comment

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

Addressed this by consolidating the two variables.

Comment on lines 358 to 362
let languages = await getLanguageDirectories(basePath);

// Auto-detect single language templates
if (languages.length === 0) {
const entries = await fs.readdir(basePath, { withFileTypes: true });
Copy link
Contributor

Choose a reason for hiding this comment

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

We are performing fs.readDir here twice with the same parameters. getLanguageDirectories calls it in the same way. Try to reuse the result instead of calling it again so soon. See

async function getLanguageDirectories(templatePath: string): Promise<string[]> {
.

Copy link
Contributor

Choose a reason for hiding this comment

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

Changed the return signature of getLanguageDirectories to include entries, so that we can reuse it here and speed up the code.

Comment on lines 374 to 376
if (hasValidFiles) {
actualBasePath = path.join(basePath, langDir);
languages = [langDir];
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like a silent failure if hasValidFiles is falsy. Shouldn't we fail or at least log a warning in this case?

Copy link
Contributor

Choose a reason for hiding this comment

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

Removed dead code.

@iankhou iankhou self-assigned this Aug 29, 2025
aws-cdk-automation and others added 2 commits October 6, 2025 20:48
Upgrades project dependencies. See details in [workflow run].

[Workflow Run]:
https://github.com/aws/aws-cdk-cli/actions/runs/17625260603

------

*Automatically created by projen via the "upgrade" workflow*

---------

Signed-off-by: github-actions <[email protected]>
Co-authored-by: github-actions <[email protected]>
Upgrades project dependencies. See details in [workflow run].

[Workflow Run]:
https://github.com/aws/aws-cdk-cli/actions/runs/17643754247

------

*Automatically created by projen via the "upgrade" workflow*

---------

Signed-off-by: github-actions <[email protected]>
Co-authored-by: github-actions <[email protected]>
@iankhou
Copy link
Contributor

iankhou commented Oct 7, 2025

Addressed my own comments and added a lot of unit testing to cover both new and old code.

Removed repeated code and consolidated the source of truth for supported CDK languages in a new LanguageInfo interface.

@iankhou iankhou self-requested a review October 7, 2025 00:54
@iankhou
Copy link
Contributor

iankhou commented Oct 7, 2025

init.ts.html

@aws-cdk-automation aws-cdk-automation added this pull request to the merge queue Oct 7, 2025
Merged via the queue into main with commit e46adaf Oct 7, 2025
28 checks passed
@aws-cdk-automation aws-cdk-automation deleted the fix/auto-detect-single-language-template branch October 7, 2025 21:11
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.