Skip to content

"Can't load package" for package main declaration #226

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

Closed
kkuchta opened this issue Jun 15, 2020 · 9 comments
Closed

"Can't load package" for package main declaration #226

kkuchta opened this issue Jun 15, 2020 · 9 comments
Labels
FrozenDueToAge upstream-tools Issues that are caused by problems in the tools that the extension depends on.

Comments

@kkuchta
Copy link

kkuchta commented Jun 15, 2020

I'm trying to set up a hello world go project using modules and vscode. I'm using the go extension without the language server. I'm getting an error on the line package main when it seems like it should build cleanly.

My project consists of two files:

// src/go.mod
module whatever

go 1.14
// src/modtest.go
package main

import "fmt"

func main() {
	fmt.Println("hello, world")
}

VS Code shows an error on package main:

/Users/kevin/code/go/modtest>Finished running tool: /usr/local/bin/go build _/Users/kevin/code/go/modtest/src
can't load package: package _/Users/kevin/code/go/modtest/src: cannot find package "_/Users/kevin/code/go/modtest/src" in any of:
	/usr/local/Cellar/go/1.14.3/libexec/src/_/Users/kevin/code/go/modtest/src (from $GOROOT)
	/Users/kevin/code/go/src/_/Users/kevin/code/go/modtest/src (from $GOPATH

Screen Shot 2020-06-15 at 4 04 50 PM

This seems like vscode-go ought to build this cleanly. go build from within the src directory builds without error, and produces a functional binary. I apologize in advance if I'm just missing a minor configuration, or if I've misunderstood something about go project organization, but any help/advice would be very much appreciated!

What version of Go, VS Code & VS Code Go extension are you using?

  • Run go version to get version of Go
    go version go1.14.3 darwin/amd64
  • Run code -v or code-insiders -v to get version of VS Code or VS Code Insiders
1.46.0
a5d1cc28bb5da32ec67e86cc50f84c67cc690321
x64
  • Check your installed extensions to get the version of the VS Code Go extension
    0.14.4
  • Run go env to get the go development environment details
go env                               
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/kevin/Library/Caches/go-build"
GOENV="/Users/kevin/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/kevin/code/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.14.3/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.14.3/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/h8/t6j1zhv57x97tdzs7rs0g2_h0000gn/T/go-build368033781=/tmp/go-build -gno-record-gcc-switches -fno-common"

Share the Go related settings you have added/edited

    "go.buildOnSave": "workspace",
    "go.formatTool": "goimports"
@stamblerre
Copy link
Contributor

I think you probably want to create a directory below GOPATH/src and put your project in there. It also seems like you intend to be in module mode, but the go command is looking on your GOPATH, so make sure that the GO111MODULE environment variable is not set to off.

Some additional resources are linked in our GOPATH and modules documentation.

@stamblerre stamblerre added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. labels Jun 16, 2020
@kkuchta
Copy link
Author

kkuchta commented Jun 16, 2020

Thanks for the reply! My go env seems to have GO111MODULE set to "" by default. I think, from my reading of the docs, that for new go versions, this should default to having module mode on. Just to be sure, I set go env -w GO111MODULE=on - that changes the error message to:
go: cannot find main module; see 'go help modules', still on the package main line.

I'm not sure I understand the bit about having this project be under GOPATH/src - shouldn't using a module remove the need to worry about the GOPATH? I've read through the linked docs on modules, but it mostly discusses which tools don't support modules - it seems like basic building should work still?

One new wrinkle: I currently have both the files for this project at project_name/src/foo.bar. If I move them to the root level of the project (so just project_name/foo.bar, the project builds cleanly. Is there something about having an src folder that vscode-go doesn't like?

@hyangah
Copy link
Contributor

hyangah commented Jun 16, 2020

I noticed the unusual _ prefix in the go build target.

/usr/local/bin/go build _/Users/kevin/code/go/modtest/src

The go build target is determined by getNonVendorPackages, which retrieves the information by running go list -f 'ImportPath: {{.ImportPath}} FolderPath: {{.Dir}}' ./....

Can you try to run the go list command from the integrated terminal?
go list -f 'ImportPath: {{.ImportPath}} FolderPath: {{.Dir}}' ./...

The ImportPath will include the _ prefix if go.mod file does not exist.
go: cannot find main module also indicates that the go command couldn't find go.mod file.

Is the go.mod file saved?

You can also check whether the go command saw your go.mod file, by running go env GOMOD

@kkuchta
Copy link
Author

kkuchta commented Jun 18, 2020

$ go list -f 'ImportPath: {{.ImportPath}} FolderPath: {{.Dir}}' ./...
go: warning: "./..." matched no packages
$ go env GOMOD
/dev/null

So yeah, it seems like it's not picking up the go.mod file (which, I double-checked, is saved). If I cd into the src folder first, we get results that look more reasonable:

$ cd src
$ go list -f 'ImportPath: {{.ImportPath}} FolderPath: {{.Dir}}' ./...
ImportPath: whatever FolderPath: /Users/kevin/code/go/modtest/src
$ go env GOMOD                                                       
/Users/kevin/code/go/modtest/src/go.mod

It seems like maybe vscode-go isn't able to detect a go.mod that's not at the root level of the project?

@stamblerre
Copy link
Contributor

stamblerre commented Jun 18, 2020

Yep, that's explained a bit more here: golang/go#36899. Does VS Code Go work when you open it from the module root (directory containing the go.mod)?

@kkuchta
Copy link
Author

kkuchta commented Jun 19, 2020

Ah, thanks for that link - that's some useful context. That said, it looks like that only applies to gopls, and I have the "Use Language Server" setting disabled for the vscode-go extension. Presumably that means that link doesn't apply to this situation?

(Yes, opening vs code from the module root seems to work)

@hyangah
Copy link
Contributor

hyangah commented Jun 19, 2020

That's go command's limitation as well - the go command is sensitive to in which directory the go command runs. As the messages in the output channel indicated, the error occurred while building the package in the workspace, and from the workspace, the go command doesn't see the packages under the nested module directory. :-(

@hyangah
Copy link
Contributor

hyangah commented Jun 30, 2020

Closing this in favor of the pinned issue #275.

@hyangah hyangah closed this as completed Jun 30, 2020
@hyangah hyangah added upstream-tools Issues that are caused by problems in the tools that the extension depends on. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. labels Jun 30, 2020
@D4v1dW3bb
Copy link

Unsupported

Open a directory that contains a module in a subdirectory. gopls will not work in this case.

For vscode I have a workaround, do not know if this work for other editors.

My directory structure is:

Projects
  Someproject1
    microservice1
      go.mod
      go.sum
      main.go
    microservice2
      go.mod
      go.sum
      main.go
  Someproject2
  ...

opening "Somepoject1" in vscode causes a problem.
To solve this I have made a second directory structure:

Workspaces
  Someproject1
  Someproject2

If i start working on a project I open the respective directory in "Workspace"s in vscode. There I add al the microservice directories from the corresponding project directory in "Projects" to the workspace.

it's a little bit of work to setup but it works.

no errors any more

@golang golang locked and limited conversation to collaborators Nov 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge upstream-tools Issues that are caused by problems in the tools that the extension depends on.
Projects
None yet
Development

No branches or pull requests

5 participants