Skip to content

Commit 73a81d8

Browse files
committed
cmd/cgo: retain original file paths in godefs generated comment
Don't rewrite relative file paths to absolute file paths in the godefs generated code comment. Fixes #52063 Change-Id: Ie9c5bd021b8f3954e827838930861622c7aa90b4 Reviewed-on: https://go-review.googlesource.com/c/go/+/396936 Trust: Tobias Klauser <[email protected]> Run-TryBot: Tobias Klauser <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 8e50298 commit 73a81d8

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

misc/cgo/testgodefs/testgodefs_test.go

+25-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"os/exec"
1111
"path/filepath"
12+
"runtime"
1213
"strings"
1314
"testing"
1415
)
@@ -58,9 +59,32 @@ func TestGoDefs(t *testing.T) {
5859
t.Fatalf("%s: %v\n%s", strings.Join(cmd.Args, " "), err, cmd.Stderr)
5960
}
6061

61-
if err := os.WriteFile(filepath.Join(dir, fp+"_defs.go"), out, 0644); err != nil {
62+
fn := fp + "_defs.go"
63+
if err := os.WriteFile(filepath.Join(dir, fn), out, 0644); err != nil {
6264
t.Fatal(err)
6365
}
66+
67+
// Verify that command line arguments are not rewritten in the generated comment,
68+
// see go.dev/issue/52063
69+
hasGeneratedByComment := false
70+
for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
71+
cgoExe := "cgo"
72+
if runtime.GOOS == "windows" {
73+
cgoExe = "cgo.exe"
74+
}
75+
if !strings.HasPrefix(line, "// "+cgoExe+" -godefs") {
76+
continue
77+
}
78+
if want := "// " + cgoExe + " " + strings.Join(cmd.Args[3:], " "); line != want {
79+
t.Errorf("%s: got generated comment %q, want %q", fn, line, want)
80+
}
81+
hasGeneratedByComment = true
82+
break
83+
}
84+
85+
if !hasGeneratedByComment {
86+
t.Errorf("%s: comment with generating cgo -godefs command not found", fn)
87+
}
6488
}
6589

6690
main, err := os.ReadFile(filepath.Join("testdata", "main.go"))

src/cmd/cgo/godefs.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ import (
1616
)
1717

1818
// godefs returns the output for -godefs mode.
19-
func (p *Package) godefs(f *File) string {
19+
func (p *Package) godefs(f *File, args []string) string {
2020
var buf bytes.Buffer
2121

2222
fmt.Fprintf(&buf, "// Code generated by cmd/cgo -godefs; DO NOT EDIT.\n")
23-
fmt.Fprintf(&buf, "// %s %s\n", filepath.Base(os.Args[0]), strings.Join(os.Args[1:], " "))
23+
fmt.Fprintf(&buf, "// %s %s\n", filepath.Base(args[0]), strings.Join(args[1:], " "))
2424
fmt.Fprintf(&buf, "\n")
2525

2626
override := make(map[string]string)

src/cmd/cgo/main.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@ func main() {
291291
usage()
292292
}
293293

294+
// Save original command line arguments for the godefs generated comment. Relative file
295+
// paths in os.Args will be rewritten to absolute file paths in the loop below.
296+
osArgs := make([]string, len(os.Args))
297+
copy(osArgs, os.Args[:])
294298
goFiles := args[i:]
295299

296300
for _, arg := range args[:i] {
@@ -390,7 +394,7 @@ func main() {
390394
p.PackagePath = f.Package
391395
p.Record(f)
392396
if *godefs {
393-
os.Stdout.WriteString(p.godefs(f))
397+
os.Stdout.WriteString(p.godefs(f, osArgs))
394398
} else {
395399
p.writeOutput(f, input)
396400
}

0 commit comments

Comments
 (0)