Aws-sam-cli: sam build --build-dir . will nuke current working directory

Created on 29 Apr 2019  路  6Comments  路  Source: aws/aws-sam-cli

Description

Executing sam build --build-dir . will fail and result in removal of all files in current working directory.

Steps to reproduce

sam init -r python3.7
cd sam-app
sam build --build-dir .

Observed result

$ sam build --debug --build-dir .
2019-04-30 02:18:11 Using SAM Template at /private/tmp/sam-app/template.yaml
2019-04-30 02:18:11 Changing event name from creating-client-class.iot-data to creating-client-class.iot-data-plane
2019-04-30 02:18:11 Changing event name from before-call.apigateway to before-call.api-gateway
2019-04-30 02:18:11 Changing event name from request-created.machinelearning.Predict to request-created.machine-learning.Predict
2019-04-30 02:18:11 Changing event name from before-parameter-build.autoscaling.CreateLaunchConfiguration to before-parameter-build.auto-scaling.CreateLaunchConfiguration
2019-04-30 02:18:11 Changing event name from before-parameter-build.route53 to before-parameter-build.route-53
2019-04-30 02:18:11 Changing event name from request-created.cloudsearchdomain.Search to request-created.cloudsearch-domain.Search
2019-04-30 02:18:11 Changing event name from docs.*.autoscaling.CreateLaunchConfiguration.complete-section to docs.*.auto-scaling.CreateLaunchConfiguration.complete-section
2019-04-30 02:18:11 Changing event name from before-parameter-build.logs.CreateExportTask to before-parameter-build.cloudwatch-logs.CreateExportTask
2019-04-30 02:18:11 Changing event name from docs.*.logs.CreateExportTask.complete-section to docs.*.cloudwatch-logs.CreateExportTask.complete-section
2019-04-30 02:18:11 Changing event name from before-parameter-build.cloudsearchdomain.Search to before-parameter-build.cloudsearch-domain.Search
2019-04-30 02:18:11 Changing event name from docs.*.cloudsearchdomain.Search.complete-section to docs.*.cloudsearch-domain.Search.complete-section
2019-04-30 02:18:11 Changing event name from creating-client-class.iot-data to creating-client-class.iot-data-plane
2019-04-30 02:18:11 Changing event name from before-call.apigateway to before-call.api-gateway
2019-04-30 02:18:11 Changing event name from request-created.machinelearning.Predict to request-created.machine-learning.Predict
2019-04-30 02:18:11 Changing event name from before-parameter-build.autoscaling.CreateLaunchConfiguration to before-parameter-build.auto-scaling.CreateLaunchConfiguration
2019-04-30 02:18:11 Changing event name from before-parameter-build.route53 to before-parameter-build.route-53
2019-04-30 02:18:11 Changing event name from request-created.cloudsearchdomain.Search to request-created.cloudsearch-domain.Search
2019-04-30 02:18:11 Changing event name from docs.*.autoscaling.CreateLaunchConfiguration.complete-section to docs.*.auto-scaling.CreateLaunchConfiguration.complete-section
2019-04-30 02:18:11 Changing event name from before-parameter-build.logs.CreateExportTask to before-parameter-build.cloudwatch-logs.CreateExportTask
2019-04-30 02:18:11 Changing event name from docs.*.logs.CreateExportTask.complete-section to docs.*.cloudwatch-logs.CreateExportTask.complete-section
2019-04-30 02:18:11 Changing event name from before-parameter-build.cloudsearchdomain.Search to before-parameter-build.cloudsearch-domain.Search
2019-04-30 02:18:11 Changing event name from docs.*.cloudsearchdomain.Search.complete-section to docs.*.cloudsearch-domain.Search.complete-section
2019-04-30 02:18:11 'build' command is called
2019-04-30 02:18:11 No Parameters detected in the template
2019-04-30 02:18:11 2 resources found in the template
2019-04-30 02:18:11 Found Serverless function with name='HelloWorldFunction' and CodeUri='hello_world/'
Traceback (most recent call last):
  File "/usr/local/bin/sam", line 11, in <module>
    load_entry_point('aws-sam-cli==0.15.0', 'console_scripts', 'sam')()
  File "/usr/local/Cellar/aws-sam-cli/0.15.0/libexec/lib/python3.7/site-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/Cellar/aws-sam-cli/0.15.0/libexec/lib/python3.7/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/usr/local/Cellar/aws-sam-cli/0.15.0/libexec/lib/python3.7/site-packages/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/Cellar/aws-sam-cli/0.15.0/libexec/lib/python3.7/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/Cellar/aws-sam-cli/0.15.0/libexec/lib/python3.7/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/Cellar/aws-sam-cli/0.15.0/libexec/lib/python3.7/site-packages/click/decorators.py", line 64, in new_func
    return ctx.invoke(f, obj, *args[1:], **kwargs)
  File "/usr/local/Cellar/aws-sam-cli/0.15.0/libexec/lib/python3.7/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/Cellar/aws-sam-cli/0.15.0/libexec/lib/python3.7/site-packages/samcli/commands/build/command.py", line 100, in cli
    skip_pull_image, parameter_overrides, mode)  # pragma: no cover
  File "/usr/local/Cellar/aws-sam-cli/0.15.0/libexec/lib/python3.7/site-packages/samcli/commands/build/command.py", line 131, in do_cli
    mode=mode) as ctx:
  File "/usr/local/Cellar/aws-sam-cli/0.15.0/libexec/lib/python3.7/site-packages/samcli/commands/build/build_context.py", line 65, in __enter__
    self._build_dir = self._setup_build_dir(self._build_dir, self._clean)
  File "/usr/local/Cellar/aws-sam-cli/0.15.0/libexec/lib/python3.7/site-packages/samcli/commands/build/build_context.py", line 82, in _setup_build_dir
    shutil.rmtree(build_dir)
  File "/usr/local/Cellar/aws-sam-cli/0.15.0/libexec/lib/python3.7/shutil.py", line 495, in rmtree
    onerror(os.rmdir, path, sys.exc_info())
  File "/usr/local/Cellar/aws-sam-cli/0.15.0/libexec/lib/python3.7/shutil.py", line 493, in rmtree
    os.rmdir(path)
OSError: [Errno 22] Invalid argument: '.'

Expected result

It should either:
a) fail cleanly
b) create a .aws-sam directory inside the build-dir which it can remove without worry in case of failure.

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: macOS 10.14.4 (python via brew)
  2. SAM CLI, version 0.15.0
arebuild typux

Most helpful comment

I just got bit by this bug. My intention when using -b . was that the build folder would be created in the location where I was running sam build i.e. in that location a .aws-sam/build/ folder structure would be created. This really does need some safety mechanisms before making such draconian assumptions.

Thank goodness for backups :-/

All 6 comments

@m1keil Why are you building into the current directory?

It nuking the base-directory is expected to give a clean build. You SHOULD NOT be using . as a build_directory for this reason.

@jfuss I haven't done that on purpose. I guess that's a user error. I assumed that build-dir acts similiarly to base-dir, in that it will create .aws-sam in that dir.

Regardless, I think that it's a pretty drastic error handling from the tool to just rmtree the cwd.

@m1keil We defiantly need more safe guards here for sure. I am not arguing against that. :)

What I was trying to understand is why you where using it and what you were trying to achieve? build-dir's help text says "Path to a folder where the built artifacts will be stored". The default here is technically .aws-sam, which is why you saw the behavior you did.

You really don't need to override any of these values unless you really wanted to control where the artifacts are placed. In most cases, you don't need to control this and will create more work for you as you need to pass the generated template and location into other commands (like sam local invoke, sam package, etc).

I don't think anything in the docs or any other support material tricked me. I guess it was sort of just laziness and the late hour that contributed here. Basically I have a wrapper script around sam cli, and a small function that return the absolute path of the CWD. So I just ran my script combining the two and 馃挜.
I didn't expect anything drastic to happen, worst case, my CWD getting filled with some build junk, right? 馃槉

As for the reasons of the wrapper - I need to deploy my lambas in multiple accounts/regions. So I need to combine sam with additional tools we use in order to make it happen. Essentially I need to have the control of where the build results are being stored (absolute path and not relative).

I just got bit by this bug. My intention when using -b . was that the build folder would be created in the location where I was running sam build i.e. in that location a .aws-sam/build/ folder structure would be created. This really does need some safety mechanisms before making such draconian assumptions.

Thank goodness for backups :-/

Disallowing cwd to the -b option was released in v0.22.0.

Closing

Was this page helpful?
0 / 5 - 0 ratings

Related issues

asyba picture asyba  路  3Comments

cmccoypdx picture cmccoypdx  路  3Comments

Caian picture Caian  路  3Comments

XDanny322 picture XDanny322  路  3Comments

zhangzhx picture zhangzhx  路  3Comments