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

Debug: noDebug build does not pass buildFlags correctly #1027

Closed
hyangah opened this issue Dec 12, 2020 · 9 comments
Closed

Debug: noDebug build does not pass buildFlags correctly #1027

hyangah opened this issue Dec 12, 2020 · 9 comments
Labels
Debug Issues related to the debugging functionality of the extension. FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.

Comments

@hyangah
Copy link
Contributor

hyangah commented Dec 12, 2020

From the code, I see the buildFlags are passed when running go build

if (launchArgs.buildFlags) {
build.push(launchArgs.buildFlags);
}

However, this flag seems to be lost somewhere. I am not sure who's stripping off this. (node.js? go? something in between?)

How to reproduce:

$ cat main.go
package main

var V = "Bye"

func main() {
	println(V)
}

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${fileDirname}",
            "buildFlags": "-ldflags='-X main.V=Hello'",
        }
    ]
}

With F5 (run by delve) the program will print 'Hello' as expected.

With Ctrl+F5 (built with go build and then run) the program will print 'Bye'.

If I use "buildFlags": "-ldflags=-X main.V=Hello", Ctrl+F5 works as expected, but that breaks F5 and prevents passing additional flags. (Q. Why is the launch arg's buildFlags is string, not string[]???)

Maybe related - see the following: if I pass quoted "-ldflags=\"-X main.V=Hello\", this flag is lost somewhere and not used in go link step at all. Maybe this could be a mistake on my side, but silently dropping the flag is pretty annoying.

 $ node -e 'require("child_process").execFile("go", ["build", "-o=hello", "-x", "-v", "-ldflags=\"-X main.V=Hello\"", "main.go"], (err, stdout, stderr) => { console.log(err, stdout, stderr) })'
null  WORK=/var/folders/bw/6r6k9d113sv1_vvzk_1kfxbm001py5/T/go-build434685295
mkdir -p $WORK/b001/
cat >$WORK/b001/importcfg.link << 'EOF' # internal
packagefile command-line-arguments=/Users/hakim/Library/Caches/go-build/2a/2af9cb66592368ea7dfeada2ab9ca250d9790d35f4bfdfae36f98fced2a18d69-d
packagefile runtime=/usr/local/go/pkg/darwin_amd64/runtime.a
...
mkdir -p $WORK/b001/exe/
cd .
/usr/local/go/pkg/tool/darwin_amd64/link -o $WORK/b001/exe/a.out -importcfg $WORK/b001/importcfg.link -buildmode=exe -buildid=h5dkj0otzSBVSupackWn/dh-q1CTeFGroUuR0JE04/7nT150mh-SjHVBoGjykk/h5dkj0otzSBVSupackWn -extld=clang /Users/hakim/Library/Caches/go-build/2a/2af9cb66592368ea7dfeada2ab9ca250d9790d35f4bfdfae36f98fced2a18d69-d
/usr/local/go/pkg/tool/darwin_amd64/buildid -w $WORK/b001/exe/a.out # internal
mv $WORK/b001/exe/a.out hello
rm -r $WORK/b001/

cc @suzmue

@AlbinoGeek
Copy link

AlbinoGeek commented Dec 12, 2020

Thank you so much for filing this, I asked in the Slack because I wanted to rule out misconfiguration on my part.
I am not using the nightly version in my experience, just the regular version of the vscode extension.

Multiple columns = multiple versions tested

Program Version ...
OS Fedora Workstation 33
gopls v0.6.0-pre.1 v0.5.x
golang go1.15.6 linux/amd64
vscode code-1.52.0-1607640984.el7 code-1.51.1-1605051146.el7
vscode-go 0.19.1 0.18.1

Live Reproduction

With single quotes:

https://github.com/AlbinoGeek/sc2-rsu/blob/15b9f4998bd77d1846873053d8ce129d67ca2d3c/.vscode/launch.json#L15-L15

  • Ctrl+F5 runs the program, but without respecting ldflags whatsoever.
  • F5 stalls then reports on STOP: Process exiting with code: null

With escaped double-quotes:

https://github.com/AlbinoGeek/sc2-rsu/blob/ab695472bf27718855319bde7a1dbd9275db75dc/.vscode/launch.json#L15-L15

  • Ctrl+F5 runs the program, but without respecting ldflags whatsoever.
  • F5 reports:
invalid value "\"-X" for flag -ldflags: missing =<value> in <pattern>=<value>
usage: go build [-o output] [-i] [build flags] [packages]
Run 'go help build' for details.
exit status 2
Process exiting with code: 1

@hyangah
Copy link
Contributor Author

hyangah commented Dec 14, 2020

I am afraid the extension has to parse the buildFlags and avoid passing quotes.

The go command silently ignores the flag if quoted (so invalid) value is passed. golang/go#43177

@hyangah hyangah added the Debug Issues related to the debugging functionality of the extension. label Dec 14, 2020
@hyangah hyangah added this to the v0.19.2 milestone Dec 14, 2020
@AlbinoGeek
Copy link

Without ldflags I cannot use vscode-go to debug/run my application. Guess it's back to gdb for me for a while. Will definitely be following #860 for more information.

@hyangah
Copy link
Contributor Author

hyangah commented Dec 15, 2020

@AlbinoGeek Let me clarify - this is a bug affecting only when using 'Run without Debug' (Ctrl+F5). You can still specify ldflags using buildFlags setting for Run (F5). That will ask delve to run your program.

@AlbinoGeek
Copy link

The issue boils down to:

  • vscode-go provides no mechanism to pass -ldflags "-X main.PROGRAM=\"sc2-rsu\"" to the build.
  • Adding this to the buildFlags field of launch.json results in two non-working scenarios described above.
    • (one with F5)
    • (one with Ctrl+F5)

@hyangah
Copy link
Contributor Author

hyangah commented Dec 15, 2020

@AlbinoGeek

"buildFlags": "-ldflags='-X main.PROGRAM=sc2-rsu -X main.VERSION=1.debug'",

(The first one in your example) This form should work with F5. If not, that's a different issue.

If the quotes around sc2-rsu is critical, please try
` "buildFlags": "-ldflags='-X main.PROGRAM="sc2-rsu" -X main.VERSION=1.debug"'

Screen Shot 2020-12-15 at 8 54 24 AM

At least, with F5, I hope you can continue debugging your program.

@suzmue FYI here is how delve is processing buildFlags https://github.com/go-delve/delve/blob/f559c3c421dcb0777896114673f2f270a2895290/pkg/gobuild/gobuild.go#L44-L64
And, this is not a regression in v0.19.X. This issue around noDebug mode build flag parsing existed for a while.

@hyangah hyangah added the NeedsFix The path to resolution is known, but the work has not been done. label Dec 15, 2020
@AlbinoGeek
Copy link

@hyangah Unfortunately, as I described in my previous reply, it does not work with or without Ctrl.

See the Live Reproduction section of this comment: #1027 (comment)

@hyangah
Copy link
Contributor Author

hyangah commented Dec 16, 2020

@AlbinoGeek That looks like a different issue (sorry I couldn't build your example). Either provide a minimal repro case or, add the following in your launch.json to capture the log and share with us. Thanks!

 "showLog": true,
 "logOutput": "rpc",
 "trace": "verbose"

@hyangah
Copy link
Contributor Author

hyangah commented Feb 1, 2021

Closing in favor of #1111

@hyangah hyangah closed this as completed Feb 1, 2021
@golang golang locked and limited conversation to collaborators Aug 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Debug Issues related to the debugging functionality of the extension. FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

3 participants