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

VSCode always show bug BrokenImport #2124

Closed
kytosai opened this issue Mar 20, 2022 · 7 comments
Closed

VSCode always show bug BrokenImport #2124

kytosai opened this issue Mar 20, 2022 · 7 comments

Comments

@kytosai
Copy link

kytosai commented Mar 20, 2022

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

Version Information
  • Run go version to get version of Go from the VS Code integrated terminal.
  • go version go1.17.8 darwin/arm64
  • Run gopls -v version to get version of Gopls from the VS Code integrated terminal.
Build info
----------
golang.org/x/tools/gopls v0.8.1
    golang.org/x/tools/gopls@v0.8.1 h1:q5nDpRopYrnF4DN/1o8ZQ7Oar4Yd4I5OtGMx5RyV2/8=
    github.com/BurntSushi/toml@v1.0.0 h1:dtDWrepsVPfW9H/4y7dDgFc2MBUSeJhlaDtK13CxFlU=
    github.com/google/go-cmp@v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
    github.com/sergi/go-diff@v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
    golang.org/x/mod@v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 h1:kQgndtyPBW/JIYERgdxfwMYh3AVStj88WQTlNDi2a+o=
    golang.org/x/sync@v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
    golang.org/x/sys@v0.0.0-20220209214540-3681064d5158 h1:rm+CHSpPEEW2IsXUib1ThaHIjuBVZjxNgSKmBLFfD4c=
    golang.org/x/text@v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
    golang.org/x/tools@v0.1.10-0.20220315142906-0c66750444e6 h1:AOqgMGxa5hFduIlFkKEX87HoRPO9wjyfhqf1yqwybeg=
    golang.org/x/xerrors@v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
    honnef.co/go/tools@v0.2.2 h1:MNh1AVMyVX23VUHE2O27jm6lNj3vjO5DexS4A1xvnzk=
    mvdan.cc/gofumpt@v0.3.0 h1:kTojdZo9AcEYbQYhGuLf/zszYthRdhDNDUi2JKTxas4=
    mvdan.cc/xurls/v2@v2.4.0 h1:tzxjVAj+wSBmDcF6zBB7/myTy3gX9xvi8Tyr28AuQgc=
go: go1.17.8
  • Run code -v or code-insiders -v to get version of VS Code or VS Code Insiders.
    • Version: 1.65.2 (Universal)
  • Check your installed extensions to get the version of the VS Code Go extension
    • v0.32.0
  • Run Ctrl+Shift+P (Cmd+Shift+P on Mac OS) > Go: Locate Configured Go Tools command.
Checking configured tools....
GOBIN: undefined
toolsGopath: 
gopath: /Users/kytosai/go
GOROOT: /usr/local/go
PATH: /opt/homebrew/opt/php@7.4/sbin:/opt/homebrew/opt/php@7.4/bin:/Users/kytosai/.nvm/versions/node/v16.14.0/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Library/Apple/usr/bin:/Users/kytosai/Library/Android/sdk/emulator:/Users/kytosai/Library/Android/sdk/tools:/Users/kytosai/Library/Android/sdk/tools/bin:/Users/kytosai/Library/Android/sdk/platform-tools:/Users/kytosai/go/bin

	go:	/usr/local/go/bin/go: go version go1.17.8 darwin/arm64

	go-outline:	/Users/kytosai/go/bin/go-outline	(version: v0.0.0-20210608161538-9736a4bde949 built with go: go1.17.8)
	gotests:	/Users/kytosai/go/bin/gotests	(version: v1.6.0 built with go: go1.17.8)
	gomodifytags:	/Users/kytosai/go/bin/gomodifytags	(version: v1.16.0 built with go: go1.17.8)
	impl:	/Users/kytosai/go/bin/impl	(version: v1.1.0 built with go: go1.17.8)
	goplay:	/Users/kytosai/go/bin/goplay	(version: v1.0.0 built with go: go1.17.8)
	staticcheck:	/Users/kytosai/go/bin/staticcheck	(version: v0.2.2 built with go: go1.17.8)
	gopls:	/Users/kytosai/go/bin/gopls	(version: v0.8.1 built with go: go1.17.8)

go env
Workspace Folder (golang-begin): /Users/kytosai/projects/kytosai-projects/golang-begin
	GO111MODULE=""
	GOARCH="arm64"
	GOBIN=""
	GOCACHE="/Users/kytosai/Library/Caches/go-build"
	GOENV="/Users/kytosai/Library/Application Support/go/env"
	GOEXE=""
	GOEXPERIMENT=""
	GOFLAGS=""
	GOHOSTARCH="arm64"
	GOHOSTOS="darwin"
	GOINSECURE=""
	GOMODCACHE="/Users/kytosai/go/pkg/mod"
	GONOPROXY=""
	GONOSUMDB=""
	GOOS="darwin"
	GOPATH="/Users/kytosai/go"
	GOPRIVATE=""
	GOPROXY="https://proxy.golang.org,direct"
	GOROOT="/usr/local/go"
	GOSUMDB="sum.golang.org"
	GOTMPDIR=""
	GOTOOLDIR="/usr/local/go/pkg/tool/darwin_arm64"
	GOVCS=""
	GOVERSION="go1.17.8"
	GCCGO="gccgo"
	AR="ar"
	CC="clang"
	CXX="clang++"
	CGO_ENABLED="1"
	GOMOD="/Users/kytosai/projects/kytosai-projects/golang-begin/go.mod"
	CGO_CFLAGS="-g -O2"
	CGO_CPPFLAGS=""
	CGO_CXXFLAGS="-g -O2"
	CGO_FFLAGS="-g -O2"
	CGO_LDFLAGS="-g -O2"
	PKG_CONFIG="pkg-config"
	GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/xc/5mpwzbh16w15ksnpsy361p0h0000gn/T/go-build1129747829=/tmp/go-build -gno-record-gcc-switches -fno-common"

Share the Go related settings you have added/edited

All setting are default

Describe the bug

I created a project with only 1 file main.go and only printed the screen 123. Although when I type go run main.go from the terminal of vscode everything works normally, but vscode always show bellow error

could not import fmt (cannot find package "fmt" in any of
/usr/local/go/src/fmt (from $GOROOT)
/Users/kytosai/go/src/fmt (from $GOPATH))compiler (BrokenImport)

I don't understand why and don't know how to fix this error ???

Screenshots or recordings

bug

@gopherbot gopherbot added this to the Untriaged milestone Mar 20, 2022
@hyangah
Copy link
Contributor

hyangah commented Mar 20, 2022

Create go.mod to define the workspace boundary.

@kytosai
Copy link
Author

kytosai commented Mar 21, 2022

Create go.mod to define the workspace boundary.

i tried creating go.mod but the result is the same as at the beginning

bug

@hyangah
Copy link
Contributor

hyangah commented Mar 21, 2022

Can you try to restart the language server?
"Go: Restart Language Server" command.
It's possible that the language server didn't expect this type of recovery well.

@kytosai
Copy link
Author

kytosai commented Mar 21, 2022

Can you try to restart the language server? "Go: Restart Language Server" command. It's possible that the language server didn't expect this type of recovery well.

I have tried the following ways but it doesn't work

  • Restart language server
  • Reload window vscode
  • Uninstall/install ext "go"
  • Uninstall/install vscode
  • Disable all ext exclude "go"
  • Uninstall/install "go" pkg on go.dev offical

@hyangah
Copy link
Contributor

hyangah commented Mar 21, 2022

Sorry for the trouble.
What's your settings like?
Follow the instruction here https://github.com/golang/vscode-go/blob/master/docs/troubleshooting.md and share the gopls log.

@kytosai
Copy link
Author

kytosai commented Mar 21, 2022

I fixed my issue by being here
golang/go#42353 (comment)

Yay!! I figured out what issue was

After I did go clean -cache and tried to build my app, it failed with the message

failed to initialize build cache at /Users/mac/Library/Caches/go-build: mkdir /Users/mac/Library/Caches/go-build/00: permission denied

After digging a bit I realized that go-build folder has root as its owner instead of $USER. I changed the owner to current user with the command sudo chown -R $USER ~/Library/Caches/go-build.

Now, everything works fine.

Thank you @stamblerre for your help!!

@kytosai kytosai closed this as completed Mar 21, 2022
@pcgeek86
Copy link

I'm having this same problem in VSCode, running on GitHub Codespaces. I have a Go module, with a single code file, and one additional package underneath the module. My VSCode editor is opened up to a parent directory, not the module directory itself, because the module is contained in a subfolder of my project.

Error loading workspace: You are outside of a module and outside of $GOPATH/src. If you are using modules, please open your editor to a directory in your module. If you believe this warning is incorrect, please file an issue: https://github.com/golang/go/issues/new.

@golang golang locked and limited conversation to collaborators Aug 15, 2023
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

4 participants