1
0
mirror of https://github.com/golang/go synced 2024-11-26 19:51:17 -07:00

cmd/go/internal/generate: call modload.InitWorkFile

This is necessary for go generate to enter workspace mode for
recognizing package paths in the workspace.

Fixes #56098

Change-Id: I25f68de24f4189259353f63194823516e9d3d505
Cq-Include-Trybots: luci.golang.try:gotip-linux-amd64-longtest,gotip-windows-amd64-longtest
Reviewed-on: https://go-review.googlesource.com/c/go/+/559195
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
This commit is contained in:
Michael Matloob 2024-01-29 11:14:05 -05:00
parent a64d5be912
commit b91bad7819
2 changed files with 29 additions and 0 deletions

View File

@ -181,6 +181,8 @@ func init() {
}
func runGenerate(ctx context.Context, cmd *base.Command, args []string) {
modload.InitWorkfile()
if generateRunFlag != "" {
var err error
generateRunRE, err = regexp.Compile(generateRunFlag)

View File

@ -0,0 +1,27 @@
# This is a regression test for Issue #56098: Go generate
# wasn't initializing workspace mode
[short] skip
go generate ./mod
cmp ./mod/got.txt want.txt
-- go.work --
go 1.22
use ./mod
-- mod/go.mod --
module example.com/mod
-- mod/gen.go --
//go:generate go run gen.go got.txt
package main
import "os"
func main() {
outfile := os.Args[1]
os.WriteFile(outfile, []byte("Hello World!\n"), 0644)
}
-- want.txt --
Hello World!