Repository navigation
fix: Report every YAML alias in W1101, including scalars - #4724
Conversation
W1101 found aliases by object identity, which only works for mappings and sequences, so a scalar alias such as `RetentionInDays: *Retention` passed cleanly even though CloudFormation rejects the template. The YAML loader now records each alias token with its location and template path (only when the source contains "&"), Template captures them before transforms run, and W1101 reports one warning per alias at the alias itself. Aliases are still resolved as before. Templates using the AWS::Serverless-2016-10-31 transform are skipped, since the SAM CLI expands aliases client-side. Fixes aws-cloudformation#4721 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kddejong
left a comment
There was a problem hiding this comment.
Approving. Verified locally against the branch.
- Repro from #4721 now emits
W1101at the alias; SAM templates are correctly skipped;AWS::LanguageExtensionsstill reports. - The #4697 ForEach false-positive test still passes; a literal
*in a quoted string doesn't false-positive. - 20 new tests pass,
ruff check/formatclean.
Token-scan is the right approach since the loader resolves aliases during compose, and capturing them in Template.__init__ before transforms swap out self.template is a nice catch.
One note for visibility (non-blocking): this changes W1101's message/column and now reports every alias use, so the snapshot update is expected.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4724 +/- ##
==========================================
- Coverage 94.78% 94.73% -0.05%
==========================================
Files 439 439
Lines 15805 15836 +31
Branches 3055 3059 +4
==========================================
+ Hits 14980 15003 +23
- Misses 450 455 +5
- Partials 375 378 +3
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Issue #, if available:
Fixes #4721
Description of changes:
W1101 already warns about YAML aliases, but it finds them by object identity (a mapping or sequence reachable from two places). That misses scalar aliases entirely, so this template lints clean while CloudFormation rejects it with
Template error: YAML aliases are not allowed in CloudFormation templates:Changes:
cfn_yaml.loadsstill resolves aliases exactly as before. When the source contains&(the same gate_guard_alias_expansionuses), it also scans the tokens for aliases and records each one's name, location and template path on the root node. Recording happens after the expansion guard, so recursive or amplified aliases are still rejected first.Templatecopies that list in__init__, because transforms (SAM, LanguageExtensions) replacecfn.template.ignore_checksstill work. It returns nothing for templates withTransform: AWS::Serverless-2016-10-31, since those are deployed with the SAM CLI, which expands aliases client-side.Behaviour changes for existing users: the W1101 message now names the alias, each use of an alias is reported (previously only the first shared container), and SAM templates no longer get W1101. The
yaml-aliassnapshot only changes in message and column range; its path is the same.Tests: new rule, decoder and end-to-end tests (scalar, sequence, merge key, SAM, LanguageExtensions), a good SAM fixture and a new integration snapshot for the repro.
tox -e py310,py314,stylepasses.By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
🤖 Generated with Claude Code