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

How can I debug projects with this structure in VSCODE? #215

Open
adrianescat opened this issue Feb 14, 2023 · 2 comments
Open

How can I debug projects with this structure in VSCODE? #215

adrianescat opened this issue Feb 14, 2023 · 2 comments

Comments

@adrianescat
Copy link

adrianescat commented Feb 14, 2023

Hey! I have installed delve, and is running fine on my PC. I'm trying to use this config in the launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Delve",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "program": "${workspaceRoot}/cmd/web",
            "env": {},
            "args": [],
            "showLog": true
        }
    ],
}

I have the main .go files inside the cmd/web folder. The problem is that I have helpers, dbmodels, error handling inside internal. folder too.

vscode starts the app correctly, but as soon as it requires things from internal or other folders in this structure outside /cmd/web it starts getting errors. It's like the packages imports are not working correctly. If I put everything inside the /cmd/web folder I can debug and put breakpoints in the whole app

How do you debug web API's with this structure?

@kcq
Copy link
Member

kcq commented Aug 19, 2023

@adrianescat not an VSCode expert, but will look for a solution. if anybody wants to help that'll be cool too :)

@Challanger524
Copy link

@adrianescat, try to split debug into build+exec(tasks.json+launch.json) like here, to keep working eviroment for debugging program in project root dir.
laucn.json:

// https://code.visualstudio.com/docs/editor/debugging#_launchjson-attributes
{
  "version": "0.2.0",
  "configurations": [
    { /* Debug Exec (faster launch) + visible build output in terminal */
      "name": "Debug Exec (faster launch)",
      "type": "go",
      "request": "launch",
      "mode": "exec",
      "preLaunchTask": "build", // <-- task build dependency
      "program": "${workspaceRoot}/web",
    },
  ]
}

tasks.json:

// https://code.visualstudio.com/docs/editor/tasks
{
  "version": "2.0.0",
  "type": "shell",
  "command": "go",
  "cwd": "${workspaceFolder}", // <-- should allow resource usage (in theory)
  "problemMatcher": [ "$go" ],
  "presentation": {
    "clear": true,
    "reveal": "silent",
  },
  "tasks": [
    { /* build */
      "label": "build",
      "group": "build",
      "args": [
        "build",
        "-v", "-x", "-gcflags='all=-N -l'", "-ldflags='-extldflags=-static'",
        "-o=web",
        "./cmd/web/"
      ],
    },
  ],
}

cmd/web/main.go

package main

import (
	"fmt"
	"os"
)

func main() {
	if _, err := os.Stat("readme.md"); err == nil {    // project's root readme
		fmt.Println("File exists\n")                        // <-- works!
	} else {
		fmt.Println("File does not exist\n")
	}
}

debug console:

Starting: C:\Users\...\go\bin\dlv.exe dap --listen=127.0.0.1:60418 from C:\...\<project_dir>
DAP server listening at: 127.0.0.1:60418
Type 'dlv help' for list of commands.
File exists    <-- here we see root Readme

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants