Skip to content

fix(build): Disallow --build-dir to resolve to cwd - #1403

Merged
sanathkr merged 4 commits into
aws:developfrom
jfuss:fix/1157
Sep 13, 2019
Merged

sanathkr merged 4 commits into
aws:developfrom
jfuss:fix/1157

Conversation

@jfuss

@jfuss jfuss commented Sep 12, 2019

Copy link
Copy Markdown
Contributor

Issue #, if available:
#1157

Description of changes:

Checklist:

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

Comment thread samcli/commands/build/command.py Outdated
default=DEFAULT_BUILD_DIR,
type=click.Path(file_okay=False, dir_okay=True, writable=True), # Must be a directory
help="Path to a folder where the built artifacts will be stored")
help="Path to a folder where the built artifacts will be stored. If a clean build, this directory will be first removed.")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we always do a clean build. So the 'if' shouldn't be necessary

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wording was future looking. I can remove. Do we even mention clean build is all we support anywhere today?

Comment thread samcli/commands/build/build_context.py Outdated
build_path = pathlib.Path(build_dir)

if os.path.abspath(str(build_path)) == os.path.abspath(str(pathlib.Path.cwd())):
exception_message = "'build-dir' should not be the current workding directoy as the directory " \

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we instead skip the "clean" if build-dir == cwd? It could give to a more intuitive experience

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I completely understand. My current understanding of the question is:

"Instead of always doing a "clean" build, we would not do a clean build if build-dir == cwd"?

If that is a fair rewording, then I don't think we should go down this route. Having a conditional do clean or not base upon a parameter that does not relate to a 'clean build' is not intuitive at all. This will make supporting clean builds more difficult in the future.

@sanathkr
sanathkr merged commit 37d0e39 into aws:develop Sep 13, 2019
@jfuss
jfuss deleted the fix/1157 branch September 13, 2019 21:12
bnusunny added a commit to bnusunny/aws-sam-cli that referenced this pull request Oct 13, 2026
sam build removes the build dir before building. aws#1403 refused a build dir
equal to the current working directory, but other cases slipped through:

- A build dir that contains the project: `sam build --build-dir ..`, or
  `sam build -t proj/template.yaml --build-dir proj` run from proj's
  parent, deletes the template and source (and, for `..`, everything beside
  the project). These are now refused: the build dir must not be or contain
  the working directory, the template's directory or the base dir. The
  comparison is case-insensitive on Windows.
- .aws-sam itself, or the cache or deps dir: `--build-dir .aws-sam` deletes
  SAM CLI's own state on every build -- the build cache and dependencies (so
  --cached never hits) and the committed pipeline config written by
  `sam pipeline bootstrap` -- and writes build.toml into the project root.
  Some users may pass this today, so it now logs a warning that it will not
  be supported in a future release, and still builds. No warning is logged
  for a non-clean build, which removes nothing.

.aws-sam/build (the default), .aws-sam/auto-dependency-layer (used by
sam sync) and directories outside the project are unaffected.
bnusunny added a commit to bnusunny/aws-sam-cli that referenced this pull request Oct 13, 2026
sam build removes the build dir before building. aws#1403 refused a build dir
equal to the current working directory, but other cases slipped through:

- A build dir that contains the project: `sam build --build-dir ..`, or
  `sam build -t proj/template.yaml --build-dir proj` run from proj's
  parent, deletes the template and source (and, for `..`, everything beside
  the project). These are now refused: the build dir must not be or contain
  the working directory, the template's directory or the base dir. The
  comparison is case-insensitive on Windows.
- .aws-sam itself, or the cache or deps dir: `--build-dir .aws-sam` deletes
  SAM CLI's own state on every build -- the build cache and dependencies (so
  --cached never hits) and the committed pipeline config written by
  `sam pipeline bootstrap` -- and writes build.toml into the project root.
  Some users may pass this today, so it now logs a warning that it will not
  be supported in a future release, and still builds. No warning is logged
  for a non-clean build, which removes nothing.

.aws-sam/build (the default), .aws-sam/auto-dependency-layer (used by
sam sync) and directories outside the project are unaffected.
bnusunny added a commit that referenced this pull request Oct 15, 2026
sam build removes the build dir before building. #1403 refused a build dir
equal to the current working directory, but other cases slipped through:

- A build dir that contains the project: `sam build --build-dir ..`, or
  `sam build -t proj/template.yaml --build-dir proj` run from proj's
  parent, deletes the template and source (and, for `..`, everything beside
  the project). These are now refused: the build dir must not be or contain
  the working directory, the template's directory or the base dir. The
  comparison is case-insensitive on Windows.
- .aws-sam itself, or the cache or deps dir: `--build-dir .aws-sam` deletes
  SAM CLI's own state on every build -- the build cache and dependencies (so
  --cached never hits) and the committed pipeline config written by
  `sam pipeline bootstrap` -- and writes build.toml into the project root.
  Some users may pass this today, so it now logs a warning that it will not
  be supported in a future release, and still builds. No warning is logged
  for a non-clean build, which removes nothing.

.aws-sam/build (the default), .aws-sam/auto-dependency-layer (used by
sam sync) and directories outside the project are unaffected.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants