-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: escape quoted strings in GOFLAGS #26849
Comments
@bcmills if you want to support cmd.exe users on Windows, you should consider how quoting works in cmd.exe. For example, this http://daviddeley.com/autohotkey/parameters/parameters.htm#WINCMDRULES shows cmd.exe rules. Alex |
Sorry, but no. I looked at this when I added GOFLAGS, and we don't do any special quoting for the other environment variables ( This means you cant use GOFLAGS for your example. |
My usage of I think Respectfully, I would request that this be reopened and reconsidered, especially since the primary use case where this feature got me excited (building the same binary for a load of different architectures in a row) aren't actually helped by this feature today. ❤️ 😅 (I commented on the CL and was redirected here. ❤️) |
@rsc I'm not sure I understand the argument for not permitting quoting in the environment variable given the special support we have for quoting in -gcflags and friends. It seems to me that there are clear uses for passing multiple options to all builds, such as the |
Leaving for Go 1.13. GOFLAGS is relatively new and the workaround is to use the command line, like you had to do before Go 1.11. |
In case we do loosen this constraint on At the moment the last
Given my use-case, this is relatively easy to work around by parsing the space-separated But if we allow escape quoted strings in I suspect allowing multiple instances of |
Also note that the per-package flags like Not saying that I disagree with allowing multiple |
Re-reading what I wrote in #26849 (comment) I realise it wasn't that clear. At the moment, because
it can't contain a multi-value
Add to that, Hence at the moment, it is not possible to specify a multi-value For my specific case there are at least two possible non-mutually exclusive solutions:
It is for this second option that I can create a separate issue if required. |
Another alternative that would only work for |
Change https://golang.org/cl/173438 mentions this issue: |
Funnily enough, I thought that was how |
Using commas makes it possible to put multiple tags into GOFLAGS. The space-separated form is still recognized and will be maintained. Alleviates #26849 somewhat. Fixes #18800 (again). Change-Id: I6f4cf28ea31e53e21ccbdad6ef1a0aee63b007d7 Reviewed-on: https://go-review.googlesource.com/c/go/+/173438 Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> Reviewed-by: Ian Cottrell <[email protected]>
* Stub out MP4 loader for production builds. * Change alohacam `Makefile` to use a `buildcmd` function instead of a constant `BUILDCMD`. This is necessary because we want to specify *two* build tags (`production` and `v4l2`), but `GOFLAGS` does not allow setting multiple tags. (This should be fixed in 1.13, which is imminent. See golang/go#26849.)
* Stub out MP4 loader for production builds. * Change alohacam `Makefile` to use a `buildcmd` function instead of a constant `BUILDCMD`. This is necessary because we want to specify *two* build tags (`production` and `v4l2`), but `GOFLAGS` does not allow setting multiple tags. (This should be fixed in 1.13, which is imminent. See golang/go#26849.)
@stapelberg I think you raise a valid point, and I've thought about it as well, but I think that should be filed as a separate issue. That is, when @myitcv already mentioned raising this issue separately in #26849 (comment) for |
I didn't get much of a response on #26849 (comment) aside form a couple of reactions, so I didn't feel like there was enough consensus to try something out for 1.14. This 1.15 cycle is being weird, but I'd like to give this a try in the two remaining weeks. @bcmills do you still prefer adding simple quoting support to @ianthehat do you still prefer adding support for flags like I used to prefer the second option, but I'm starting to think that simple quoting support is a better long-term fix. Commas are really only a short-term fix, don't support nesting of list parameters which also use commas, and IMO make complex list flags like So, I now propose that we add support for two quoting syntaxes on all platforms equally:
These should be fairly easy to implement. For example, the second would reuse https://golang.org/pkg/strconv/#Unquote for unquoting. If this generally seems like a good idea, I can send a CL this coming week and we can give it a try during the freeze. |
Filed #38522. Please amend as you see fit. |
This commit removes the DWARF debugging information[1] Decided not to try to also turn off generation of the Go symbol table using the `-s` flag, as: 1. the GOFLAGS functionality currently only allows 1 ldflag to be set[2] (NB this may be fixed in Go 1.15[3]) 2. The improvements to the binary size in Go 1.15 [4] mean that the `-s` flag will no longer really do anything[5] Will also try shrinking the binary size further using upx[6] in a separate pull request. [1] https://stackoverflow.com/a/22276273 [2] golang/go#38522 [3] golang/go#26849 (comment) [4] See #64 [5] https://twitter.com/bradfitz/status/1256989624590712834 [6] https://upx.github.io/
This commit removes the DWARF debugging information[1], resulting in the Docker image size shrinking from 4.76MB to 2.88MB. Decided not to try to also turn off generation of the Go symbol table using the `-s` flag, as: 1. the GOFLAGS functionality currently only allows 1 ldflag to be set[2] (NB this may be fixed in Go 1.15[3]) 2. The improvements to the binary size in Go 1.15 [4] mean that the `-s` flag will no longer really do anything[5] Will also try shrinking the binary size further using upx[6] in a separate pull request. [1] https://stackoverflow.com/a/22276273 [2] golang/go#38522 [3] golang/go#26849 (comment) [4] See #64 [5] https://twitter.com/bradfitz/status/1256989624590712834 [6] https://upx.github.io/
This commit removes the DWARF debugging information[1], resulting in the Docker image size shrinking from 4.76MB to 2.88MB. Decided not to try to also turn off generation of the Go symbol table using the `-s` flag, as: 1. the GOFLAGS functionality currently only allows 1 ldflag to be set[2] (NB this may be fixed in Go 1.15[3]) 2. The improvements to the binary size in Go 1.15 [4] mean that the `-s` flag will no longer really do anything[5] Will also try shrinking the binary size further using upx[6] in a separate pull request. [1] https://stackoverflow.com/a/22276273 [2] golang/go#38522 [3] golang/go#26849 (comment) [4] See #64 [5] https://twitter.com/bradfitz/status/1256989624590712834 [6] https://upx.github.io/
Just hit this while trying to make a Specific problems encountered:
My conclusion so far is that making Go more pleasant to use in non-trivial build environments is not possible with GOFLAGS, and painfully difficult without. Neither is a happy outcome, and makes it harder to use a mostly-plain EDIT: sorry, minor correction. Looking at the source code of the |
This requires moving ldflags from goflags to the cmdline because of golang/go#26849
This requires moving ldflags from goflags to the cmdline because of golang/go#26849
This is interesting as some Golang RPM packaging macros rely on the packager being able to use |
For what it's worth, this isn't the right syntax. It should be
which doesn't require any additional quoting (also -mod=vendor has been implied by the presence of a vendor directory since Go 1.14). Of course, if you want two -X flags, now you need more quoting. The hard part about all this is that there's no standard definition of quoting, nor how you escape inner quotes, and so on. It was much easier to say don't do that. Unfortunately, this does seem to keep coming up, so maybe we need to make a change. In 2018 I wrote
CL 341936 changed that. It would make sense to do the same for $GOFLAGS. The specific definition of quoting we now use in other variables should be completely backwards compatible for use in $GOFLAGS: the quotes must go around the entire thing, not appear mid-word. That means for $GOFLAGS they must go around the -name=value, and to date any field not beginning with - would have been rejected. So no extant valid $GOFLAGS settings would change meaning. |
Thinking aloud, it would be nice if the quoting was somewhat consistent between
Then, one could imagine two levels of quoting as an extreme example:
Though I admit that, even with the added spaces for readability, I still struggle a bit to follow what's going on. An alternative that doesn't mean escaping quote characters (akin to POSIX shell or Go's double-quoted string literals) would be for
We remove the need for the nested quotes, at least. Multi-line strings in interactive shells aren't super pretty, but I imagine such nested quoting scenarios will most commonly happen in moderately complex shell scripts or programs calling cmd/go, in which case the multi-line strings are generally not a problem at all - and more readable. |
I suggested exactly that multiline approach to Bryan just before posting what I did. 😄 The 'two levels of quoting' example does not behave as written in your post, because quotes can't nest (you can't tell left from right). If you want to pass two different ldflags, you have to use different quotes, as in (this is the actual argv value):
and then to get that into GOFLAGS you have to put (different) quotes around it as in
or you can swap the quotes:
To get this into a shell you have to introduce a third level of quoting; in sh you use " so that you can escape things inside:
All this complexity is exactly why I said "Sorry, but no" in 2018. But the giant improv scene that is software engineering plays on, and other players sent us down this path. Now the right thing to do is almost certainly to yes and ..., meaning do what makes the most sense given those earlier decisions, not try to back them out. For the many parts of the toolchain that now accept possibly quoted space-separated argument lists, there is one consistent rule about how you add quotes. I think it serves us much better to keep to that convention, restoring the 2018 invariant that all environment variables are handled the same way, rather than introduce a separate convention just for GOFLAGS. |
Also go env -w disallows newlines in values:
|
Change https://go.dev/cl/443956 mentions this issue: |
Yeah, using newlines is a bit awkward, but the bigger problem is that it clashes with the |
GOFLAGS didn't split on quotes because no other env vars (such as CC, CXX, ...) did either. This kept them all consistent. CL 341936 changed everything but GOFLAGS, making them inconsistent. Split GOFLAGS the same way as the other environment variables. Fixes golang#26849. Change-Id: I99bb450fe30cab949da48af133b6a36ff320532f Reviewed-on: https://go-review.googlesource.com/c/go/+/443956 Run-TryBot: Russ Cox <[email protected]> Reviewed-by: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Russ Cox <[email protected]>
In https://golang.org/cl/126656, @rsc added the
GOFLAGS
environment variable.The variable is space-separated, but it is sometimes useful to include spaces within flags passed to the go command (for example, as the final argument to
list -f
). It would be nice to recognize at least a subset of the usual quoting conventions when parsingGOFLAGS
.The text was updated successfully, but these errors were encountered: