You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Maybe my bias arises from my understanding (or lack thereof) of the behaviour of bash etc in these situations, but I see two problems here:
in the second instance, the unquoted $VARNOTSET should not have resulted in the empty string being passed as os.Args[1] to playground
in the third instance, the unquoted $HELLOWORLD should have resulted in hello and world being passed as os.Args[2] and os.Args[3] respectively, not hello world
I stumbled across this because I was looking to use environment variables to pass common flags to my go generators (to adjust log levels). But when that environment variable is not set, the empty string is passed as an argument... and flag parsing stops just before the first non-flag argument, and the empty string matches that condition.
I can think of one workaround: putting these flag environment variables at the end of all my go:generate directives).. and then ensure my go generators do not do any handling of flag.Args() (or equivalent).
Assuming for one second the current behaviour is not desirable/expected, I can't actually think of a good solution here that is backwards compatible...
It's working as intended. As documented, it doesn't run the shell, but rather uses Go quoting rules, and $VARNOTSET is an empty string. For the behavior you want, you need to run the shell, something like this (untested):
//go:generate sh -c 'playground $VARNOTSET "$VARNOTSET"'
As documented, it doesn't run the shell, but rather uses Go quoting rules, and $VARNOTSET is an empty string
I guess the key bit here is that this happens after tokenising (from go generate -help):
The arguments to the directive are space-separated tokens or
double-quoted strings passed to the generator as individual
arguments when it is run.
...
As a last step before running the command, any invocations of any
environment variables with alphanumeric names, such as $GOFILE or
$HOME, are expanded throughout the command line.
So it's definitely working as documented, and given that you've confirmed it's also working as intended, you've confirmed my bash bias is the root cause of my bad expectations...
The solution using sh -c does indeed work, but such an approach then makes parsing of go generate directives that bit more tricky (instead of simply using the basename of the first word after the directive, post expansion). I've opted for an alternative solution for now
What version of Go are you using (
go version
)?(but same behaviour observed at tip)
What operating system and processor architecture are you using (
go env
)?What did you do?
Place the code found here https://play.golang.org/p/PKeAQFaVSe in a package directory called
playground
and then run:What did you expect to see?
What did you see instead?
Maybe my bias arises from my understanding (or lack thereof) of the behaviour of
bash
etc in these situations, but I see two problems here:$VARNOTSET
should not have resulted in the empty string being passed asos.Args[1]
toplayground
$HELLOWORLD
should have resulted inhello
andworld
being passed asos.Args[2]
andos.Args[3]
respectively, nothello world
I stumbled across this because I was looking to use environment variables to pass common flags to my go generators (to adjust log levels). But when that environment variable is not set, the empty string is passed as an argument... and flag parsing stops just before the first non-flag argument, and the empty string matches that condition.
I can think of one workaround: putting these flag environment variables at the end of all my
go:generate
directives).. and then ensure my go generators do not do any handling offlag.Args()
(or equivalent).Assuming for one second the current behaviour is not desirable/expected, I can't actually think of a good solution here that is backwards compatible...
cc @robpike perhaps?
The text was updated successfully, but these errors were encountered: