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

Compiler (BrokenImport) (for IntelliSense and testing) but go build and go test works #2761

Closed
dsm-72 opened this issue May 5, 2023 · 2 comments

Comments

@dsm-72
Copy link

dsm-72 commented May 5, 2023

Issue description

I am having trouble setting up a Golang project to work with the Golang extension in VSCode. I have followed the instructions for setting up a Golang project and have been able to build and test my project successfully using the command line (go build and go test ./...). However, the Golang extension in VSCode is giving me an error when I try to import a package (gonum.org/v1/gonum/mat). The error message says that the package cannot be found in either $GOROOT or $GOPATH, but I have confirmed that the package is installed under $GOPATH/pkg/mod/gonum.org/v1/[email protected]/mat. Additionally, the Golang extension does not seem to recognize my project and I am unable to run tests or use other features of the extension.

Steps to reproduce

  1. Install the Golang extension v0.38.0 in VSCode.

  2. Create a new Golang project with the following directory structure:

    my_project/
    ├── PackageName/
    │   ├── subpkg/
    │   │   └── subpkg.go
    │   ├── go.mod
    │   └── main.go
    
  3. Add the following contents to the go.mod file:

    module github.com/USER/PackageName
    
    go 1.20
    
    require gonum.org/v1/gonum v0.13.0 // indirect
    

Note: go get gonum.org/v1/gonum/mat and go get -u gonum.org/v1/gonum/mat results in require gonum.org/v1/gonum v0.13.0

  1. Add the following contents to the main.go file:

    package main
    
    import (
        "fmt"
        "github.com/USER/PackageName/subpkg"
    )
    
    func main() {
        subpkg.Hello()
    }
  2. Add the following contents to the subpkg/subpkg.go file:

package subpkg

import (
	"fmt"
	"gonum.org/v1/gonum/mat"
)

func Hello() {
	mat := mat.NewDense(4, 5, nil)
	fmt.Sprintf("Hello from subpk. mat=%v", mat)
}
  1. Open the my_project directory in VSCode and attempt to run tests using the VSCode test runner or use other features of the Golang extension.

Expected behavior

The Golang extension should recognize the project and allow me to run tests and use other features of the extension. The package gonum.org/v1/gonum/mat should be found and able to be imported.

Actual behavior

The Golang extension does not recognize the project and I am unable to run tests or use other features of the extension. The package gonum.org/v1/gonum/mat cannot be found and gives an error when trying to import.

System information

  • OS: Ventura 13.3.1 (a)
  • VSCode: version:1.78.0 arm64
  • Golang version: go version go1.20.4 darwin/arm64
  • $GOPATH environment variable: /Users/USER/go/
  • $GOROOT environment variable: /opt/homebrew/opt/go/libexec

Additional information

  • Output of go version command: go version go1.20.4 darwin/arm64
  • Output of go env command:
$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/USER/Library/Caches/go-build"
GOENV="/Users/USER/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/USER/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/USER/go/"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/opt/homebrew/opt/go/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/opt/homebrew/opt/go/libexec/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.20.4"
GCCGO="gccgo"
AR="ar"
CC="cc"
CXX="c++"
CGO_ENABLED="1"
GOMOD="/Users/USER/Projects/my_project/PackageName/go.mod"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/35/0b6fp_4n3c35y2f9yxx_wkvr0000gn/T/go-build3458128548=/tmp/go-build -gno-record-gcc-switches -fno-common"
  • List of installed VSCode extensions: Go v0.38.0
  • Log files: N/a
@gopherbot gopherbot added this to the Untriaged milestone May 5, 2023
@findleyr
Copy link
Member

findleyr commented May 9, 2023

Hi @dsm-72 this is currently a limitation of gopls' workspace detection. You will need to either open the PackageName directory in your example, or use a go.work file to tell gopls which modules you are working on.

We're in the process of improving this workflow, in golang/go#57979.

@findleyr
Copy link
Member

findleyr commented May 9, 2023

Closing as a dupe of golang/go#57979 (we have other issues open to improve error messages in this case).

Please comment if you are not able to get this working.

@findleyr findleyr closed this as not planned Won't fix, can't repro, duplicate, stale May 9, 2023
@golang golang locked and limited conversation to collaborators May 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants