You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bug: sam build --build-dir deletes the project when given a parent directory, and SAM CLI's own state when given .aws-sam #9327
sam build removes --build-dir before building. #1403 refuses a build dir equal to the current working directory, but nothing stops two other cases:
A parent of the working directory deletes the project.sam build --build-dir .. removes the parent directory: the template, the source and anything next to the project. The build then crashes with FileNotFoundError from getcwd().
--build-dir .aws-sam deletes SAM CLI's own state on every build..aws-sam also holds cache/ and deps/, so --cached never gets a hit, and pipeline/pipelineconfig.toml from sam pipeline bootstrap is deleted too. build.toml and sync.toml are written to <build-dir>/.., which is the project root in this case.
Steps to reproduce:
mkdir -p outer/proj/src &&cd outer/proj && touch ../sibling.txt
cat > template.yaml <<'EOF'Transform: AWS::Serverless-2016-10-31Resources: Fn: Type: AWS::Serverless::Function Properties: {Runtime: python3.12, Handler: app.handler, CodeUri: src/}EOFecho'def handler(e, c): return 1'> src/app.py
sam build --build-dir ..
ls .. # outer/ is empty: proj/ and sibling.txt are gone
For case 2: sam pipeline bootstrap (or create .aws-sam/pipeline/pipelineconfig.toml), then sam build --build-dir .aws-sam --cached. The pipeline config is gone, and every later --cached build rebuilds from scratch.
Observed result:
Case 1 deletes the whole parent directory, then:
FileNotFoundError: [Errno 2] No such file or directory
File ".../samcli/lib/telemetry/project_metadata.py", line 71, in get_project_name
project_name = basename(getcwd().replace("\\", "/"))
Expected result:
Case 1 is refused with the same error as --build-dir ., and nothing is deleted.
Case 2 at least warns that SAM CLI's own state will be removed.
Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
OS: Amazon Linux 2023
sam --version: SAM CLI, version 1.166.2
AWS region: n/a
The cause is BuildContext._setup_build_dir in samcli/commands/build/build_context.py, which compares the build dir to the working directory for equality only.
Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.
Activity
github-actions commentedon Oct 15, 2026
Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.