-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/go: add -C flag to change directory #50332
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
-C
flag to go
- change dir-C
flag to go
- change dir
I imagine this would be a flag to follow As for the logic itself: would it effectively replace the use of the current directory everywhere? For example, if I do I think I'm neutral on this proposal. On one hand, it seems like a harmless addition, and there is indeed precedent in other popular tools. On the other hand, it's hard to judge how necessary the flag really is, and it does seem like a pretty far-reaching flag compared to the other common flags we have right now. What particular use case are you thinking about? What is the particular problem with pushd/popd, the verbosity of it? |
I imagine it would be a very straightforward implementation. If the flag
is set, chdir() and carry on as before. No further semantic changes.
I ack the notion that the bare go command doesn't take flags, but I would
argue this sort of flag makes the most sense as a universal modifier. If
not, I would then argue that most sub-commands should have it. Happy to
audit those individually.
The main use case is to avoid the constant refrain of `pushd path/to/foo
/dev/null; to do whatever; popd >/dev/null` in scripts (even worse if I
need the exit code from go) and to make it more like the other tools.
It's totally a convenience and convention thing, but also pretty trivial to
implement (I hope).
…
Message ID: ***@***.***>
|
You could use a subshell, which is a lot shorter than $ (cd foo; pwd; true); echo $?
foo
0
$ (cd foo; pwd; false); echo $?
foo
1 I most commonly do this with my Go checkout to run |
This proposal has been added to the active column of the proposals project |
If
or worse
Does that x.exe get written to bar/x.exe? What behavior will people expect? It sounds like the suggestion here is that this command writes to bar/x.exe. |
I think bar/x.exe is consistent with other tools' semantics.
…On Wed, Jan 19, 2022 at 10:18 AM Russ Cox ***@***.***> wrote:
If -C is really "do a chdir before anything else", then that's easy. But
what about
go build -C bar -o x.exe
or worse
go build -o x.exe -C bar
Does that x.exe get written to bar/x.exe? What behavior will people expect?
It sounds like the suggestion here is that this command writes to
bar/x.exe.
Will that be too confusing for people?
/cc @matloob <https://github.com/matloob> @bcmills
<https://github.com/bcmills>
—
Reply to this email directly, view it on GitHub
<#50332 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABKWAVFJWLCREK54BOTWD6TUW357PANCNFSM5KVXFUMA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Being nitpicky here, but I assume we'd want to make it equivalent to |
This would be a nice, small change that is consistent with other tools like I worked on some build automation last year on a previous team that had most of their code in a monorepo. All of the build instructions and scripts were littered with I've seen some Makefiles in Go projects that followed patterns similar to this that would have also benefitted: GOCMD=go
GOBUILD=$(GOCMD) build
BINARY_NAME=app
build:
cd dir; $(GOBUILD) -o $(BINARY_NAME) -v The directory traversal was typically handled with something like that or |
Any objections to adding this? |
I tried to think of whether any existing cmd/go behaviors might intersect poorly with this, and did not spot a glaring serious conflict. For example, go build takes -modfile that allows selecting an alternative go.mod file, which is okay to do at the same time as using another working directory. Presumably, it'd also be possible to set -C via cmd/go's Edit: Also, |
I'd be interested in working on this if there are no objections to the proposal and no one else has picked it up yet. |
Based on the discussion above, this proposal seems like a likely accept. |
I also reached for that solution in a GitHub action and then found that it failed on Windows: it would accept the syntax, but then ran the following commands in the directory of the subshell. So +1 from me for the proposal. |
No change in consensus, so accepted. 🎉 |
-C
flag to go
- change dir-C
flag to go
- change dir
Change https://go.dev/cl/421436 mentions this issue: |
-C
flag to go
- change dir
YAY! Thanks all. |
The -C flag is like tar -C or make -C: it changes to the named directory early in command startup, before anything else happens. Fixes golang#50332. Change-Id: I8e4546f69044cb3a028d4d26dfba482b08cb845d Reviewed-on: https://go-review.googlesource.com/c/go/+/421436 Reviewed-by: Bryan Mills <bcmills@google.com> Auto-Submit: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Russ Cox <rsc@golang.org>
Change https://go.dev/cl/449075 mentions this issue: |
Updates #41696. Updates #50332. Updates #41583. Change-Id: I99e96a2996f14da262570a5cb5273dcdce45df2b Reviewed-on: https://go-review.googlesource.com/c/go/+/449075 Reviewed-by: Russ Cox <rsc@golang.org> Run-TryBot: Bryan Mills <bcmills@google.com> Reviewed-by: Michael Matloob <matloob@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Bryan Mills <bcmills@google.com>
I'm not sure if this rises to a proposal or not.
Many tools (e.g. make, git) have a universal
-C <dir>
flag which lets you do things in other directories without pushd/popd silliness. Given how go uses CWD for important things like modules, it would be nice to have something like this in go. For examplego -C path/to/dir list
gives me the package-path. Otherwise it's things likepushd path/to/dir >/dev/null; go list; popd >/dev/null
.It's really just a convenience thing. Has it already been discussed? I couldn't find it.
The text was updated successfully, but these errors were encountered: