Skip to content

Debugging Golang code in SAM CLI #281

Closed

Description

@tfnaylor

I am able run my SAM Local Golang project just fine (get expected results, etc.) but cannot get my breakpoints to be hit in Visual Studio Code. I am using a Mac and the project is a simple REST API.

The VS Code debugger starts but the break icons never get filled in and breaks don't happen. My launch.json file configuration also gives me a warning that it doesn't recognize the debug type even though I can debug pure Golang applications on this machine without issue (Delve installed, etc.).

My launch.json file configurations section looks like this:

{
            "name": "Attach to SAM Local",
            "type": "go",
            "request": "attach",
            "address": "localhost",
            "port": 5858,
            "localRoot": "${workspaceRoot}/main.go",
            "remoteRoot": "/var/task"
}

I use the configuration to start the debugger, which seems to start normally (the debug control widget appears) and no errors are reported.

I suspect that the standard Go extension for VS Code (lukehoban.go) isn't compatible with this configuration. Is anyone else having this problem?

Activity

  1. afdabro commented on May 25, 2018

    @afdabro

    I've ran into the same issue. It is a pain not being able to debug from SAM/VsCode. I keep the main func light and use a combination of DI and unit testing for the handle request func. Context and ProxyRequest are mockable making unit testing straight forward.

    `
    func init() {
    // DI arguments init
    }

    func main() {
    lambda.Start(handleRequest)
    }

    func handleRequest(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
    return myDIHandleAPIRequest(ctx, request, ... any DI arguments ...)
    }
    `

  2. sanathkr commented on Jun 4, 2018

    @sanathkr

    SAM CLI doesn't have support for debugging Golang support yet. We need an entry point for Golang similar to https://github.com/awslabs/aws-sam-cli/blob/develop/samcli/local/docker/lambda_container.py#L131.

    This should be fairly easy I would think. How do you usually start Go in debugging mode?

  3. changed the title [-]Debugging SAM Local + Golang in VS Code[/-] [+]Debugging Golang code in SAM CLI[/+] on Jun 4, 2018
  4. austinlparker commented on Jun 18, 2018

    @austinlparker
    Contributor

    @sanathkr I started looking into this because we're using a lot of golang + lambda and I've made some progress. Go, out of the box, doesn't really have a debugger (other than gdb or whatever), delve (https://github.com/derekparker/delve) is the thing everyone uses afaik.

    That said, the best way to get dlv inside the container is to actually modify the Dockerfile being used to include it. It looks like those are being maintained by someone else, though, and none of the other runtimes really have a 'change containers based on debugging being enabled or not' feature... I don't think it'd be terribly hard to do. But, I also don't think it'd be unreasonable to suggest that delve gets installed out of the box on those other images.

    I'm trying to get a functioning POC going; So far I've got a new base container image with Delve installed, and an override for the entry point to start up delve as a server, and can connect to it. I'll keep hacking away and try to get it into a state where it can be PR'ed for review and discussion. Would appreciate feedback in the meantime on the basic idea.

  5. austinlparker commented on Jun 19, 2018

    @austinlparker
    Contributor

    Ok, I've got a first pass up that I wouldn't mind some eyes on... couple things I've noticed that I'm not sure how to address.

    When running with start-api, the Python HTTP server side of things just keep trucking along - should it pause, or extend the timeout when debugging is enabled?

    In addition, I don't have a 'debug flags' section included because I can't quite think of a great way to pass them into the container. Unlike other languages, where the entry point is being changed to call the language runtime, the golang one is still calling a wrapper binary... since the way the container works is by calling the wrapper application then running it as a subprocess, I needed to have delve be invoked by that wrapper. I suppose the debug wrapper could be modified more, but since it's a separate project from this that's meant to emulate the lambda container itself, I'm not sure how comfortable they'll be with drift.

  6. jfuss commented on Jun 19, 2018

    @jfuss
    Contributor

    @austinlparker Regarding

    When running with start-api, the Python HTTP server side of things just keep trucking along - should it pause, or extend the timeout when debugging is enabled?

    What do you mean by things keep trucking along? The service should pause to wait for the Local Lambda to return.

  7. austinlparker commented on Jun 19, 2018

    @austinlparker
    Contributor

    It doesn't seem to be pausing forever. Looks like it timed out after around 60s?

    Here's the logs: https://gist.github.com/austinlparker/f12ee4c9048834c99891384c433f4ec3

  8. jfuss commented on Jun 19, 2018

    @jfuss
    Contributor

    Ahh. That's not specific to you (or go). We have an open PR to address it here #422. I haven't kept up on the thread, so will have to give it a read but I think there were some initial concerns with it. But I think that's out of scope for this.

  9. austinlparker commented on Jun 19, 2018

    @austinlparker
    Contributor

    Ah, yeah, that's what's happening. Yeah, doesn't seem related after skimming that thread.

  10. austinlparker commented on Jun 22, 2018

    @austinlparker
    Contributor

    So, I did a bit of a refactor on the container work (see lambci/docker-lambda#97) that caused more changes to the work required on the SAM CLI side. I pushed a first pass of that on #495, but I think I'd like to generalize it some more. Looking at issues like #500, I think I can turn the delve_path into something like debugger_path, which would allow us to use a similar strategy for debugging netcore in the future (by mapping vsdbg into the lambda container).

    That said, it feels a bit gnarly right now so I'd like to get thoughts from @jfuss / @sanathkr before doing cleanup and getting it mergeable.

  11. added
    stage/accepted
    on Jul 2, 2018
  12. jfuss commented on Jul 2, 2018

    @jfuss
    Contributor

    PR was merged, putting issue into 'stage/accepted' to mark this is waiting for a release.

  13. JanDeDobbeleer commented on Jul 17, 2018

    @JanDeDobbeleer

    @jfuss I see the merge got reverted, any idea if/when this functionality will be available?

  14. jfuss commented on Jul 17, 2018

    @jfuss
    Contributor

    @JanDeDobbeleer Sadly yes. :( We had to revert the commit due to some breaking functional/integration tests and didn't have enough time to resolve them before our v0.5.0 release. I am hoping we can get this into our next release.

  15. added
    stage/waiting-for-release
    and removed
    stage/accepted
    on Aug 6, 2018
  16. jfuss commented on Aug 6, 2018

    @jfuss
    Contributor

    Updating labels indicting this is waiting on release

  17. NapalmCodes commented on Aug 6, 2018

    @NapalmCodes

    Any eta on the release?

  18. afdabro commented on Aug 18, 2018

    @afdabro

    Thank you!!

  19. jfuss commented on Aug 29, 2018

    @jfuss
    Contributor

    Closing this as it was released in 0.6.0

  20. removed
    stage/waiting-for-release
    on Aug 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions