Skip to content

Commit 01c93ad

Browse files
matloobmknyszek
authored andcommitted
[release-branch.go1.21] cmd/go/internal/generate: call modload.InitWorkFile
This is necessary for go generate to enter workspace mode for recognizing package paths in the workspace. For #56098 Fixes #65351 Change-Id: I25f68de24f4189259353f63194823516e9d3d505 Reviewed-on: https://go-review.googlesource.com/c/go/+/559195 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Bryan Mills <[email protected]> (cherry picked from commit b91bad7) Reviewed-on: https://go-review.googlesource.com/c/go/+/559235
1 parent db74bfb commit 01c93ad

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/cmd/go/internal/generate/generate.go

+2
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ func init() {
181181
}
182182

183183
func runGenerate(ctx context.Context, cmd *base.Command, args []string) {
184+
modload.InitWorkfile()
185+
184186
if generateRunFlag != "" {
185187
var err error
186188
generateRunRE, err = regexp.Compile(generateRunFlag)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# This is a regression test for Issue #56098: Go generate
2+
# wasn't initializing workspace mode
3+
4+
[short] skip
5+
6+
go generate ./mod
7+
cmp ./mod/got.txt want.txt
8+
9+
-- go.work --
10+
go 1.21
11+
12+
use ./mod
13+
-- mod/go.mod --
14+
module example.com/mod
15+
-- mod/gen.go --
16+
//go:generate go run gen.go got.txt
17+
18+
package main
19+
20+
import "os"
21+
22+
func main() {
23+
outfile := os.Args[1]
24+
os.WriteFile(outfile, []byte("Hello World!\n"), 0644)
25+
}
26+
-- want.txt --
27+
Hello World!

0 commit comments

Comments
 (0)