]> Cypherpunks repositories - gostls13.git/commit
cmd/go/internal/generate: stop premature variable substitution in commands
authorShawn Elliott <selliott@microsoft.com>
Mon, 22 Apr 2019 14:45:19 +0000 (14:45 +0000)
committerJay Conrod <jayconrod@google.com>
Mon, 22 Apr 2019 18:34:50 +0000 (18:34 +0000)
commite6ae4e86ad59c45f302a8828e77e6c234307fce4
treec296c75862cf3b3ccf325ebaa2e87629860ba362
parent43001a0dc96a29f662f2782c5fb3ca998eadd623
cmd/go/internal/generate: stop premature variable substitution in commands

go:generate commands passed no arguments are currently subject
to premature variable substitution due to mistakenly assuming append
guarantees a copy.  The change fixes this by forcing a slice copy at
each invocation of a command.

The previous code assumed that append would always generate a
copy of its inputs. However, append wouldn't create a copy if there was
no need to increase capacity and it would just return the original
input slice. This resulted in premature variable substitutions in
the "master word list" of generate commands, thus yielding incorrect
results across multiple invocations of the same command when the
body contained substitutions e.g. environment variables, moreover
these can change during the lifetime of go:generate processing a
file.

Note that this behavior would not manifest itself if any arguments were
passed to the command, because append would make a copy of the slice
as it needed to increase its capacity.   The "hacky" work-around was to
always pass at least one argument to any command, even if the
command ignores it.  e.g.,
       //go:generate MyNoArgsCmd ' '

This CL fixes that issue and removes the need for the hack mentioned
above.

Fixes #31608

Change-Id: I782ac2234bd7035a37f61c101ee4aee38ed8d29f
GitHub-Last-Rev: 796d3430191f183c123c450a60b4a7987cc85e20
GitHub-Pull-Request: golang/go#31527
Reviewed-on: https://go-review.googlesource.com/c/go/+/172580
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
src/cmd/go/internal/generate/generate.go
src/cmd/go/internal/generate/generate_test.go