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.51k stars 437 forks source link

Golang Debugger for Lambdas throws "Failed to install Delve for lambda container" #4156

Open dan-dragan opened 11 months ago

dan-dragan commented 11 months ago

Problem

I am trying to locally debug a golang-based lambda. As I press the Run/Start Debugging menu, nothing happens for a while then a message pops up stating "Failed to install Delve for the lambda container."

I can start dlv from the command line.

This also works:

sam local start-api --invoke-image artifactory.mydomain/sam/build-go1.x

Steps to reproduce the issue

  1. Go to Run/Start Debugging menu,
  2. Click on 'Start Debugging'
  3. See error above

Expected behavior

The lambda should load and the breakpoints in code should be active.

System details

OS: Darwin arm64 22.6.0 Visual Studio Code extension host: 1.84.2 AWS Toolkit: 2.2.0 node: 18.15.0 electron: 25.9.2

GO111MODULE=''
GOARCH='arm64'
GOBIN='/Users/myuser/go/bin'
GOCACHE='/Users/myuser/Library/Caches/go-build'
GOENV='/Users/myuser/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/myuser/go/pkg/mod'
GONOPROXY='\*.mydomain'
GONOSUMDB='\*.mydomain'
GOOS='darwin'
GOPATH='/Users/myuser'
GOPRIVATE='\*.mydomain'
GOPROXY='https://artifactory.mydomain/go/go-shared'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.21.3'
GCCGO='gccgo'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK='/Users/myuser/repos/myrepo/golang-go-al2/go.work'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/mf/6rd4nk591kz7d2qyjxydnm4h0000gp/T/go-build320416223=/tmp/go-build -gno-record-gcc-switches -fno-common'

template.yaml:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  go-go1.x

  Sample SAM Template for go-go1.x

Globals:
  Function:
    Timeout: 5

Resources:
  S3ReadFileFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: s3-read-file/
      Handler: bootstrap 
      Runtime: go1.x
      Tracing: Active 
      Events:
        CatchAll:
          Type: Api 
          Properties:
            Path: /get-file
            Method: GET
      Environment: 
        Variables:
          PARAM1: VALUE
      Architectures:
        -  arm64
    Metadata:
      BuildMethod: makefile

Outputs:

  S3ReadFileAPI:
    Description: "API Gateway endpoint URL for Prod environment for First Function"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/get-file/"
  S3ReadFileFunction:
    Description: "First Lambda Function ARN"
    Value: !GetAtt S3ReadFileFunction.Arn
  S3ReadFileFunctionIamRole:
    Description: "Implicit IAM Role created forS3ReadFile function"
    Value: !GetAtt S3ReadFileFunctionRole.Arn

launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "aws-sam",
            "request": "direct-invoke",
            "name": "API go1.x:S3ReadFileFunction",
            "invokeTarget": {
                "target": "api",
                "templatePath": "${workspaceFolder}/template.yaml",
                "logicalId": "S3ReadFileFunction"
            },
            "api": {
                "path": "/",
                "httpMethod": "get",
                "payload": {
                    "json": {}
                }
            },
            "lambda": {
                "runtime": "go1.x"
            }            
        }
    ]
}

Logs:

023-12-08 16:25:10 [INFO]: CloudFormationTemplateRegistry: building with: Watching **/*.{yaml,yml}, Untitled Files, Excluding /.*devfile\.(yaml|yml)/i, /.*[/\\]\.aws-sam([/\\].*|$)/
2023-12-08 16:25:10 [INFO]: CloudFormationTemplateRegistry: processed 1 items
2023-12-08 16:25:11 [INFO]: SAM CLI location (version: 1.97.0): /usr/local/bin/sam
2023-12-08 16:26:17 [INFO]: The Delve repo was not found in your GOPATH. Downloading in a temporary directory...
2023-12-08 16:26:18 [INFO]: Command: (not started) [/Users/myuser/Library/Application Support/Code/User/globalStorage/amazonwebservices.aws-toolkit-vscode/debuggers/delve/install-1.21.1.sh ]
2023-12-08 16:26:20 [ERROR]: [Delve install script] -> # runtime/cgo
linux_syscall.c:67:13: error: call to undeclared function 'setresgid'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
linux_syscall.c:67:13: note: did you mean 'setregid'?
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/unistd.h:593:6: note: 'setregid' declared here
linux_syscall.c:73:13: error: call to undeclared function 'setresuid'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
linux_syscall.c:73:13: note: did you mean 'setreuid'?
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/unistd.h:595:6: note: 'setreuid' declared here

2023-12-08 16:26:20 [ERROR]: Failed to cross-compile Delve debugger: 'Error: Install script did not generate the Delve binary: exit code 1'
2023-12-08 16:26:20 [ERROR]: SamLaunchRequestError: Failed to install Delve for the lambda container. [NoDelveInstallation]
justinmk3 commented 11 months ago

Since building dlv failed, this issue is different than https://github.com/aws/aws-toolkit-vscode/issues/3984 where build succeeded but attaching the debugger failed.

This also works:

sam local start-api --invoke-image artifactory.mydomain/sam/build-go1.x

That command doesn't install a debugger, which is the tricky part. We try to automate building and installing the Go dlv debugger: https://github.com/aws/aws-toolkit-vscode/blob/170588fcf4b6fb7d2a6eda7eed17262fbeb049f8/src/shared/sam/debugger/goSamDebug.ts#L238

but unfortunately it seems to be failing on your (arm64) machine.

2023-12-08 16:26:20 [ERROR]: [Delve install script] -> # runtime/cgo
linux_syscall.c:67:13: error: call to undeclared function 'setresgid'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
linux_syscall.c:67:13: note: did you mean 'setregid'?
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/unistd.h:593:6: note: 'setregid' declared here

We attempt to cross-compile, but something went wrong. https://github.com/aws/aws-toolkit-vscode/blob/170588fcf4b6fb7d2a6eda7eed17262fbeb049f8/src/shared/sam/debugger/goSamDebug.ts#L31-L33

https://github.com/golang/go/issues/54695 mentions that we might need to set CC to cross-compile from mac to linux, but that wasn't always needed, did something change?

Proposal

If cross-compiling is unreliable, we may need to remove that feature and instead require that a correct dlv build exists on the container.

Related

dan-dragan commented 10 months ago

@justinmk3 thanks for your comment. Since I work in a locked down, corporate environment, github.com and any external repositories are not available. go get will fail in my case. That's probably the cause. A solution that locates, copies and uses a delve already installed and working on the user's machine - at least an alternative solution - might be preferable for locked down corporate envs such as mine.