-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
x/tools/cmd/gopls: should support modules in rootURI subdirectories #30841
Comments
I found this, too. go list (and therefore go/packages) only seems to work properly when its pwd is inside the module being resolved. ~/issue30841 $ tree
.
└── a
├── b
│ ├── go.mod
│ └── main.go
├── c
│ └── main.go
├── go.mod
└── main.go
3 directories, 5 files Works:
Does not work (pwd is outside module):
I found this diff seems to load everything properly in vscode, but I'm not sure if it's the correct fix, since some tests fail: diff --git a/internal/lsp/cache/check.go b/internal/lsp/cache/check.go
index 2082b47d..7a912bd4 100644
--- a/internal/lsp/cache/check.go
+++ b/internal/lsp/cache/check.go
@@ -99,6 +99,7 @@ func (v *View) checkMetadata(ctx context.Context, f *File) ([]packages.Error, er
if v.reparseImports(ctx, f, filename) {
cfg := v.Config
cfg.Mode = packages.LoadImports
+ cfg.Dir = filepath.Dir(filename)
pkgs, err := packages.Load(&cfg, fmt.Sprintf("file=%s", filename))
if len(pkgs) == 0 {
if err == nil { |
@broady : I applied your change locally and it makes the language server work properly for me at least |
@broady thx~ |
This issue is a duplicate of #29174, and as the discussion there suggests, @broady's fix will not actually work. We are trying to figure out a good approach for fixing this problem, but it's difficult because LSP sends a |
Duplicate of #29174 |
Encounter this problem too, based on @broady's fix, I use the following approach for the time being. Looking forward to the official fix! originDir := cfg.Dir
fdir := filepath.Dir(f.filename)
if !strings.HasPrefix(fdir, filepath.Join(os.Getenv("GOPATH"), "pkg")) {
cfg.Dir = fdir
}
pkgs, err := packages.Load(&cfg, fmt.Sprintf("file=%s", f.filename))
cfg.Dir = originDir |
…folders. Since the `go list` command, which is a necessary command to resolve the packages, is running at the rootURI folder, we have to run server at rootURI too, see golang/go#30841.
The official fix is already in, we support multiple workspace folders now. |
|
No, there is no automatic support, just add the directories to your workspace by hand. |
Thanks, @ianthehat! You are right, the editor should provide the whole picture of the project, in contrast, the server should focus on the functionality. |
@ianthehat, is there anything remaining to be done for this issue? |
@ianthehat : how should editors implement the necessary support ? Right now this is the most annoying issue with the language server, with no specific editor support for this, having to manually open the root directory of each module does not make for a great user experience at all. |
The last piece for gopls of this is tracked in #31635 and fixed in https://go-review.googlesource.com/c/tools/+/175477 I don't want to dictate the right approach to editors, but I would suggest adding a workspace folder for all go.mod files seen at the earliest opportunity would cover the 99% case. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I am using vscode to edit a project consisting of several subdirectories, one of them being the root of my go module.
What did you expect to see?
The root directory /home/vince/dev is opened in vscode, the go module is at /home/vince/dev/src/go.mod. When opening src/admin/file.go I'd like to have working completion / hover / ...
What did you see instead?
gopls does not work: the newColumnMap function returns an error "no file information for file:///home/vince/dev/src/admin/file.go"
(tok := f.GetToken(ctx) returns nil)
Manually updating the packages.Config.Dir path in server.go (line 108) works around the problem (Dir: rootPath + "/src") but of course this is not a proper fix
Should gopls try to find the appropriate go module for each given file by itself ? Or should the vscode-go plugin be responsible for it ? (In this case this bug should be reassigned to https://github.com/Microsoft/vscode-go)
The text was updated successfully, but these errors were encountered: