Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
cmd/go: 'fmt x.go' fails with `cannot find main module` #27099
What version of Go are you using (
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.
Thanks for the answer.
This sounds like a good solution to me. Since a client uses a path to file, there is no need to resolve
I'm not sure what you mean by this, could you elaborate, please? The modules are new themselves, as well as
As far as I remember we migrated to
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.
As Russ pointed out
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.