Skip to content

feat(cli): accept cdk.yaml and ~/.cdk.yaml so config can carry comments - #2193

Open
kidbrax wants to merge 2 commits into
aws:mainfrom
kidbrax:feat/yaml-config-files
Open

kidbrax wants to merge 2 commits into
aws:mainfrom
kidbrax:feat/yaml-config-files

Conversation

@kidbrax

@kidbrax kidbrax commented Dec 1, 2026

Copy link
Copy Markdown

Closes aws/aws-cdk#15529.

The hand-authored config files can now be written as YAML, so that they can carry comments explaining why a setting or a feature flag is the way it is. JSON remains the default, and nothing about it changes.

Scope

cdk.yaml and ~/.cdk.yaml are accepted as alternatives to cdk.json and ~/.cdk.json. cdk.context.json stays JSON only.

That split is deliberate, and it is narrower than the literal title of the issue (which asked for cdk.context.yaml). cdk.context.json is a cache of context provider lookups that the CLI writes on every run, not a file meant to be hand-edited, so comments in it would have nowhere useful to live. The workaround posted in the issue thread — loading a YAML file and passing it in as context — is about the authored config too. Happy to widen this if maintainers disagree.

This is not #632. That asks which context file is read, per environment; this is only about how the bytes of the two authored files are parsed. The code is adjacent but the two are independent.

Backwards compatibility

Where both spellings exist, the JSON one is read, exactly as it is today, and a warning names the file being ignored:

ignoring /path/cdk.yaml because /path/cdk.json also exists; remove one of the two

Erroring out would have been tidier, but it would break any project that already has an unrelated cdk.yaml sitting next to its cdk.json, so JSON keeps winning. No feature flag seemed warranted, because no existing configuration changes meaning — a project with only cdk.json behaves identically. Please say if you would rather it was gated.

Two places that needed more than a parser swap

  • cdk flags --set writes the project config. It is the one code path that writes a hand-authored file. For YAML it now goes through the yaml document API, so a flag change leaves the file's comments intact.
  • CdkAppMultiContext reads the config independently of the CLI's own Configuration. It resolves the same two spellings, so context held in a YAML config is visible to the programmatic API as well.

Notes

  • No new dependency: yaml@^2 is already a production dependency of both packages/aws-cdk and packages/@aws-cdk/toolkit-lib.
  • No change to toolkit-lib's public API. The shared helper lives in lib/util/config-file.ts and reaches the CLI through api-private.ts, so there is no API report change.
  • cdk init still generates cdk.json; YAML is opt-in by renaming and converting.

Testing

  • 20 unit tests for the new helper covering format detection, resolution precedence, parse failures, an empty document, a non-map document, and comment preservation across a write.
  • 6 tests at the Configuration level for settings, context, contextSourceFiles, the both-files warning, and the unchanged JSON path.
  • Full aws-cdk and @aws-cdk/toolkit-lib suites run with no new failures; the failures that remain are present unchanged on main in the same environment (bootstrap template fixtures, sdk-provider, and the init/migrate tests that need the .NET SDK).
  • Verified against the built CLI: cdk context in a directory holding only a commented cdk.yaml lists its context and names the file, and emits the warning above once a cdk.json is added alongside.

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

Braxton Beyer added 2 commits December 1, 2026 21:19
The hand-authored config files can now be written as YAML, which lets them
hold comments explaining why a setting or a feature flag is the way it is.
JSON remains the default and nothing about it changes.

- `cdk.yaml` and `~/.cdk.yaml` are read as alternatives to `cdk.json` and
  `~/.cdk.json`. `cdk.context.json` stays JSON only: it is a cache of context
  provider lookups that the CLI writes, not a file meant to be hand-edited.
- Where both spellings exist the JSON one is read, exactly as today, and a
  warning names the file being ignored. A project that already has an
  unrelated `cdk.yaml` therefore keeps working.
- `cdk flags --set` writes a YAML config back through the YAML document API,
  so the comments in the file survive a flag change.
- `CdkAppMultiContext` resolves the same two spellings, so context held in a
  YAML config is visible to the programmatic API as well.

closes aws/aws-cdk#15529
@kidbrax
kidbrax requested a review from a team December 1, 2026 23:13
@github-actions github-actions Bot added the p2 label Dec 1, 2026

This branch is waiting to be deployed

1 inactive and 1 waiting deployments
integ-approval — 2facefc7 Waiting Dec 1, 2026 by kidbrax via prepare #7826
automation — 2facefc7 Deployed Dec 1, 2026 by kidbrax via Set AutoQueue on PR #2193 #4068
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.

Allow us to define context values using YAML instead of JSON.

1 participant