Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
having-fun-serverless opened this issue Mar 28, 2024 · 3 comments
Labels
area/workflow/node_npm blocked/close-if-inactive Blocked for >14 days with no response, will be closed if still inactive after 7 days

Comments

@having-fun-serverless
Copy link

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 -

tarfile_name = self.subprocess_npm.run(["pack", "-q", package_path], cwd=self.scratch_dir).splitlines()[-1]

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
@having-fun-serverless having-fun-serverless added the stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. label Mar 28, 2024
@hawflau
Copy link
Contributor

hawflau commented Apr 1, 2024

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?

@hawflau hawflau added area/workflow/node_npm blocked/close-if-inactive Blocked for >14 days with no response, will be closed if still inactive after 7 days and removed stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. labels Apr 1, 2024
@having-fun-serverless
Copy link
Author

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
Copy link
Contributor

hnnasit commented Jun 7, 2024

Closing this issue as the logging has been updated.

@hnnasit hnnasit closed this as completed Jun 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/workflow/node_npm blocked/close-if-inactive Blocked for >14 days with no response, will be closed if still inactive after 7 days
Projects
None yet
Development

No branches or pull requests

3 participants