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: 'fmt x.go' fails with `cannot find main module` #27099

Open
zolotov opened this Issue Aug 20, 2018 · 11 comments

Comments

Projects
None yet
6 participants
@zolotov
Contributor

zolotov commented Aug 20, 2018

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

go1.11rc1 windows/amd64

What operating system and processor architecture are you using (go env)?

Windows, Linux

What did you do?

  1. create a main file, e.g. at C:\gopath\src\testProject\main.go with a content package main; func main() {}
  2. in terminal:
set GO111MODULE="on"
cd C:\gopath\src\testProject\
go fmt main.go

on linux:

➜  echo 'package main; func main() {}' > main.go
➜ GO111MODULE=on go fmt main.go 
go: cannot find main module; see 'go help modules'

What did you expect to see?

Formatted main.go, since formatting does not rely on a compilation, an absence of main module doesn't seem like a good reason to stop formatting.

What did you see instead?

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

This comment has been minimized.

Contributor

rsc commented Aug 21, 2018

Sorry but formatting does rely on compilation. In general 'go fmt pkg' must figure out what files are in the package. We could add a special case for fmt not to chase down deps and then a different special case for 'go fmt x.go' not to require module initialization, and maybe we should, but this problem is not really new in modules.

In any event, I think you are really looking for gofmt -w.

@rsc rsc added this to the Go1.12 milestone Aug 21, 2018

@rsc rsc added the modules label Aug 21, 2018

@zolotov

This comment has been minimized.

Contributor

zolotov commented Aug 21, 2018

Hi @rsc,

Thanks for the answer.

We could add a special case for fmt not to chase down deps and then a different special case for 'go fmt x.go' not to require module initialization

This sounds like a good solution to me. Since a client uses a path to file, there is no need to resolve importPath to a package and retrieve files of that packages, so no reason to require module initialization.

this problem is not really new in modules

I'm not sure what you mean by this, could you elaborate, please? The modules are new themselves, as well as GO111MODULE variable which is not on by default.

In any event, I think you are really looking for gofmt -w.

As far as I remember we migrated to go fmt from gofmt because not all OS-specific distributions (like deb or rpm packages) contained gofmt binary nearby the bin/go. At that time we already had a mess regarding looking for go binary for different go distributions (https://github.com/go-lang-plugin-org/go-lang-idea-plugin/blob/master/src/com/goide/sdk/GoSdkService.java#L106), and adding more logic regarding looking for gofmt for a given GOROOT seemed not reasonable. It would be great to have a single entry point to all go features.

@agnivade

This comment has been minimized.

Member

agnivade commented Aug 21, 2018

I'm not sure what you mean by this, could you elaborate, please?

He means this was always the case. And not something due to modules.

@zolotov

This comment has been minimized.

Contributor

zolotov commented Aug 21, 2018

He means this was always the case. And not something due to modules.

Still not clear. Requiring module initialization is not because of modules? But there is no module without modules, and go fmt works as expected without modules enabled.

@agnivade

This comment has been minimized.

Member

agnivade commented Aug 21, 2018

Not the module initialization part, the requirement of compilation part (like syntax errors and such). Now with modules, gofmt requires module initialization. Hope that is clear (And what was intended by Russ).

@zolotov

This comment has been minimized.

Contributor

zolotov commented Aug 21, 2018

Not the module initialization part, the requirement of compilation part (like syntax errors and such)

Thanks, still I'm pretty sure it was related to a module initialization part and would like to know the details.

As I understood from the initial answer, the compilation is required for resolving package and files in it, not for syntax checking. Building AST for formatting (and syntax checking as part of it) is not a compilation, it's a parsing a particular file isolated from other files in a package (and from module's dependencies), and it doesn't lead to the problem described here.

gofmt requires module initialization

As Russ pointed out gofmt does not require module initialization, go fmt does.

This is the point of the issue, I don't see any reason to require module initialization for formatting a file, not a package. And I think this is a new thing since modules are new.

@agnivade

This comment has been minimized.

Member

agnivade commented Aug 21, 2018

My apologies, I did not see it was go fmt.

@josharian

This comment has been minimized.

Contributor

josharian commented Aug 23, 2018

As far as I remember we migrated to go fmt from gofmt because not all OS-specific distributions (like deb or rpm packages) contained gofmt binary nearby the bin/go.

Would fixing #27166 help you?

@zolotov

This comment has been minimized.

Contributor

zolotov commented Aug 23, 2018

@josharian I think it will. Thanks!

@rsc

This comment has been minimized.

Contributor

rsc commented Oct 25, 2018

It is OK to change go fmt x.go y.go z.go to see that the file list is only Go files and just apply the formatting to them without all the usual package loading.

@bcmills bcmills added the NeedsFix label Oct 25, 2018

@bcmills bcmills changed the title from cmd/go: fmt fails with `cannot find main module` to cmd/go: 'fmt x.go' fails with `cannot find main module` Nov 15, 2018

@gopherbot

This comment has been minimized.

gopherbot commented Dec 10, 2018

Change https://golang.org/cl/153459 mentions this issue: cmd/go: fix 'go test' and 'go fmt' with files outside a module

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment