Skip to content

Bug: sam build --build-dir deletes the project when given a parent directory, and SAM CLI's own state when given .aws-sam #9327

Closed

Description

@bnusunny

Description:

sam build removes --build-dir before building. #1403 refuses a build dir equal to the current working directory, but nothing stops two other cases:

  1. 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().
  2. --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-31
Resources:
  Fn:
    Type: AWS::Serverless::Function
    Properties: {Runtime: python3.12, Handler: app.handler, CodeUri: src/}
EOF
echo '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)

  1. OS: Amazon Linux 2023
  2. sam --version: SAM CLI, version 1.166.2
  3. 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.

Activity

  1. added
    stage/needs-triage
    area/build
    and removed
    stage/needs-triage
    on Oct 12, 2026
  2. github-actions commented on Oct 15, 2026

    @github-actions
    Contributor

    ⚠️COMMENT VISIBILITY WARNING⚠️

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/buildtype/bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions