golang / vscode-go

Go extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=golang.Go
Other
3.87k stars 751 forks source link

go module=on, but import from GOROOT,GOPATH #1496

Closed kaka775905353 closed 3 years ago

kaka775905353 commented 3 years ago

Issue Type: Bug image

could not import github.com/kaka775905353/calculator (cannot find package "github.com/kaka775905353/calculator" in any of \n\tC:\Program Files\Go\src\github.com\kaka775905353\calculator (from $GOROOT) C\src\github.com\kaka775905353\calculator (from $GOPATH) \project\go\src\github.com\kaka775905353\calculator (from $GOPATH))

Extension version: 0.24.2 VS Code version: Code 1.56.1 (e713fe9b05fc24facbec8f34fb1017133858842b, 2021-05-06T10:08:05.141Z) OS version: Windows_NT x64 10.0.19042

findleyr commented 3 years ago

Hi, that error message seems pretty descriptive, and isn't necessarily indicative of a bug. It looks like you are still in GOPATH mode, why do you think you are in module mode? Does your project compile from the command line?

Please also include your go version.

kaka775905353 commented 3 years ago

image image

kaka775905353 commented 3 years ago

image

findleyr commented 3 years ago

Thanks.

That error message suggests that the environment used by vscode/gopls differs from the command line environment you see. Could you please attach your gopls logs? Instructions for how to do so are here: https://github.com/golang/vscode-go/blob/master/docs/troubleshooting.md#collect-gopls-information

kaka775905353 commented 3 years ago

[Error - 上午2:40:04] 2021/05/13 02:40:04 helloworld: no dep handle for github.com/kaka775905353/calculator: no metadata for github.com/kaka775905353/calculator snapshot=26

[Error - 上午2:40:05] 2021/05/13 02:40:05 helloworld: no dep handle for github.com/kaka775905353/calculator: no metadata for github.com/kaka775905353/calculator snapshot=27

[Info - 上午2:40:34] 2021/05/13 02:40:34 background imports cache refresh starting

[Info - 上午2:40:34] 2021/05/13 02:40:34 background refresh finished after 1.0892ms

findleyr commented 3 years ago

Can you please enable -rpc.trace logging, as described at that link? I'd like to see the configuration that is passed to gopls, and the go environment that is logged. For example, from our CI:

[Trace - 10:30:02.523 AM] Sending response 'workspace/configuration - (3)' in 1ms.
Result: [{"completionBudget":"10s","env":{"GO111MODULE":"","GOMODCACHE":"","GOPACKAGESDRIVER":"off","GOPATH":"/tmp/workdir/tmp/gopls-regtest-803416373/TestQuickFixEmptyFiles/full/singleton/gopath","GOPROXY":"file:///tmp/workdir/tmp/gopls-regtest-803416373/TestQuickFixEmptyFiles/full/singleton/proxy","GOSUMDB":"off"},"expandWorkspaceToModule":true,"experimentalDiagnosticsDelay":"10ms","verboseWorkDoneProgress":true}]

[Trace - 10:30:02.751 AM] Received notification 'window/logMessage'.
Params: {"type":3,"message":"2021/05/12 10:30:02 go env for /tmp/workdir/tmp/gopls-regtest-803416373/TestQuickFixEmptyFiles/full/singleton/work\n(root /tmp/workdir/tmp/gopls-regtest-803416373/TestQuickFixEmptyFiles/full/singleton/work)\n(go version go version go1.15.12 openbsd/386)\n(valid build configuration = true)\n(build flags: [])\nGO111MODULE=\nGOCACHE=/tmp/workdir/gocache\nGONOPROXY=\nGOPRIVATE=\nGOROOT=/tmp/workdir/go\nGOFLAGS=\nGOINSECURE=\nGOMOD=/tmp/workdir/tmp/gopls-regtest-803416373/TestQuickFixEmptyFiles/full/singleton/work/go.mod\nGOPATH=/tmp/workdir/tmp/gopls-regtest-803416373/TestQuickFixEmptyFiles/full/singleton/gopath\nGONOSUMDB=\nGOPROXY=file:///tmp/workdir/tmp/gopls-regtest-803416373/TestQuickFixEmptyFiles/full/singleton/proxy\nGOSUMDB=off\nGOMODCACHE=\n\n"}
kaka775905353 commented 3 years ago

image Amazing!!! It works now when I enable -rpc.trace logging.

kaka775905353 commented 3 years ago

image

[Info  - 上午2:52:17] 2021/05/13 02:52:17 Shutdown session
    shutdown_session=1

[Trace - 02:52:17.911 AM] Sending request 'initialize - (0)'.
Params: {"processId":2960,"clientInfo":{"name":"Visual Studio Code","version":"1.56.1"},"locale":"zh-cn","rootPath":"c:\\project\\go\\src","rootUri":"file:///c%3A/project/go/src","capabilities":{"workspace":{"applyEdit":true,"workspaceEdit":{"documentChanges":true,"resourceOperations":["create","rename","delete"],"failureHandling":"textOnlyTransactional","normalizesLineEndings":true,"changeAnnotationSupport":{"groupsOnLabel":true}},"didChangeConfiguration":{"dynamicRegistration":true},"didChangeWatchedFiles":{"dynamicRegistration":true},"symbol":{"dynamicRegistration":true,"symbolKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]},"tagSupport":{"valueSet":[1]}},"codeLens":{"refreshSupport":true},"executeCommand":{"dynamicRegistration":true},"configuration":true,"workspaceFolders":true,"semanticTokens":{"refreshSupport":true},"fileOperations":{"dynamicRegistration":true,"didCreate":true,"didRename":true,"didDelete":true,"willCreate":true,"willRename":true,"willDelete":true}},"textDocument":{"publishDiagnostics":{"relatedInformation":true,"versionSupport":false,"tagSupport":{"valueSet":[1,2]},"codeDescriptionSupport":true,"dataSupport":true},"synchronization":{"dynamicRegistration":true,"willSave":true,"willSaveWaitUntil":true,"didSave":true},"completion":{"dynamicRegistration":true,"contextSupport":true,"completionItem":{"snippetSupport":true,"commitCharactersSupport":true,"documentationFormat":["markdown","plaintext"],"deprecatedSupport":true,"preselectSupport":true,"tagSupport":{"valueSet":[1]},"insertReplaceSupport":true,"resolveSupport":{"properties":["documentation","detail","additionalTextEdits"]},"insertTextModeSupport":{"valueSet":[1,2]}},"completionItemKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]}},"hover":{"dynamicRegistration":true,"contentFormat":["markdown","plaintext"]},"signatureHelp":{"dynamicRegistration":true,"signatureInformation":{"documentationFormat":["markdown","plaintext"],"parameterInformation":{"labelOffsetSupport":true},"activeParameterSupport":true},"contextSupport":true},"definition":{"dynamicRegistration":true,"linkSupport":true},"references":{"dynamicRegistration":true},"documentHighlight":{"dynamicRegistration":true},"documentSymbol":{"dynamicRegistration":true,"symbolKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]},"hierarchicalDocumentSymbolSupport":true,"tagSupport":{"valueSet":[1]},"labelSupport":true},"codeAction":{"dynamicRegistration":true,"isPreferredSupport":true,"disabledSupport":true,"dataSupport":true,"resolveSupport":{"properties":["edit"]},"codeActionLiteralSupport":{"codeActionKind":{"valueSet":["","quickfix","refactor","refactor.extract","refactor.inline","refactor.rewrite","source","source.organizeImports"]}},"honorsChangeAnnotations":false},"codeLens":{"dynamicRegistration":true},"formatting":{"dynamicRegistration":true},"rangeFormatting":{"dynamicRegistration":true},"onTypeFormatting":{"dynamicRegistration":true},"rename":{"dynamicRegistration":true,"prepareSupport":true,"prepareSupportDefaultBehavior":1,"honorsChangeAnnotations":true},"documentLink":{"dynamicRegistration":true,"tooltipSupport":true},"typeDefinition":{"dynamicRegistration":true,"linkSupport":true},"implementation":{"dynamicRegistration":true,"linkSupport":true},"colorProvider":{"dynamicRegistration":true},"foldingRange":{"dynamicRegistration":true,"rangeLimit":5000,"lineFoldingOnly":true},"declaration":{"dynamicRegistration":true,"linkSupport":true},"selectionRange":{"dynamicRegistration":true},"callHierarchy":{"dynamicRegistration":true},"semanticTokens":{"dynamicRegistration":true,"tokenTypes":["namespace","type","class","enum","interface","struct","typeParameter","parameter","variable","property","enumMember","event","function","method","macro","keyword","modifier","comment","string","number","regexp","operator"],"tokenModifiers":["declaration","definition","readonly","static","deprecated","abstract","async","modification","documentation","defaultLibrary"],"formats":["relative"],"requests":{"range":true,"full":{"delta":true}},"multilineTokenSupport":false,"overlappingTokenSupport":false},"linkedEditingRange":{"dynamicRegistration":true}},"window":{"showMessage":{"messageActionItem":{"additionalPropertiesSupport":true}},"showDocument":{"support":true},"workDoneProgress":true},"general":{"regularExpressions":{"engine":"ECMAScript","version":"ES2020"},"markdown":{"parser":"marked","version":"1.1.0"}}},"initializationOptions":{},"trace":"off","workspaceFolders":[{"uri":"file:///c%3A/project/go/src","name":"src"}]}
findleyr commented 3 years ago

Can you upload your full logs, or find the part where it logs the values of GO111MODULE and GOMOD?

Do you have a go.mod file?

kaka775905353 commented 3 years ago

Where is the full logs? gopls.log

kaka775905353 commented 3 years ago
[2021-05-12 20:34:56.891] [renderer1] [error] no supported code action to execute for file:///C:/project/go/src/helloworld/go.sum, wanted []: Error: no supported code action to execute for file:///C:/project/go/src/helloworld/go.sum, wanted []
    at handleResponse (c:\Users\59399\.vscode\extensions\golang.go-0.24.2\dist\goMain.js:77834:48)
    at processMessageQueue (c:\Users\59399\.vscode\extensions\golang.go-0.24.2\dist\goMain.js:77649:17)
    at Immediate.<anonymous> (c:\Users\59399\.vscode\extensions\golang.go-0.24.2\dist\goMain.js:77633:13)
    at processImmediate (internal/timers.js:461:21)
[2021-05-12 20:35:00.433] [renderer1] [error] no supported code action to execute for file:///C:/project/go/src/helloworld/go.sum, wanted []: Error: no supported code action to execute for file:///C:/project/go/src/helloworld/go.sum, wanted []
    at handleResponse (c:\Users\59399\.vscode\extensions\golang.go-0.24.2\dist\goMain.js:77834:48)
    at processMessageQueue (c:\Users\59399\.vscode\extensions\golang.go-0.24.2\dist\goMain.js:77649:17)
    at Immediate.<anonymous> (c:\Users\59399\.vscode\extensions\golang.go-0.24.2\dist\goMain.js:77633:13)
    at processImmediate (internal/timers.js:461:21)
[2021-05-12 20:52:38.689] [renderer1] [error] no imported package for github.com/kaka775905353/calculator: Error: no imported package for github.com/kaka775905353/calculator
    at handleResponse (c:\Users\59399\.vscode\extensions\golang.go-0.24.2\dist\goMain.js:77834:48)
    at processMessageQueue (c:\Users\59399\.vscode\extensions\golang.go-0.24.2\dist\goMain.js:77649:17)
    at Immediate.<anonymous> (c:\Users\59399\.vscode\extensions\golang.go-0.24.2\dist\goMain.js:77633:13)
    at processImmediate (internal/timers.js:461:21)
[2021-05-13 02:51:25.045] [renderer1] [warning] Settings pattern "keyboard.*" doesn't match any settings
[2021-05-13 02:52:19.224] [renderer1] [warning] Settings pattern "keyboard.*" doesn't match any settings
[2021-05-13 02:53:06.444] [renderer1] [warning] Settings pattern "keyboard.*" doesn't match any settings
[2021-05-13 02:54:04.704] [renderer1] [warning] Settings pattern "keyboard.*" doesn't match any settings
[2021-05-13 02:59:42.290] [renderer1] [error] context canceled: Error: context canceled
    at handleResponse (c:\Users\59399\.vscode\extensions\golang.go-0.24.2\dist\goMain.js:77834:48)
    at processMessageQueue (c:\Users\59399\.vscode\extensions\golang.go-0.24.2\dist\goMain.js:77649:17)
    at Immediate.<anonymous> (c:\Users\59399\.vscode\extensions\golang.go-0.24.2\dist\goMain.js:77633:13)
    at processImmediate (internal/timers.js:461:21)
[2021-05-13 03:14:20.706] [renderer1] [warning] Settings pattern "keyboard.*" doesn't match any settings
findleyr commented 3 years ago

Where did you set GO111MODULE=on? In your windows environment? It's possible that modifying -rpc.trace triggered a restart/reconfiguration of gopls that picked up your new environment. If that's the case, restarting vscode would have also worked.

kaka775905353 commented 3 years ago

go env -w GO111MODULE=on

Restarting the computer also did not work. but it works when I enable -rpc.trace logging.

findleyr commented 3 years ago

Restarting the computer also did not work. but it works when I enable -rpc.trace logging.

Huh. I don't know how that would be possible. You did a full reboot of your computer (i.e. not suspend or hibernate)? -rpc.trace does not otherwise affect the configuration of your workspace (unless there's a bug of course, but that would be a very weird bug).

kaka775905353 commented 3 years ago

I don't know why.but it works now. Haha. ~ thanks a lot. image

findleyr commented 3 years ago

Ok, well I wish we could get to the bottom of exactly what happened, but if it works now we probably won'e be able to see exactly what was wrong.

Closing. Please reopen if you have any additional information.

dimisjim commented 3 years ago

For me, adding this config in vscode fixed the issue:

 "gopls": {
        "experimentalWorkspaceModule": true,
}

as I had multiple modules in my workspace.

I also had to set "GO111MODULE=on" in my environment. Removing a previous installation of Go that didn't have the module mode set by default, and then installing 1.16, apparently did not override the previous env.

SAMSONEE commented 2 years ago

In my case, with GO111MODULE=on set both on my cmd environment and vscode/gopls environment, after command go mod init <my-package>, go mod tidy and go get github.com/<someone's username>/<some's repo>, I encountered the same issue "could not import github.com/...". The github.com package was downloaded to $GOMODCACHE, and vscode searched for it in $GOPATH and $GOROOT. I checked my gopls logs and found an error reports something like "you are not in $GOPATH/src, if you are working in a module, please set your workspace to your module". So I realized it's because that, I'm viewing at multiple modules, but not using multiple modules mode (there are multiple go.mod files in my workspace) (Module Mode). So I changed my workspace to the currently working mudule and solved the problem (file - add folder to workspace - select a module you want).

jamalc commented 2 years ago

In my case, with GO111MODULE=on set both on my cmd environment and vscode/gopls environment, after command go mod init <my-package>, go mod tidy and go get github.com/<someone's username>/<some's repo>, I encountered the same issue "could not import github.com/...". The github.com package was downloaded to $GOMODCACHE, and vscode searched for it in $GOPATH and $GOROOT. I checked my gopls logs and found an error reports something like "you are not in $GOPATH/src, if you are working in a module, please set your workspace to your module". So I realized it's because that, I'm viewing at multiple modules, but not using multiple modules mode (there are multiple go.mod files in my workspace) (Module Mode). So I changed my workspace to the currently working mudule and solved the problem (file - add folder to workspace - select a module you want).

Please file a new issue.

findleyr commented 2 years ago

Please file a new issue.

It sounds like using a single module fixed the problem. Another possibility (if on Go 1.18) is to use a go.work file.

haiwood commented 2 years ago

For me, adding this config in vscode fixed the issue:

 "gopls": {
        "experimentalWorkspaceModule": true,
}

as I had multiple modules in my workspace.

I also had to set "GO111MODULE=on" in my environment. Removing a previous installation of Go that didn't have the module mode set by default, and then installing 1.16, apparently did not override the previous env.

you just saved me!!!

farums commented 2 years ago

For me, adding this config in vscode fixed the issue:

 "gopls": {
        "experimentalWorkspaceModule": true,
}

as I had multiple modules in my workspace.

I also had to set "GO111MODULE=on" in my environment. Removing a previous installation of Go that didn't have the module mode set by default, and then installing 1.16, apparently did not override the previous env.

Good

youngqqcn commented 2 years ago

For me, adding this config in vscode fixed the issue:

 "gopls": {
        "experimentalWorkspaceModule": true,
}

as I had multiple modules in my workspace.

I also had to set "GO111MODULE=on" in my environment. Removing a previous installation of Go that didn't have the module mode set by default, and then installing 1.16, apparently did not override the previous env.

add this config in setting.json (user space) , it's work for me

Prateeknandle commented 1 year ago

"experimentalWorkspaceModule": true,

I'm geeting this warning/error: Invalid settings: experimentalWorkspaceModule has been replaced by go workspaces, and will be removed in a future version of gopls (https://go.dev/issue/55331) -- see https://github.com/golang/tools/blob/master/gopls/doc/workspace.md for information on setting up multi-module workspaces using go.work files

loumzy commented 1 year ago

I have the same problem. I found myself installed go and go nightly, delete anyone of it can solve the problem for me. go version 1.19.3

WeChatb37acd4ba9b70acceee35afa8dd06a20

"experimentalWorkspaceModule": true,

I'm geeting this warning/error: Invalid settings: experimentalWorkspaceModule has been replaced by go workspaces, and will be removed in a future version of gopls (https://go.dev/issue/55331) -- see https://github.com/golang/tools/blob/master/gopls/doc/workspace.md for information on setting up multi-module workspaces using go.work files

bobkolodkin commented 1 year ago

I have the same problem. I found myself installed go and go nightly, delete anyone of it can solve the problem for me. go version 1.19.3

WeChatb37acd4ba9b70acceee35afa8dd06a20

"experimentalWorkspaceModule": true,

I'm geeting this warning/error: Invalid settings: experimentalWorkspaceModule has been replaced by go workspaces, and will be removed in a future version of gopls (https://go.dev/issue/55331) -- see https://github.com/golang/tools/blob/master/gopls/doc/workspace.md for information on setting up multi-module workspaces using go.work files

loumzy, try go env -w GO111MODULE=on and reopen vscode.

yokuse commented 1 year ago

For all those still facing the Broken import, this answer expands on @findleyr comment about single modules.

  1. Are you opening a parent directory that has multiple go modules inside? As mentioned by @findleyr, u can use go work. Heres how to use it:

parent directory/ ├── go-microservice-with-module1/ ├── go-microservice-with-module2/

Then do this

  1. cd parent_directory
  2. go work init ./<folder_name> i.e. go work init ./go-microservice-with-module1
  3. go work use ./<folder_name> i.e. go work use ./go-microservice-with-module2

For those adding the vscode settings, i.e.

"experimentalWorkspaceModule": true, It is deprecated already as mentioned in the comments above, do use go work instead.

If you don't have a need for multi module workspaces, then just make sure you have 1 module per vs-code session, this should solve the issue.

Source: https://go.dev/doc/tutorial/workspaces

niconc commented 1 year ago

As all people spoke before me, the solution is to go to "parent" or any "root" directory contains the various directory projects with go.mod files, and issue this command:

go work use -r <the_directory_which_includes_all_other>

// go work use [-r] [dir] adds a use directive to the go.work file for dir, if it exists, and removes the use directory if the argument directory doesn’t exist. The -r flag examines subdirectories of dir recursively.

This will create a go.work file at that directory and by using the -r flag, it will recursively add every subfolder contains any module (with go.mod file present) to the go.work file, and exclude anything that's not having go.mod file.

I'm always working by opening a workspace using the old GOPATH directory structure (I'M USING ONLY THE DIRECTORY STRUCTURE. NOT THE GOPATH system, because I'm using modules), and in GOPATH/src/github.com everything was red before. This was because the gopls server only worked for the standard go library, and ignored everything was under GOPATH/pkg/mod . The projects worked but without help of gopls I had no information, intellisense, nothing!

The solution was found at the bottom of this link, at the go.dev/tutorials, here: https://go.dev/doc/tutorial/workspaces

Now everything went green! All OK! Hallelujah!!

svadr commented 7 months ago

I have the same problem. I found myself installed go and go nightly, delete anyone of it can solve the problem for me. go version 1.19.3

WeChatb37acd4ba9b70acceee35afa8dd06a20

"experimentalWorkspaceModule": true,

I'm geeting this warning/error: Invalid settings: experimentalWorkspaceModule has been replaced by go workspaces, and will be removed in a future version of gopls (https://go.dev/issue/55331) -- see https://github.com/golang/tools/blob/master/gopls/doc/workspace.md for information on setting up multi-module workspaces using go.work files

This worked for me, thank you!

stym06 commented 5 months ago

Restarting the language server worked for me https://github.com/golang/vscode-go/blob/master/docs/troubleshooting.md#restart-gopls