aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

sam build deletes (package.json local path) source files

Open cheruvian opened this issue 4 years ago • 1 comments

Description

When using npm local paths (ie a package.json dependency that is not in npm but rather on the local file system), sam build builds correctly the first time, on subsequent builds however, it deletes the source files in addition to the build files.

tl;dr; Mitigation is to rm -rf .aws-sam every time prior to running sam build

Steps to reproduce

sam init #hello-world
cd sam-app

# Shared libs are common with APIG / Lambda since many tutorials indicate that each API should be its own lambda function
mkdir -p shared-libs 
echo "{}" > shared-libs/package.json # Fake package for now
# Add a dependency on the shared-libs
cat > hello-world/package.json << PACKAGE_JSON 
{
  "name": "hello_world",
  "version": "1.0.0",
  "dependencies": {
    "shared-libs": "file:../../../shared-libs"
  }
}
PACKAGE_JSON

sam build # this one will succeed
sam build # this one will fail because the shared-libs directory has been deleted

Observed result

$ sam build --debug
Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
'build' command is called
No Parameters detected in the template
2 resources found in the template
Found Serverless function with name='HelloWorldFunction' and CodeUri='hello-world/'
Building resource 'HelloWorldFunction'
Loading workflow module 'aws_lambda_builders.workflows'
Registering workflow 'PythonPipBuilder' with capability 'Capability(language='python', dependency_manager='pip', application_framework=None)'
Registering workflow 'NodejsNpmBuilder' with capability 'Capability(language='nodejs', dependency_manager='npm', application_framework=None)'
Registering workflow 'RubyBundlerBuilder' with capability 'Capability(language='ruby', dependency_manager='bundler', application_framework=None)'
Registering workflow 'GoDepBuilder' with capability 'Capability(language='go', dependency_manager='dep', application_framework=None)'
Registering workflow 'GoModulesBuilder' with capability 'Capability(language='go', dependency_manager='modules', application_framework=None)'
Registering workflow 'JavaGradleWorkflow' with capability 'Capability(language='java', dependency_manager='gradle', application_framework=None)'
Registering workflow 'JavaMavenWorkflow' with capability 'Capability(language='java', dependency_manager='maven', application_framework=None)'
Registering workflow 'DotnetCliPackageBuilder' with capability 'Capability(language='dotnet', dependency_manager='cli-package', application_framework=None)'
Found workflow 'NodejsNpmBuilder' to support capabilities 'Capability(language='nodejs', dependency_manager='npm', application_framework=None)'
Running workflow 'NodejsNpmBuilder'
Running NodejsNpmBuilder:NpmPack
NODEJS packaging file:C:\Users\cheruvian\workspaces\sam-app\hello-world to C:\Users\CHERUV~1\AppData\Local\Temp\tmp3rug8kkf
executing NPM: ['npm.cmd', 'pack', '-q', 'file:C:\\Users\\cheruvian\\workspaces\\sam-app\\hello-world']
NODEJS packed to hello_world-1.0.0.tgz
NODEJS extracting to C:\Users\CHERUV~1\AppData\Local\Temp\tmp3rug8kkf\unpacked
NodejsNpmBuilder:NpmPack succeeded
Running NodejsNpmBuilder:CopyNpmrc
NodejsNpmBuilder:CopyNpmrc succeeded
Running NodejsNpmBuilder:CopySource
NodejsNpmBuilder:CopySource succeeded
Running NodejsNpmBuilder:NpmInstall
NODEJS installing in: C:\Users\cheruvian\workspaces\sam-app\.aws-sam\build\HelloWorldFunction
executing NPM: ['npm.cmd', 'install', '-q', '--no-audit', '--no-save', '--production']
NodejsNpmBuilder:NpmInstall failed
Traceback (most recent call last):
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\workflows\nodejs_npm\actions.py", line 108, in execute
    ["install", "-q", "--no-audit", "--no-save", "--production"], cwd=self.artifacts_dir
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\workflows\nodejs_npm\npm.py", line 85, in run
    raise NpmExecutionError(message=err.decode("utf8").strip())
aws_lambda_builders.workflows.nodejs_npm.npm.NpmExecutionError: NPM Failed: npm ERR! code ENOLOCAL
npm ERR! Could not install from "..\..\..\shared-libs" as it does not contain a package.json file.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\cheruvian\AppData\Roaming\npm-cache\_logs\2020-04-30T08_36_46_582Z-debug.log

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\workflow.py", line 269, in run
    action.execute()
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\workflows\nodejs_npm\actions.py", line 112, in execute
    raise ActionFailedError(str(ex))
aws_lambda_builders.actions.ActionFailedError: NPM Failed: npm ERR! code ENOLOCAL
npm ERR! Could not install from "..\..\..\shared-libs" as it does not contain a package.json file.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\cheruvian\AppData\Roaming\npm-cache\_logs\2020-04-30T08_36_46_582Z-debug.log

Build Failed
Sending Telemetry: {'metrics': [{'commandRun': {'awsProfileProvided': False, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam build', 'duration': 5577, 'exitReason': 'BuildError', 'exitCode': 1, 'requestId': 'fff5b25a-cb15-4e5b-9efe-edcdd1351b97', 'installationId': 'df7219e8-3473-48e3-9d07-ed6a44dee7f3', 'sessionId': '76da64b5-0cc0-464c-b398-519796eb1624', 'executionEnvironment': 'CLI', 'pyversion': '3.7.6', 'samcliVersion': '0.46.2'}}]}
Telemetry response: 200
Error: NodejsNpmBuilder:NpmInstall - NPM Failed: npm ERR! code ENOLOCAL
npm ERR! Could not install from "..\..\..\shared-libs" as it does not contain a package.json file.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\cheruvian\AppData\Roaming\npm-cache\_logs\2020-04-30T08_36_46_582Z-debug.log

Expected result

  1. Should never delete source files
  2. Should build successfully every time

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

  1. OS: Windows 10 Pro
  2. Shell: gitbash
  3. sam --version: SAM CLI, version 0.46.2

cheruvian avatar Apr 30 '20 08:04 cheruvian

I am facing the same issue here. I would love to hear about a fix (or a workaround). It seems related to this npm issue.

rfrad avatar Jun 15 '20 18:06 rfrad