Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

go.testFlags are not applied correctly when using the "debug test" code lens #2115

Closed
segevfiner opened this issue Nov 11, 2018 · 14 comments
Closed
Labels

Comments

@segevfiner
Copy link
Contributor

Steps to Reproduce:

  1. Save:

foo_test.go:

package foo

import (
	"flag"
	"testing"
)

var ok = flag.Bool("ok", false, "")

func TestFoo(t *testing.T) {
	if !*ok {
		t.Fail()
	}
}

.vscode/settings.json:

{
    "go.testFlags": ["-ok"]
}
  1. Open foo_test.go and click the "run test" code lens, the test will pass.
  2. Click the "debug test" code lens, the test will fail.
@ramya-rao-a
Copy link
Contributor

I believe this is because the run test codelens runs the test as go test -run ^TestFoo$ -ok whereas the debug codelens starts delve and passes -ok as value for the --build-flags argument.

The debug test codelens always had its limitations, especially when the test relied on arguments being passed to it. This is such a case and the workaround is to fallback to using a debug configuration in the launch.json file and pass -ok as part of the args property.

@rubenzinho07
Copy link

rubenzinho07 commented Nov 13, 2018 via email

@segevfiner
Copy link
Contributor Author

From go test --help

The 'go test' command rewrites or removes recognized flags,
as appropriate, both before and after the optional package list,
before invoking the test binary.

For instance, the command

	go test -v -myflag testdata -cpuprofile=prof.out -x

will compile the test binary and then run it as

	pkg.test -test.v -myflag testdata -test.cpuprofile=prof.out

And in code go/src/cmd/go/internal/test/testflag.go#testFlags.

I guess either us or dlv will need to pickup this logic to properly separate and rewrite the test arguments as done when running go test. Or otherwise separate the test build/test run flags in our configuration and pass them appropriately, which feels less "clean".

@segevfiner
Copy link
Contributor Author

The debug test codelens always had its limitations, especially when the test relied on arguments being passed to it. This is such a case and the workaround is to fallback to using a debug configuration in the launch.json file and pass -ok as part of the args property.

I don't think there is a convenient way to get a "debug test under cursor" this way... You will have to constantly edit the launch.json to debug a single test this way.

@brycekahle
Copy link
Contributor

The debug test codelens always had its limitations, especially when the test relied on arguments being passed to it. This is such a case and the workaround is to fallback to using a debug configuration in the launch.json file and pass -ok as part of the args property.

@ramya-rao-a Could we add a new config value go.testArgs that is specifically for args during tests? That would solve this particular issue.

@brycekahle
Copy link
Contributor

A lot of limitations of the debug test code lens are because it uses a hard-coded launch config:
https://github.com/Microsoft/vscode-go/blob/6d3476914e841008e1692960b3f64904b5684b69/src/goRunTestCodelens.ts#L18-L26

@ramya-rao-a
Copy link
Contributor

Could we add a new config value go.testArgs that is specifically for args during tests? That would solve this particular issue.

#1534 tracked something similar for running tests. This makes use of the -args flag which is passed before rest of the real args. Maybe we can do something similar for debug scenario.

Instead of passing all the flags as buildFlags, the ones appearing after args are passed as args at https://github.com/Microsoft/vscode-go/blob/latest/src/goRunTestCodelens.ts#L100

Thoughts?

@sjwl
Copy link

sjwl commented Mar 26, 2019

@ramya-rao-a I tried the workaround you mentioned

the workaround is to fallback to using a debug configuration in the launch.json file and pass -ok as part of the args property.

{
    // 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": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${fileDirname}",
            "env": {},
            "args": ["-v"]
        }
    ]
}

But the -v isn't passed as a flag during go test.

@nilekhc
Copy link

nilekhc commented Mar 27, 2019

@ramya-rao-a I am facing same problem.

@ramya-rao-a
Copy link
Contributor

@sjwl, @apollocse Add "trace": "log" to your debug configuration. This will print out exactly how dlv is being called into the debug console. Review that call and see if we are calling dlv the right way

@lfourky
Copy link

lfourky commented Nov 19, 2019

Any updates for this? I am still having this issue (see referenced issue #2894 above ^)

@huanghantao
Copy link

I have the same problem.

@ramya-rao-a
Copy link
Contributor

The latest beta version of this extension respects the -args passed to the go.testFlags setting. Any flags in the array that appear before -args get passed as "build flags" to delve. The rest get passed as "args".

Can folks here please try it out and let us know how it goes? We will release an official update next week

@ramya-rao-a
Copy link
Contributor

The latest version of this extension (0.12.0) has this feature now.
Values passed after -args in go.testFlags will be treated as arguments. The ones before will be treated as build flags.

Thanks @segevfiner for reporting and everyone else for pitching in :)

Happy Coding!

@vscodebot vscodebot bot locked and limited conversation to collaborators Feb 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

8 participants