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

go101 opened this issue Jan 12, 2020 · 7 comments


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?


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
Copy link

@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.

Copy link

@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.

Copy link

@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.

Copy link

@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

Copy link

@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.

Copy link

@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() {

$ go run hello.go

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
can't load package: cannot find module providing package 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
Copy link

@bcmills bcmills commented Jan 22, 2020

At the very least, we would need to decide what language version to use in module mode for a package in a directory outside of any module.

We would also need to determine the effective import path for such a package for debug info and build caching. (Perhaps the same import path that we generate in GOPATH mode today?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants