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

cmd/go: crash during loading in dirAndRoot (vendor-related) #65415

Open
adonovan opened this issue Jan 31, 2024 · 6 comments
Open

cmd/go: crash during loading in dirAndRoot (vendor-related) #65415

adonovan opened this issue Jan 31, 2024 · 6 comments
Labels
GoCommand cmd/go NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@adonovan
Copy link
Member

adonovan commented Jan 31, 2024

@samthanawalla encountered this crash while using the web-based VS Code with a Google VM as the backend.

image

The error indicates a crash propagated via gopls and go/packages up from the go command, specifically the dirAndRoot function:

// dirAndRoot returns the source directory and workspace root
// for the package p, guaranteeing that root is a path prefix of dir.
func dirAndRoot(path string, dir, root string) (string, string) {
	origDir, origRoot := dir, root
	dir = filepath.Clean(dir)
	root = filepath.Join(root, "src")
	if !str.HasFilePathPrefix(dir, root) || path != "command-line-arguments" && filepath.Join(root, path) != dir {
		// Look for symlinks before reporting error.
		dir = expandPath(dir)
		root = expandPath(root)
	}

	if !str.HasFilePathPrefix(dir, root) || len(dir) <= len(root) || dir[len(root)] != filepath.Separator || path != "command-line-arguments" && !build.IsLocalImport(path) && filepath.Join(root, path) != dir {
		debug.PrintStack()
		base.Fatalf("unexpected directory layout:\n"+
			"	import path: %s\n"+
			"	root: %s\n"+
			"	dir: %s\n"+
			"	expand root: %s\n"+
			"	expand dir: %s\n"+
			"	separator: %s",
			path,
			filepath.Join(origRoot, "src"),
			filepath.Clean(origDir),
			origRoot,
			origDir,
			string(filepath.Separator))
	}

	return dir, root
}

In addition the VS Code UI showed this truncated error:

Failed to run "go env env, -json, GOMOD": Command failed: /usr/lib/go...

Sorry there's not much to go on yet. I wonder whether the remote machine has as GOPACKAGESDRIVER env var (or $PATH executable).

@adonovan
Copy link
Member Author

adonovan commented Jan 31, 2024

@hyangah The "failed to run go env" part appears to be from logic in vscode-go (in src/goModules.ts).

@adonovan
Copy link
Member Author

adonovan commented Jan 31, 2024

We managed to get the complete stack and the actual values in the assertion failure:

Error loading workspace: packages.Load error: err: exit status 1: stderr:

goroutine 3373 [running]:
runtime/debug.Stack()
	runtime/debug/stack.go:24 +0x5e
runtime/debug.PrintStack()
	runtime/debug/stack.go:16 +0x13
cmd/go/internal/load.dirAndRoot({0xc00058ace1, 0x17}, {0xc000c31630, 0x4e}, {0xc000026187, 0x27})
	cmd/go/internal/load/pkg.go:1203 +0x4ad
cmd/go/internal/load.vendoredImportPath({0xc0005bb9c1, 0x4}, {0xc00058ace1, 0x17}, {0xc000c31630?, 0xc000224548?}, {0xc000026187?, 0x48?})
	cmd/go/internal/load/pkg.go:1231 +0x85
cmd/go/internal/load.resolveImportPath({0xc0005bb9c1, 0x4}, {0xc00058ace1, 0x17}, {0xc000c31630, 0x4e}, {0xc000026187, 0x27}, 0x9b?)
	cmd/go/internal/load/pkg.go:1183 +0x9e
cmd/go/internal/load.loadPackageData.func1()
	cmd/go/internal/load/pkg.go:905 +0x1ac
cmd/go/internal/par.(*Cache[...]).Do(0xbb0de0, {{0xc0005bb9c1, 0x4}, {0xc00058ace1, 0x17}, {0xc000c31630, 0x4e}, {0xc000026187, 0x27}, 0x0, ...}, ...)
	cmd/go/internal/par/work.go:160 +0x159
cmd/go/internal/load.loadPackageData({0xbab970, 0xf96260}, {0xc0005bb9c1, 0x4}, {0xc00058ace1, 0x17}, {0xc000c31630, 0x4e}, {0xc000026187, 0x27}, ...)
	cmd/go/internal/load/pkg.go:893 +0x38e
cmd/go/internal/load.(*preload).preloadImports.func1({0xc0005bb9c1?, 0x8?})
	cmd/go/internal/load/pkg.go:1115 +0x8b
created by cmd/go/internal/load.(*preload).preloadImports in goroutine 3883
	cmd/go/internal/load/pkg.go:1114 +0x265
unexpected directory layout:

import path: golang.org/x/mod/semver
root: /usr/local/google/home/samthanawalla/go/src
dir: /usr/local/google/home/samthanawalla/go/src/cmd/vendor/golang.org/x/mod/semver
expand root: /usr/local/google/home/samthanawalla/go
expand dir: /usr/local/google/home/samthanawalla/go/src/cmd/vendor/golang.org/x/mod/semver
separator: / 

it seems like the immediate problem is the "root + path = dir" invariant is violated because of a missing "vendor" segment.

cc @matloob

@adonovan adonovan changed the title cmd/go: crash during loading in dirAndRoot cmd/go: crash during loading in dirAndRoot (vendor-related) Jan 31, 2024
@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 31, 2024
@mknyszek mknyszek added this to the Backlog milestone Jan 31, 2024
@mknyszek mknyszek added the GoCommand cmd/go label Jan 31, 2024
@bcmills
Copy link
Contributor

bcmills commented Feb 1, 2024

I'm surprised that vendoredImportPath is being called at all: that's specific to GOPATH mode, which shouldn't be in use. 🤔

https://cs.opensource.google/go/go/+/master:src/cmd/go/internal/load/pkg.go;l=1177-1183;drc=b32ec6c961182e5bca6ae8420d6aa1204d7d2821

@matloob
Copy link
Contributor

matloob commented Feb 5, 2024

Yeah I think we should figure out why vendoredImportPath is being called. Sam, would you have time to do some debugging of this?

@samthanawalla
Copy link
Contributor

Sure. I can try to debug the issue on my machine.

@samthanawalla
Copy link
Contributor

samthanawalla commented Feb 7, 2024

We debugged this to be an issue with conflicting Go versions. I had one default installation of Go and one that I built from the github Repo.

This is what fixed it for me:

  1. mv $HOME/go $HOME/w/go/
  2. Copy the output of echo $PATH
  3. Add this line to $HOME/.bashrc: export PATH=$HOME/go/bin:$HOME/w/go/bin:<what you copied from echo $PATH>
  4. unset GOROOT and unset GOPATH.
  5. Reload vscode

Optionally: go env -w GOMODCACHE=$HOME/w/go/pkg/mod

This fixed this issue and allowed gopls to work perfectly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GoCommand cmd/go NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants