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

node_module code doesn't get updated when calling lambda with code lens #801

Closed
XiamiYoung opened this issue Nov 1, 2019 · 7 comments
Closed
Labels
bug We can reproduce the issue and confirmed it is a bug. needs-response Waiting on reply from issue/PR author. sam

Comments

@XiamiYoung
Copy link

Describe the bug

node_module code doesn't get updated when file is changed and called by code lens debug
To Reproduce
I have a module installed in lambda, with an index.js. if I add a console.log inside index.js, and debug lambda with code lens, no console log printed, change is not reflected, however if I start lambda by sam local invoke, I can see the console print.
Where the node module code is cached on Mac OS? how I can force toolkit to use latest code instead?

Expected behavior

Code change is reflected with code lens run/debug.

Screenshots

Desktop (please complete the following information):

  • OS: Mac OS
  • Visual Studio Code Version: 1.39.2
  • AWS Toolkit for Visual Studio Code Version: 1.2.0

Additional context

@XiamiYoung XiamiYoung added the bug We can reproduce the issue and confirmed it is a bug. label Nov 1, 2019
@awschristou
Copy link
Contributor

Hi @XiamiYoung can you please illustrate what the folder structure looks like for the locations of:

  • your sam template file
  • your lambda handler file (and what the name of your handler is)
  • your package.json file
  • the index.js file

Additionally, can you please share the sam template resource properties: CodeUri, Handler, Runtime

@XiamiYoung
Copy link
Author

XiamiYoung commented Nov 3, 2019

Hi @awschristou ,

Thing is my module is installed on a private registry, it will be installed by multiple lambdas as a reuse library(because custom module is not supported by sam cli -- aws/aws-sam-cli#1481), I'm changing my module code inside node_module folder to test aws specific function with sam cli/toolkit.

I can't share my module here as it's private, however I created a hello world lambda by below command to demo the issue:
sam init --runtime nodejs -n helloworld -d npm -o .
The structure is default with a default handler app.js:

helloword:
| events
| template.yaml
| hello-world
| app.js
| package.json
| tests
| node_modules

template is also default:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  helloworld

  Sample SAM Template for helloworld
  
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 3

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: hello-world/
      Handler: app.lambdaHandler
      Runtime: nodejs10.x
      Events:
        HelloWorld:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /hello
            Method: get

Outputs:
  # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
  # Find out more about other implicit resources you can reference within SAM
  # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
  HelloWorldApi:
    Description: "API Gateway endpoint URL for Prod stage for Hello World function"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
  HelloWorldFunction:
    Description: "Hello World Lambda Function ARN"
    Value: !GetAtt HelloWorldFunction.Arn
  HelloWorldFunctionIamRole:
    Description: "Implicit IAM Role created for Hello World function"
    Value: !GetAtt HelloWorldFunctionRole.Arn

Then add folder helloword to VS code, I can see code lens generated, then adding "once" to my dependency for demo purpose, below is my package.json:

{
  "name": "hello_world",
  "version": "1.0.0",
  "description": "hello world sample for NodeJS",
  "main": "app.js",
  "repository": "https://github.com/awslabs/aws-sam-cli/tree/develop/samcli/local/init/templates/cookiecutter-aws-sam-hello-nodejs",
  "author": "SAM CLI",
  "license": "MIT",
  "dependencies": {
    "once": "*"
  },
  "scripts": {
    "test": "mocha tests/unit/"
  },
  "devDependencies": {
    "chai": "^4.2.0",
    "mocha": "^6.1.4"
  }
}

after this run npm install, then modify app.js as below(console.log(once);):

var once = require("once");
let response;

exports.lambdaHandler = async (event, context) => {
    try {
        console.log(once);
        response = {
            'statusCode': 200,
            'body': JSON.stringify({
                message: 'hello world',
                // location: ret.data.trim()
            })
        }
    } catch (err) {
        console.log(err);
        return err;
    }

    return response
};

then run lambda from both terminal(sam local invoke --no-event)l and VS code(Run code lens), got same message:

START RequestId: 24f2c9e6-4590-1bde-1dd0-6c650d2620d4 Version: $LATEST
2019-11-03T13:02:48.404Z 24f2c9e6-4590-1bde-1dd0-6c650d2620d4 INFO
{ [Function: wrapper] strict: [Function: wrapper] }
END RequestId: 24f2c9e6-4590-1bde-1dd0-6c650d2620d4
REPORT RequestId: 24f2c9e6-4590-1bde-1dd0-6c650d2620d4 Duration: 13.88 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 42 MB
{"statusCode":200,"body":"{"message":"hello world"}"}

then modify file: helloword/hello-world/node_modules/once/once.js as below:

module.exports.log = function (msg) { 
  console.log(msg);
};

Now if I run lambda from terminal(sam local invoke --no-event), I get:

START RequestId: 91a942d8-464d-1c48-132d-dc1e1bd91cf2 Version: $LATEST
2019-11-03T13:03:06.498Z 91a942d8-464d-1c48-132d-dc1e1bd91cf2 INFO
{ log: [Function] }
END RequestId: 91a942d8-464d-1c48-132d-dc1e1bd91cf2
REPORT RequestId: 91a942d8-464d-1c48-132d-dc1e1bd91cf2 Duration: 14.80 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 42 MB
{"statusCode":200,"body":"{"message":"hello world"}"}

If I run from VS code with code lens Run:

START RequestId: 8431cca6-1035-1b03-c4a8-1e3a7ff7a7ed Version: $LATEST
2019-11-03T13:03:38.182Z 8431cca6-1035-1b03-c4a8-1e3a7ff7a7ed INFO { [Function: wrapper] strict: [Function: wrapper] }
END RequestId: 8431cca6-1035-1b03-c4a8-1e3a7ff7a7ed
REPORT RequestId: 8431cca6-1035-1b03-c4a8-1e3a7ff7a7ed Duration: 14.34 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 42 MB
{"statusCode":200,"body":"{"message":"hello world"}"}
Local invoke of SAM Application has ended.

So seems toolkit is not updating the code inside node_modules, may I know how I can tell/force toolkit to run with latest code, as I really need a way to debug my shared library.

Thank you

@awschristou
Copy link
Contributor

Thank you for the detailed repro steps @XiamiYoung , we will need to investigate this.

@XiamiYoung
Copy link
Author

@awschristou

Thank you, please let me know once you have update.

@XiamiYoung
Copy link
Author

hi @awschristou ,

may I know if there's a way to "force" delete node_modules cache when I debug lambda from VS Code toolkit?

Thank you

@justinmk3 justinmk3 added needs-response Waiting on reply from issue/PR author. and removed response-requested labels Jan 23, 2020
@justinmk3 justinmk3 added the sam label Jul 30, 2020
@justinmk3
Copy link
Contributor

may I know if there's a way to "force" delete node_modules cache when I debug lambda from VS Code toolkit?

Like an "after" or "before" hook? There is no Toolkit mechanism to do that, though you could add it to scripts in package.json somehow.

@justinmk3
Copy link
Contributor

Since Toolkit 1.12 the SAM local run/debug system was redesigned. Toolkit should always do a sam build, so node_modules should always be updated.

Please try latest Toolkit and comment here if this is still an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug We can reproduce the issue and confirmed it is a bug. needs-response Waiting on reply from issue/PR author. sam
Projects
None yet
Development

No branches or pull requests

3 participants