aws / aws-toolkit-vscode

Amazon Q, CodeCatalyst, Local Lambda debug, SAM/CFN syntax, ECS Terminal, AWS resources
https://marketplace.visualstudio.com/items?itemName=AmazonWebServices.amazon-q-vscode
Apache License 2.0
1.46k stars 407 forks source link

SamLaunchRequestError when running aws-sam debug configuration in VScode #5389

Closed sm0ke21 closed 1 month ago

sm0ke21 commented 1 month ago

Problem

When I try to run a local debugging session using aws-sam I get the error below:

2024-07-28 18:20:37.441 [error] SamLaunchRequestError: Failed to run launch configuration
     -> Error: ToolkitGlobals.codelensRootRegistry accessed, but this property is not set.

The project has been initialized by running the following:

sam init --name sslchecker --runtime nodejs18.x --app-template quick-start-web

My launch.json configuration is as follows:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "aws-sam",
            "request": "direct-invoke",
            "name": "sslchecker:getAllItemsFunction (nodejs18.x)",
            "invokeTarget": {
                "target": "template",
                "templatePath": "${workspaceFolder}/template.yaml",
                "logicalId": "getAllItemsFunction"
            },
            "sam": {
                "dockerNetwork": "sam"
            },
            "lambda": {
                "payload": {
                    "path": "${workspaceFolder}/events/event-get-all-items.json"
                },
                "environmentVariables": {}
            }
        },
        {
            "type": "aws-sam",
            "request": "direct-invoke",
            "name": "API sslchecker:getAllItemsFunction (nodejs18.x)",
            "invokeTarget": {
                "target": "api",
                "templatePath": "${workspaceFolder}/template.yaml",
                "logicalId": "getAllItemsFunction"
            },
            "api": {
                "path": "/",
                "httpMethod": "GET",
                "payload": {
                    "json": {}
                }
            },
            "lambda": {
                "runtime": "nodejs18.x"
            }
        },
        {
            "type": "aws-sam",
            "request": "direct-invoke",
            "name": "sslchecker:getByIdFunction (nodejs18.x)",
            "invokeTarget": {
                "target": "template",
                "templatePath": "${workspaceFolder}/template.yaml",
                "logicalId": "getByIdFunction"
            },
            "lambda": {
                "payload": {},
                "environmentVariables": {}
            }
        },
        {
            "type": "aws-sam",
            "request": "direct-invoke",
            "name": "API sslchecker:getByIdFunction (nodejs18.x)",
            "invokeTarget": {
                "target": "api",
                "templatePath": "${workspaceFolder}/template.yaml",
                "logicalId": "getByIdFunction"
            },
            "api": {
                "path": "/{id}",
                "httpMethod": "GET",
                "payload": {
                    "json": {}
                }
            },
            "lambda": {
                "runtime": "nodejs18.x"
            }
        },
        {
            "type": "aws-sam",
            "request": "direct-invoke",
            "name": "sslchecker:putItemFunction (nodejs18.x)",
            "invokeTarget": {
                "target": "template",
                "templatePath": "${workspaceFolder}/template.yaml",
                "logicalId": "putItemFunction"
            },
            "lambda": {
                "payload": {},
                "environmentVariables": {}
            }
        },
        {
            "type": "aws-sam",
            "request": "direct-invoke",
            "name": "API sslchecker:putItemFunction (nodejs18.x)",
            "invokeTarget": {
                "target": "api",
                "templatePath": "${workspaceFolder}/template.yaml",
                "logicalId": "putItemFunction"
            },
            "api": {
                "path": "/",
                "httpMethod": "POST",
                "payload": {
                    "json": {}
                }
            },
            "lambda": {
                "runtime": "nodejs18.x"
            }
        },
        {
            "type": "aws-sam",
            "request": "direct-invoke",
            "name": "sslchecker:SampleTable",
            "invokeTarget": {
                "target": "template",
                "templatePath": "${workspaceFolder}/template.yaml",
                "logicalId": "SampleTable"
            },
            "lambda": {
                "payload": {},
                "environmentVariables": {}
            }
        }
    ]
}

Steps to reproduce the issue

  1. Create launch.json with above configuration
  2. Select the configuration sslchecker:getAllItemsFunction (nodejs18.x)
  3. Press F5 to start debugging session
  4. See error

Expected behavior

The function should be invoked running in the docker network sam (where dynamodb is currently running).

System details (run AWS: About and/or Amazon Q: About)

OS: Linux x64 5.15.0-117-generic Visual Studio Code extension host: 1.91.1 AWS Toolkit: 3.16.0 node: 20.9.0 electron: 29.4.0

jpinkney-aws commented 1 month ago

Can you try adding the following setting to your package.json "aws.samcli.enableCodeLenses": true and then re-running the launch config

jpinkney-aws commented 1 month ago

As it turns out, as a last resort if we can't find the codeRoot we fall back to discovering the project root:

  1. https://github.com/aws/aws-toolkit-vscode/blob/master/packages/core/src/shared/sam/debugger/typescriptSamDebug.ts#L62
  2. https://github.com/aws/aws-toolkit-vscode/blob/master/packages/core/src/shared/sam/debugger/typescriptSamDebug.ts#L43
  3. findParentProjectFile expects globals.codeLensRegistry to be defined and globals.codeLensRegistry only gets defined when this is true in the settings: "aws.samcli.enableCodeLenses": true

The reason why it goes through that codepath is because originally codeRoot isn't defined because there isn't a CodeUri in the template: https://github.com/aws/aws-toolkit-vscode/blob/master/packages/core/src/lambda/local/debugConfiguration.ts#L134

sm0ke21 commented 1 month ago

Hey there, first of all thanks for the help.

I have added the setting above to my package.json file. I then run sam build and tried to start a debug session and are getting the same error.

{
    "name": "delete-test-01",
    "description": "delete-test-01-description",
    "aws.samcli.enableCodeLenses": true,
    "version": "0.0.1",
    "private": true,
    "dependencies": {
        "@aws-sdk/client-dynamodb": "^3.188.0",
        "@aws-sdk/lib-dynamodb": "^3.188.0"
    },
    "devDependencies": {
        "aws-sdk-client-mock": "^2.0.0",
        "jest": "^29.2.1"
    },
    "scripts": {
        "test": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js"
    },
    "jest": {
        "testMatch": [
            "**/__tests__/**/*.[jt]s?(x)",
            "**/?(*.)+(spec|test).[jt]s?(x)",
            "**/__tests__/**/*.mjs?(x)",
            "**/?(*.)+(spec|test).mjs?(x)"
        ],
        "moduleFileExtensions": [
            "mjs",
            "js"
        ]
    }
}
jpinkney-aws commented 1 month ago

Ah!

"aws.samcli.enableCodeLenses": true should actually be in your VSCode settings.json file

sm0ke21 commented 1 month ago

Hey @jpinkney-aws ,

That does the trick.

sm0ke21 commented 1 month ago

Thank you!