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

Go Companion: "Run Tests with Coverage" leads to error #3654

Open
t-hofmann opened this issue Jan 8, 2025 · 4 comments
Open

Go Companion: "Run Tests with Coverage" leads to error #3654

t-hofmann opened this issue Jan 8, 2025 · 4 comments
Assignees
Labels
Go Companion Issues relating to the Go Companion extension
Milestone

Comments

@t-hofmann
Copy link

t-hofmann commented Jan 8, 2025

Version Information
  • Run go version to get version of Go from the VS Code integrated terminal.
    • 1.23.4
  • Run gopls -v version to get version of Gopls from the VS Code integrated terminal.
    • v0.17.1
  • Run code -v or code-insiders -v to get version of VS Code or VS Code Insiders.
    • 1.96.2
  • Check your installed extensions to get the version of the VS Code Go extension
    • 0.44.0
  • Go Companion version
    • 0.0.5
  • Run Ctrl+Shift+P (Cmd+Shift+P on Mac OS) > Go: Locate Configured Go Tools command.
[...]
## Tools

	go:	/usr/local/go/bin/go: go version go1.23.4 linux/amd64
	gopls:	/home/USERNAME/go/bin/gopls	(version: v0.17.1 built with go: go1.23.4)
	gotests:	/home/USERNAME/go/bin/gotests	(version: v1.6.0 built with go: go1.23.0)
	gomodifytags:	/home/USERNAME/go/bin/gomodifytags	(version: v1.16.0 built with go: go1.23.0)
	impl:	/home/USERNAME/go/bin/impl	(version: v1.1.0 built with go: go1.23.0)
	goplay:	/home/USERNAME/go/bin/goplay	(version: v1.0.0 built with go: go1.23.0)
	dlv:	/home/USERNAME/go/bin/dlv	(version: v1.23.0 built with go: go1.23.0)
	staticcheck:	/home/USERNAME/go/bin/staticcheck	(version: v0.5.1 built with go: go1.23.4)
[...]

Share the Go related settings you have added/edited

cat ~/.config/Code/User/settings.json | grep go:

        "go.toolsManagement.autoUpdate": true,
                "*.go": "${capture}_test.go"
        "go.toolsEnvVars": {
        "go.testTimeout": "5s",
        "go.disableConcurrentTests": true,
        "go.testExplorer.packageDisplayMode": "nested",
        "go.testExplorer.showDynamicSubtestsInEditor": true,
        "go.testFlags": [
        "go.generateTestsFlags": [
                "-go.gopath",
                "-go.toolsGopath",
                "-go.toolsManagement.go"
        "[go]": {
        "go.coverShowCounts": true,
        "go.coverOnSingleTestFile": true,
        "go.coverOnSingleTest": true,
        "go.coverageDecorator": {
        "go.testOnSave": true,
        "go.coverOnSave": true,

Describe the bug

When running tests with coverage, e. g.:

# via Command Palette:
Test: Run All Tests with Coverage
# or
Test: Run Tests with Coverage in Current File
# or
Test: Run Test at Cursor with Coverage

Then the following error pops up:

An error occurred attempting to run tests: 
Error: Command failed: 
/usr/local/go/bin/go list -m -json all 

go: can't compute 'all' using the vendor directory 
(Use -mod=mod or -mod=readonly to bypass.)

Steps to reproduce the behavior:

Prerequisite is a test-file with a test.

  1. Go to 'Command Palette' [Ctrl-Shift-P]
  2. locate 'Test: Run Tests with Coverage in Current File'
  3. See error

The error message suggests two workarounds, that is to add the option -mod=mod or -mod=readonly, the resulting command would be:
/PATH/go list -m -json all -mod=readonly

@gopherbot gopherbot added this to the Untriaged milestone Jan 8, 2025
@firelizzard18 firelizzard18 self-assigned this Jan 8, 2025
@firelizzard18 firelizzard18 added the Go Companion Issues relating to the Go Companion extension label Jan 8, 2025
firelizzard18 added a commit to firelizzard18/exp-vscode-go that referenced this issue Jan 9, 2025
@firelizzard18
Copy link
Contributor

firelizzard18 commented Jan 9, 2025

@hyangah @findleyr Can you confirm that -mod=readonly is the solution? I believe this error is occurring in parseCoverage. The coverage file contains module paths like example.com/package/file.go so I use modules to resolve those into real paths. It sounds like this 'fix' will ignore vendored code which doesn't sound great.

@t-hofmann I pushed a branch with the change. If you're willing I'd appreciate if you tested the fix. I'm attaching a VSIX of the extension with the fix or if you'd prefer you can build it yourself from the branch. GitHub won't let me attach a .vsix so you'll need to change the extension (VSIX files are actually zips so I renamed it to that). exp-vscode-go-0.0.5.zip

@findleyr
Copy link
Member

findleyr commented Jan 9, 2025

@firelizzard18 yes, -mod=readonly will make this work, but will hit the network to download go.mod files. Is that desirable for people using vendoring? Furthermore, the mod directories will be in the module cache, not in the vendor directory.

Can you help me understand what you are trying to achieve with that go list invocation? Then I may be able to better help. (for example, perhaps you would be better off listing only main modules, or perhaps you should be listing packages).

@firelizzard18
Copy link
Contributor

@findleyr Ultimately the point is to resolve paths present in the profile generated by go test -coverprofile=.... If I use Go Companion to collect a coverage profile for tests in gopls, it includes entries like this:

golang.org/x/tools/gopls/internal/analysis/deprecated/deprecated.go:38.64,42.73 3 0
golang.org/x/tools/gopls/internal/analysis/deprecated/deprecated.go:42.73,44.3 1 0
golang.org/x/tools/gopls/internal/analysis/deprecated/deprecated.go:46.2,46.66 1 0
golang.org/x/tools/gopls/internal/analysis/deprecated/deprecated.go:46.66,52.59 2 0

When Hana brought this to my attention in #3597 I asked myself, "How can I resolve these to real paths?" In some circumstances (I'm honestly not sure when/why), this can include entries for dependencies, so my solution was to build a (module path => filesystem path) for all dependencies, and the obvious choice for that seemed to be go list.

Furthermore, the mod directories will be in the module cache, not in the vendor directory.

That's definitely not ideal. If a module is vendored, that vendored code will be used instead of what's in the module cache, correct? If that's true, resolving these paths to the module cache would be incorrect. Though I don't actually know what paths the coverage profile would report for vendored code; I suppose it might report the true path.

As I was implementing this I questioned whether it makes sense for VSCode's/Go Companion's test coverage feature to report coverage for external modules. The UX is not great, because it means VSCode's "Test Coverage" pane has to resort to showing the full path instead of the path relative to the current workspace. I could exclude external modules, but someone is sure to report that as a bug eventually.

Go Companion does have a way to set -coverpkg=./... (more or less) and I'm guessing external modules won't be included without that, but it still seems weird that they're included at all.

@t-hofmann
Copy link
Author

t-hofmann commented Jan 10, 2025

@firelizzard18 The VSIX-version fixed it. And even more: also the file-coverage decorators - they were missing before - now show at various places:

  1. "Explorer View"

  2. "Test View":
    Now both "Test Explorer" and "Test Coverage" are visible, the latter displaying the file coverage (see screenshot). Before only the "Test Explorer" was shown and the "Test Coverage" was neither displayed nor selectable to be displayed.

Screenshot from 2025-01-10 09-45-39

So: Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Go Companion Issues relating to the Go Companion extension
Projects
None yet
Development

No branches or pull requests

4 participants