fix: restrict SAM/CFN project detection to SAM-supported extensions - #13739
Conversation
Any file named template.* or samconfig.* in the working directory made
the CLI treat the project as a SAM/CloudFormation stack, regardless of
extension. A template.mjs application module hijacked runner selection
away from serverless.yml, was loaded (and executed, for js/mjs/cjs/ts)
during initialization, and left only the CFN commands available; files
with unparseable extensions such as template.html crashed every command
in the directory, including help.
Project detection and template lookup now only consider the file names
the AWS SAM CLI itself supports: template.{yaml,yml,json} and
samconfig.{toml,yaml,yml}. Files with other extensions are ignored, so
discovery falls through to the framework config or onboarding. A
samconfig file without a SAM-supported template next to it now fails
with a clear TEMPLATE_FILE_NOT_FOUND error instead of crashing on a
null path. Also removes the unused getConfigFileDetails helper, which
duplicated this discovery logic and had no callers.
Fixes #13738
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
📝 WalkthroughWalkthroughRunner discovery now applies extension allowlists, SAM template resolution restricts supported formats, ChangesRunner discovery filtering
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant findRunner
participant CfnRunner
participant getConfigFilePath
findRunner->>CfnRunner: Read allowed config extensions
findRunner->>getConfigFilePath: Search basename with extension filter
getConfigFilePath-->>findRunner: Return matching path or null
findRunner-->>CfnRunner: Select runner
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/sf-core/src/lib/router.js`:
- Around line 253-263: Normalize configured extension allowlists to lowercase
before comparing them with the discovered extension in the router’s
file-matching logic. Also normalize supplied extensions to lowercase before
constructing candidate paths in getConfigFilePath; apply these changes in
packages/sf-core/src/lib/router.js lines 253-263 and
packages/sf-core/src/utils/fs/index.js line 292, and add mixed-case coverage.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: af0ea947-2562-4f45-b577-26fccb83ef3a
📒 Files selected for processing (7)
packages/sf-core/src/lib/router.jspackages/sf-core/src/lib/runners/cfn/cfn.jspackages/sf-core/src/utils/fs/config-file.jspackages/sf-core/src/utils/fs/index.jspackages/sf-core/src/utils/index.jspackages/sf-core/tests/unit/lib/runner-discovery.test.jspackages/sf-core/tests/unit/utils/fs/get-config-file-path.test.js
💤 Files with no reviewable changes (2)
- packages/sf-core/src/utils/index.js
- packages/sf-core/src/utils/fs/config-file.js
Summary
template.{yaml,yml,json}andsamconfig.{toml,yaml,yml}(packages/sf-core/src/lib/runners/cfn/cfn.js,packages/sf-core/src/lib/router.js)samconfigprojects, and fail with a clearTEMPLATE_FILE_NOT_FOUNDerror when asamconfigfile has no SAM-supported template next to it (previously crashed reading a null path)getConfigFileDetailshelper (packages/sf-core/src/utils/fs/config-file.js), which duplicated this discovery logic and has no callersRoot cause
Runner discovery matched directory files against known config names by base name only, ignoring the extension. Any file named
template.*orsamconfig.*— for example atemplate.mjsapplication module — selected the SAM/CloudFormation runner, which only supportsdeploy,remove,info, andprint. That hid the framework commands fromserverless help, madeserverless packagefail with "Command not found", shadowed an existingserverless.ymlin the same directory, and loaded (forjs/mjs/cjs/ts, executed) the file during initialization. Files with unparseable extensions such astemplate.htmlcrashed every command in the directory, includinghelp.Behavior changes
template.*/samconfig.*files with extensions SAM does not support no longer affect project detection and are no longer loaded at startup; discovery falls through to the framework config or onboardingtemplate.*files with unparseable extensions no longer crash the CLIserverless.*andserverless-compose.*configs is unchanged for every extension (pinned by tests)template.{yaml,yml,json}andsamconfig.{toml,yaml,yml}projects behave exactly as beforeTest plan
serverless.yml/serverless.mjs/serverless-compose.ymlselection unchanged;serverless.ymlno longer shadowed bytemplate.mjs) plusgetConfigFilePathextension-filter tests and aTEMPLATE_FILE_NOT_FOUNDcaseserverless.yml+template.mjs,template.mjsalone,template.yamlalone,template.html+serverless.yml,samconfig.toml+template.yaml,samconfig.toml+template.mjsFixes #13738
Summary by CodeRabbit
Bug Fixes
Tests