-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: support a -C flag value to not use any current directory #56868
Comments
One existing alternative would be There's also |
Apparently some people do build at root |
I guess I don't have anything against people building as the root/superuser in the root directory, but I definitely don't want to be doing that :) I'm not particularly worried about taking |
This proposal has been added to the active column of the proposals project |
-C=off is ambiguous but it doesn't seem likely to arise in practice, and it's consistent with most of our other settings that can be disabled (and you can use -C=./off to work around it). We should probably just do that. An alternative would be -C= (empty string) but that looks like and might actually be a bug. |
Based on the discussion above, this proposal seems like a likely accept. |
No change in consensus, so accepted. 🎉 |
cmd/go
uses the current directory for a number of things:.
or./foo
However, in some cases, we do not need any of those. For example:
go env -json GOROOT GOCACHE GOPROXY
go list -f={{context.ReleaseTags}} runtime
go list -json runtime
On one hand, finding the current module and workspace for these three commands is wasteful. On the other, it can be actively harmful. For example, if the current directory or any of its parents has a
go.mod
with broken syntax, then any of the three commands above may fail. In our fork ofcmd/go/script_test.go
, we had to make calls like these inside a temporary directory to work around the problem: rogpeppe/go-internal#188Using a temporary directory is also far from ideal. It still walks the parent directories, which is still wasteful. And it could still break in some (hopefully rare) edge cases, like if
/tmp/go.mod
existed and had broken syntax.One possible solution would be to manually turn off the search for "current module" and "current workspace". For the latter, it seems that we can use
GOWORK=off
. For the former, I don't seem to be able to find an "off" setting. There'sGO111MODULE=off
, but I don't really want GOPATH mode either, and I believe this mode will disappear at some point anyway.Moreover, turning off the search for these two files may be enough today, but in the future cmd/go may use the current directory to look for more files, just like
go.work
was introduced recently. So I really want a general option to mean "do not use the current directory to alter your behavior in any way".Luckily, in #50332 we gained a global "chdir" option. I propose that it gain a special value,
-C=off
, to mean just that. The process would still have a current directory, and it would still make direct accesses to the filesystem (e.g. to look insideGOROOT
or to read theGOENV
file), but any major feature relying on the current directory would be disabled or fail. For example:go list .
orgo list ./foo
, would failI realise that this is likely a complex set of changes for the innards of
cmd/go
, as the current directory is likely used in many places. I also realise that it likely does not make sense to use some commands likego build -C=off
. So perhaps we could start by only supporting this mode ingo env
andgo list
: two commands often used by Go tools to get the information they need.cc @myitcv @rogpeppe
The text was updated successfully, but these errors were encountered: