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

Enhance go.mod resolution to look at all parent folders between a .go file and project root. #2249

Closed
james-lawrence opened this issue May 20, 2022 · 6 comments
Labels
Duplicate Make it obvious when issues are closed as duplicate gopls/workspace workspace setup related issues (UX of multi-modules, nested modules, GOPATH handling)
Milestone

Comments

@james-lawrence
Copy link

Is your feature request related to a problem? Please describe.
Yes. I'm always frustrated when I'm working on a .go file and because I open vscode in the directory above the directory the go.mod file exists. none of the tooling works because the plugin thinks its not in a go project.

Describe the solution you'd like
the plugin should resolve the go.mod file by checking the parent files of the file being operated on.

Describe alternatives you've considered
none really, I've solved the same issue for a tool called hover resolving the pubspec file.

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

hyangah commented May 23, 2022

I thought the go language server works if there is only one single subdirectory in the workspace with go.mod. Maybe that's no longer true? (cc @findleyr )

@james-lawrence If you are using go1.18+, you may be interested in configuring go.work.
https://go.dev/blog/get-familiar-with-workspaces

I proposed #2132 to automate handling of go.work but it still needs investigation and may take a while.

@hyangah hyangah 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 May 23, 2022
@james-lawrence
Copy link
Author

@hyangah go.work solves a different problem. this is about resolving go.mod correctly for any given go file open by the editor.

@findleyr
Copy link
Contributor

@hyangah yes gopls will narrow the workspace to a single module if it exists, but if two modules exist in subdirectories then it doesn't know what to do, because it doesn't know how to construct the build list. go.work is a solution to that problem.

@james-lawrence there is a long history here, and this is not as trivial as it may seem at first.

Suppose I open moduleA/a.go and moduleB/b.go in my workspace. Now suppose both moduleA and moduleB depend on a (different!) version of moduleC. If you jump to definition from moduleA->moduleC and moduleB->moduleC, would you expect to go to the same place, or different places?

If you answer 'the same place', then gopls has (for now) a way to do what you want: "experimentalWorkspaceModule": true. This creates a fake parent module requiring both moduleA and moduleB, for the purposes of constructing the build list. However, due to not being integrated with the go command, this mode can be surprising for users, as error messages inevitably reference this fake module and users cannot replicate the precise behavior of gopls from the command line. We decided it's much better to have a unified solution, which is go.work, and are planning to start deprecating "experimentalWorkspaceModule" mode in #52897.

So the solution is to create a go.work file using the modules in your subdirectories. You can use all the modules by running go work usr -r .. If you have reasons why this won't work for you, or suggestions for improving this workflow, we'd love to hear them.

@hyangah hyangah added the gopls/workspace workspace setup related issues (UX of multi-modules, nested modules, GOPATH handling) label Jun 7, 2022
@james-lawrence
Copy link
Author

@findleyr current open buffer is the correct solution here. vscode gives plugins enough information to find the correct go.mod file to use based on the current file. I understand it may be difficult for this project; but the underlying solution to the problem is fairly trivial.

@findleyr
Copy link
Contributor

@james-lawrence agreed. We want to change this behavior.

@hyangah
Copy link
Contributor

hyangah commented Sep 8, 2023

The future version of gopls will offer the zero-config gopls workspace feature.
golang/go#57979

That implements various heuristics to infer the right workspace folder. One of the scenarios it's targeting is exactly this.
Closing in favor of golang/go#57979.

@hyangah hyangah closed this as not planned Won't fix, can't repro, duplicate, stale Sep 8, 2023
@hyangah hyangah added Duplicate Make it obvious when issues are closed as duplicate and removed WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Sep 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate Make it obvious when issues are closed as duplicate gopls/workspace workspace setup related issues (UX of multi-modules, nested modules, GOPATH handling)
Projects
None yet
Development

No branches or pull requests

4 participants