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: directory named go.mod defines new module but is otherwise ignored #30788

Closed
heschi opened this issue Mar 12, 2019 · 4 comments
Closed
Labels
FrozenDueToAge GoCommand cmd/go modules NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@heschi
Copy link
Contributor

heschi commented Mar 12, 2019

What version of Go are you using (go version)?

go version devel +ee6bec958d Fri Mar 1 18:35:31 2019 +0000 linux/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

I have no idea how I ended up with a go.mod directory, but I did. I discovered this in x/tools so that's where the repro is.

tools/imports (master)$ git reset --hard
HEAD is now at e65039ee internal/lsp: make sure completion items are never null
tools/imports (master)$ GO111MODULE=on go list .  
golang.org/x/tools/imports
tools/imports (master)$ mkdir go.mod
tools/imports (master)$ GO111MODULE=on go list .
go: finding golang.org/x/tools/imports latest
can't load package: package golang.org/x/tools/imports: unknown import path "golang.org/x/tools/imports": cannot find module providing package golang.org/x/tools/imports
tools/imports (master)$ GO111MODULE=on go mod tidy
go: finding golang.org/x/tools/imports latest
tools/imports (master)$ echo $?
0
tools/imports (master)$ echo 'module golang.org/x/tools' > ../go.mod
tools/imports (master*)$ GO111MODULE=on go mod tidy
tools/imports (master*)$ GO111MODULE=on go mod tidy                  
[snip]
tools/imports (master*)$ cat ../go.mod
module golang.org/x/tools

go 1.12

require golang.org/x/net v0.0.0-20190311183353-d8887717615a

The way it's ignored by tidy is strange; I imagine there are two code paths for finding go.mod and only one of them checks whether the file is a regular file. This is particularly confusing since Git won't show you an empty directory in git status.

@stamblerre stamblerre removed gopls Issues related to the Go language server, gopls. labels Mar 12, 2019
@bcmills
Copy link
Contributor

bcmills commented Mar 13, 2019

I imagine there are two code paths for finding go.mod and only one of them checks whether the file is a regular file.

Three, actually. And you're right that only one of them checks for a file vs. a directory:

if fi, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil && !fi.IsDir() {

if _, err := os.Stat(filepath.Join(path, "go.mod")); err == nil {

_, err := os.Stat(filepath.Join(d, "go.mod"))

@bcmills
Copy link
Contributor

bcmills commented Mar 13, 2019

Make that four. And two of them check.

go/src/go/build/build.go

Lines 1042 to 1043 in 5f40351

info, err := os.Stat(filepath.Join(abs, "go.mod"))
if err == nil && !info.IsDir() {

@bcmills
Copy link
Contributor

bcmills commented Mar 13, 2019

Five.

if _, err := os.Stat(filepath.Join(path, "go.mod")); err == nil {

@bcmills bcmills added the NeedsFix The path to resolution is known, but the work has not been done. label Mar 13, 2019
@bcmills bcmills added this to the Go1.13 milestone Mar 13, 2019
@gopherbot
Copy link

Change https://golang.org/cl/167393 mentions this issue: cmd: ignore the directory named go.mod

@golang golang locked and limited conversation to collaborators Mar 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge GoCommand cmd/go modules NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

4 participants