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

'Build on save' fails for module-enabled package inside GOPATH #2238

Closed
nezorflame opened this issue Jan 10, 2019 · 7 comments
Closed

'Build on save' fails for module-enabled package inside GOPATH #2238

nezorflame opened this issue Jan 10, 2019 · 7 comments
Labels
go-modules Related to Go modules

Comments

@nezorflame
Copy link
Contributor

nezorflame commented Jan 10, 2019

Setup

Env

  • GO111MODULE: on

Working directory

$GOPATH/src/test-project

Project layout

  • cmd/
    • test/
      • go.mod
      • go.sum
      • main.go
  • go.mod
  • go.sum
  • test.go

File contents

./test.go:

package test

import (
	"github.com/google/uuid"
)

// SomeUUID ...
func SomeUUID() string {
	return uuid.New().String()
}

./go.mod:

module example.com/nezorflame/test

require github.com/google/uuid v1.1.0

cmd/test/main.go:

package main

import (
	"fmt"

	"example.com/nezorflame/test"
)

func main() {
	fmt.Println(test.SomeUUID())
}

cmd/test/go.mod:

module example.com/nezorflame/test/cmd/test

require example.com/nezorflame/test v0.0.0

replace example.com/nezorflame/test => ../..

The problem

When trying to check for package errors with Build on save action inside the test.go, I get an error:

[go] can't load package: package test-project: unknown import path "test-project": cannot find module providing package test-project

Same happens with the cmd/test/main.go:

[go] can't load package: package test-project/cmd/test: unknown import path "test-project/cmd/test": cannot find module providing package test-project/cmd/test

Command output:

/Users/nezorflame/Go/src/test-project/cmd/test>Finished running tool: /Users/nezorflame/.gvm/gos/go1.11.4/bin/go build -o /var/folders/j3/c8l_b0nj4w5b6zmrgx3z755c0000gp/T/vscode-goXfH5T2/go-code-check test-project/cmd/test can't load package: package test-project/cmd/test: unknown import path "test-project/cmd/test": cannot find module providing package test-project/cmd/test

The error text and command output indicates that Build on save action detected that my package is inside $GOPATH (which is correct), but it uses relative path test-project/cmd/test instead of absolute path.

Suggestion on how to fix it

From my experiments, it seems that using the absolute path fixes the issue:

~ /Users/nezorflame/.gvm/gos/go1.11.4/bin/go build -o /var/folders/j3/c8l_b0nj4w5b6zmrgx3z755c0000gp/T/vscode-goXfH5T2/go-code-check /Users/nezorflame/Go/src/test-project/cmd/test`
[Command succeeds]
@nezorflame nezorflame changed the title Build on save fails for module-enabled package inside GOPATH 'Build on save' fails for module-enabled package inside GOPATH Jan 10, 2019
@ramya-rao-a ramya-rao-a added go-modules Related to Go modules bug labels Mar 12, 2019
@ramya-rao-a
Copy link
Contributor

Thanks for reporting this @nezorflame! And apologies for not getting to this sooner.

We try to deduce the import path of a package based on the folder path and the GOPATH it is under. But when using modules under GOPATH, as you have shown this will not yield the right import path for the package.

When fixing this, I noticed that 2 other features (debugging and the Go: Install Current Package command) are also broken due to the exact same reason.

I have pushed a fix for all of this, would you mind giving the latest beta version of this extension a try?

@nezorflame
Copy link
Contributor Author

Thanks @ramya-rao-a! I'll try this today and will let you know.

@nezorflame
Copy link
Contributor Author

Yup, seems to work well.

@qjerome
Copy link

qjerome commented Mar 27, 2019

Hello,
I still have the same issue, has this change been pushed to the latest release?
BTW, I have noticed it works if the package is built with a relative path like this (since this command is executed from package directory):
go build -o /tmp/vscode-gof3phkT/go-code-check ./

@nezorflame
Copy link
Contributor Author

@qjerome there wasn't a release yet, use latest beta.

@qjerome
Copy link

qjerome commented Mar 27, 2019

awesome, thanks

@ramya-rao-a
Copy link
Contributor

The fix for this issue is out in the latest release(0.10.0) of the Go extension

@vscodebot vscodebot bot locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
go-modules Related to Go modules
Projects
None yet
Development

No branches or pull requests

3 participants