aws / aws-lambda-builders

Python library to compile, build & package AWS Lambda functions for several runtimes & framework
Apache License 2.0
329 stars 136 forks source link

Bug: Building a NodeJS Lambda with husky in the "prepare" script fails #635

Closed having-fun-serverless closed 1 month ago

having-fun-serverless commented 3 months ago

Description:

My prepare script runs husky. When running sam build I'm getting a NpmPack error -

Error: NodejsNpmBuilder:NpmPack - [Errno 2] No such file or directory: "/var/folders/pr/73l0p75n6kqbj796pmlgp9q80000gp/T/tmpt3d7o90i/.git can't be foundmypackage-1.0.0.tgz"

After further investigation I found out that the culprit is this piece of code - https://github.com/aws/aws-lambda-builders/blob/bd8bd9e5143ab2a8e083086792a41ee01e50a3bd/aws_lambda_builders/workflows/nodejs_npm/actions.py#L60 It runs npm pack and pulls the last line which is supposed to be the packed file name, but npm pack appends to the last line also the error that husky produces in which it complaints it runs in a non git folder.

As a workaround I added husky > /dev/null 2>&1 to the prepare script.

When running npm pack to simulate the issue I'm getting

npm pack -q file:/Users/user/code/mypackage/.out/dist

> mypackage@1.0.0 prepare
> husky

.git can't be foundmypackage-1.0.0.tgz

Steps to reproduce:

Add

"scripts": {
    "prepare": "husky"
  }

to your package.json

Observed result:

2024-03-28 20:51:15,501 | Running workflow 'NodejsNpmBuilder'                                                                   
2024-03-28 20:51:15,502 |  Running NodejsNpmBuilder:NpmPack                                                                     
2024-03-28 20:51:15,502 | NODEJS packaging file:/Users/user/code/mypackage/.out/dist to                          
/var/folders/pr/73l0p75n6kqbj796pmlgp9q80000gp/T/tmpjrs26f0z                                                                    
2024-03-28 20:51:15,502 | executing NPM: ['npm', 'pack', '-q', 'file:/Users/user/code/mypackage/.out/dist']      
2024-03-28 20:51:15,816 | NODEJS packed to .git can't be foundmypackage-1.0.0.tgz                                                
2024-03-28 20:51:15,817 | NODEJS extracting to /var/folders/pr/73l0p75n6kqbj796pmlgp9q80000gp/T/tmpjrs26f0z/unpacked            
2024-03-28 20:51:15,817 | NodejsNpmBuilder:NpmPack raised unhandled exception                                                   
Traceback (most recent call last):                                                                                              
  File "/opt/homebrew/Cellar/aws-sam-cli/1.113.0/libexec/lib/python3.12/site-packages/aws_lambda_builders/workflow.py", line    
372, in run                                                                                                                     
    action.execute()                                                                                                            
  File                                                                                                                          
"/opt/homebrew/Cellar/aws-sam-cli/1.113.0/libexec/lib/python3.12/site-packages/aws_lambda_builders/workflows/nodejs_npm/actions.
py", line 68, in execute                                                                                                        
    extract_tarfile(tarfile_path, self.artifacts_dir)                                                                           
  File "/opt/homebrew/Cellar/aws-sam-cli/1.113.0/libexec/lib/python3.12/site-packages/aws_lambda_builders/utils.py", line 230,  
in extract_tarfile                                                                                                              
    with tarfile.open(tarfile_path, "r:*") as tar:                                                                              
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                      
  File "/opt/homebrew/Cellar/python@3.12/3.12.2_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/tarfile.py", line    
1802, in open                                                                                                                   
    return func(name, "r", fileobj, **kwargs)                                                                                   
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                   
  File "/opt/homebrew/Cellar/python@3.12/3.12.2_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/tarfile.py", line    
1870, in gzopen                                                                                                                 
    fileobj = GzipFile(name, mode + "b", compresslevel, fileobj)                                                                
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                
  File "/opt/homebrew/Cellar/python@3.12/3.12.2_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/gzip.py", line 192,  
in __init__                                                                                                                     
    fileobj = self.myfileobj = builtins.open(filename, mode or 'rb')                                                            
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                            
FileNotFoundError: [Errno 2] No such file or directory: "/var/folders/pr/73l0p75n6kqbj796pmlgp9q80000gp/T/tmpjrs26f0z/.git can't
be foundmypackage-1.0.0.tgz"                                                                                                     

Build Failed

Expected result:

Pack was successful, so I was expecting NpmPack to succeed as well. The only issue is the last line of the execution.

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

  1. OS: OSX
  2. If using SAM CLI, sam --version: 1.113.0
  3. AWS region: eu-central-1
hawflau commented 3 months ago

Hi @having-fun-serverless thanks for raising the issue. As explained in https://github.com/aws/aws-sam-cli/issues/6835#issuecomment-2000527587, Lambda Builders does not include dev dependencies by default. Could you please try the workarounds mentioned in the comment?

having-fun-serverless commented 3 months ago

Hi @having-fun-serverless thanks for raising the issue. As explained in aws/aws-sam-cli#6835 (comment), Lambda Builders does not include dev dependencies by default. Could you please try the workarounds mentioned in the comment?

Yep, I did just that. The workaround works, the problem is that it's not obvious, people can easily spend hours in understanding the failure.

hnnasit commented 1 month ago

Closing this issue as the logging has been updated.