Hyang-Ah Hana Kim submitted this change.
src/util: avoid picking a module root as an inferred gopath
When go.inferGopath is set, the extension adds the workspace
root folder or a parent directory of an open file as an additional
GOPATH element. This feature was useful when GOPATH was the way
to define the workspace. With Go Modules, however, this feature
is no longer useful and can confuse go commands and gopls.
src/goModules.ts getModFolderPath attempts to disable go.inferGopath
and warn users if the module is enabled. However, the code relies
on successful run of `go env GOMOD`. Of course, that will fail
in Go Module mode since the extension runs the go command with the
inferred GOPATH and `go env` command will not like the go.mod file
in the GOPATH.
This change takes a different approach - if a directory contains
go.mod file so it is a module root, that directory cannot be used as
an inferred GOPATH.
Manually tested by setting go.inferGopath = true and running
"Go: Current GOPATH" command for each case:
case 1: directory that doesn't have go.mod --> Current GOPATH should
have the workspace root.
case 2: directory that has go.mod --> Current GOPATH shouldn't have
the workspace root.
case 3: directory whose parent directory has /src directory but no
go.mod file --> Current GOPATH should have the parent directory.
case 4: directory whose parent directory has /src directory and go.mod
file --> Current GOPATH shouldn't have the parent directory.
Also, remove the broken prompting & disabling inferGopath logic.
Fixes golang/vscode-go#1663
Change-Id: Ie25574748710065201554b1cbbb2e57ef3384df3
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/459859
Run-TryBot: Hyang-Ah Hana Kim <hya...@gmail.com>
Reviewed-by: Jamal Carvalho <ja...@golang.org>
TryBot-Result: kokoro <noreply...@google.com>
---
M src/goModules.ts
M src/util.ts
2 files changed, 59 insertions(+), 8 deletions(-)
diff --git a/src/goModules.ts b/src/goModules.ts
index 6305caf..a4ea5d0 100644
--- a/src/goModules.ts
+++ b/src/goModules.ts
@@ -78,14 +78,6 @@
if (goModEnvResult) {
goModEnvResult = path.dirname(goModEnvResult);
const goConfig = getGoConfig(fileuri);
-
- if (goConfig['inferGopath'] === true && !fileuri?.path.includes('/vendor/')) {
- goConfig.update('inferGopath', false, vscode.ConfigurationTarget.WorkspaceFolder);
- vscode.window.showInformationMessage(
- 'The "inferGopath" setting is disabled for this workspace because Go modules are being used.'
- );
- }
-
if (goConfig['useLanguageServer'] === false && getFormatTool(goConfig) === 'goreturns') {
const promptFormatToolMsg =
'The goreturns tool does not support Go modules. Please update the "formatTool" setting to "goimports".';
diff --git a/src/util.ts b/src/util.ts
index 42bed93..f7d919e 100644
--- a/src/util.ts
+++ b/src/util.ts
@@ -530,6 +530,16 @@
// No op
}
}
+ if (inferredGopath) {
+ // inferred GOPATH must not have go.mod in it.
+ try {
+ if (fs.existsSync(path.join(inferredGopath, 'go.mod'))) {
+ inferredGopath = '';
+ }
+ } catch (e) {
+ // No op
+ }
+ }
if (inferredGopath && process.env['GOPATH'] && inferredGopath !== process.env['GOPATH']) {
inferredGopath += path.delimiter + process.env['GOPATH'];
}
To view, visit change 459859. To unsubscribe, or for help writing mail filters, visit settings.