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: it is inconvenient and unfriendly to require a simple program must have a go.mod file #36513

Open
go101 opened this issue Jan 12, 2020 · 6 comments
Labels
Milestone

Comments

@go101
Copy link

@go101 go101 commented Jan 12, 2020

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

$ go version
go version go1.14beta1 linux/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

  • Create a simple hello world project with only one main.go file.
  • Set GO111MODULE=on
  • Run go run . in the project folder.

What did you expect to see?

Run okay.

What did you see instead?

go: cannot find main module; see 'go help modules'

or for a git repository

go: cannot find main module, but found .git/config in /path/to/projects/helloworld
	to create a module there, run:
	go mod init
@mvdan

This comment has been minimized.

Copy link
Member

@mvdan mvdan commented Jan 12, 2020

Why are you setting GO111MODULE=on? A module requires a go.mod file, so it seems like you have conflicting objectives here.

In any case, it seems like you disagree with #32027. That discussion happened a while ago, though.

@TapirLiu

This comment has been minimized.

Copy link
Contributor

@TapirLiu TapirLiu commented Jan 12, 2020

Some people might have set it on globally before for some other reasons.
I just found this issue from a PR of one my project.

@TapirLiu

This comment has been minimized.

Copy link
Contributor

@TapirLiu TapirLiu commented Jan 12, 2020

The current design also causes inconveniences in writing some Go tutorials with a simple Go file (and might have caused for some old tutorials). They must remind readers to create a go.mod file if GO111MODULE=on is set.

If some new gophers have set GO111MODULE=on before reading such old tutorials, the result might frustrate them at least for awhile.

@mvdan

This comment has been minimized.

Copy link
Member

@mvdan mvdan commented Jan 12, 2020

I think it depends on what exactly the user is doing. If they need third party dependencies, requiring a go.mod sounds correct.

If they don't need third party dependencies, i.e. they use the standard library alone, I agree that a go.mod is largely not needed right now. You could work around that by asking the user to set GO111MODULE=off.

But it would still be a good idea to use a go.mod file in the long term, even if it's just for simple examples. It will be required to use newer language features, for example. See https://golang.org/doc/go1.13#language.

@TapirLiu

This comment has been minimized.

Copy link
Contributor

@TapirLiu TapirLiu commented Jan 12, 2020

Yes, the go.mod file have some benefits, but not for new gophers to learn Go.

At least, the error message should not depend on whether or not there is a .git subfolder. The help message should also be printed for both cases. And even for the git repository case, the printed help message is also too unfriendly for new gophers. It is better to provide an example, such as go mod init my.module.

@jayconrod

This comment has been minimized.

Copy link
Contributor

@jayconrod jayconrod commented Jan 13, 2020

I'd recommend against setting GO111MODULE=on globally. That was useful for working on modules checked out into GOPATH in Go 1.12 or earlier, but since GO111MODULE=auto was changed in 1.13, I don't think it makes sense now. Maybe in a few specific cases for tools, but not generally. Hopefully people who have it set know the implications, so I wouldn't expect that tutorials need to be written for them. If any documentation recommends new users set GO111MODULE=on, we should fix that.

This specific change in behavior was #32027. In short, if a user has GO111MODULE=on set explicitly, and there's no go.mod file in the current directory or in any parent, you can only build or import packages in the standard library or packages specified as .go files on the command line. So for example, this works:

$ cat >hello.go <<EOF
package main

import "fmt"

func main() {
  fmt.Println("hello")
}
EOF

$ go run hello.go
hello

Import paths in modules other than std and cmd won't work. That includes relative imports and patterns.

$ go run
go run: no go files listed
$ go run .
go: cannot find main module; see 'go help modules'
$ go list golang.org/x/tools/gopls
can't load package: cannot find module providing package golang.org/x/tools/gopls: working directory is not part of a module
$ go list ./...
go: cannot find main module; see 'go help modules'

The rationale for this change was that early on, people would set GO111MODULE=on, not create a go.mod file, then find that everything is very slow: we need to look up module paths and versions for every import on every command, since there's nowhere to write down dependencies.

I don't think this necessarily means go build . or go run . should not work, but fixing this would probably involve adding a significant special case, and it would be risky to do that this late in the cycle.

@bcmills @matloob WDYT? We might want to do this if/when GOPATH mode is deprecated, since GO111MODULE=on will be the only mode after that.

@jayconrod jayconrod added this to the Unplanned milestone Jan 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.