]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo: retain original file paths in godefs generated comment
authorTobias Klauser <tklauser@distanz.ch>
Fri, 1 Apr 2022 07:41:57 +0000 (09:41 +0200)
committerTobias Klauser <tobias.klauser@gmail.com>
Sun, 3 Apr 2022 07:18:10 +0000 (07:18 +0000)
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 <tobias.klauser@gmail.com>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
misc/cgo/testgodefs/testgodefs_test.go
src/cmd/cgo/godefs.go
src/cmd/cgo/main.go

index 7628ffc595b7e261ad24e502d8257f1d7494f1f1..d03769ea87cce11b23da399fefa219e104a1d51a 100644 (file)
@@ -9,6 +9,7 @@ import (
        "os"
        "os/exec"
        "path/filepath"
+       "runtime"
        "strings"
        "testing"
 )
@@ -58,9 +59,32 @@ func TestGoDefs(t *testing.T) {
                        t.Fatalf("%s: %v\n%s", strings.Join(cmd.Args, " "), err, cmd.Stderr)
                }
 
-               if err := os.WriteFile(filepath.Join(dir, fp+"_defs.go"), out, 0644); err != nil {
+               fn := fp + "_defs.go"
+               if err := os.WriteFile(filepath.Join(dir, fn), out, 0644); err != nil {
                        t.Fatal(err)
                }
+
+               // Verify that command line arguments are not rewritten in the generated comment,
+               // see go.dev/issue/52063
+               hasGeneratedByComment := false
+               for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
+                       cgoExe := "cgo"
+                       if runtime.GOOS == "windows" {
+                               cgoExe = "cgo.exe"
+                       }
+                       if !strings.HasPrefix(line, "// "+cgoExe+" -godefs") {
+                               continue
+                       }
+                       if want := "// " + cgoExe + " " + strings.Join(cmd.Args[3:], " "); line != want {
+                               t.Errorf("%s: got generated comment %q, want %q", fn, line, want)
+                       }
+                       hasGeneratedByComment = true
+                       break
+               }
+
+               if !hasGeneratedByComment {
+                       t.Errorf("%s: comment with generating cgo -godefs command not found", fn)
+               }
        }
 
        main, err := os.ReadFile(filepath.Join("testdata", "main.go"))
index c0d59aee01d747ec8ed02a587c72da0dd55f016b..3a27b31bfb3294a65e1aa2abff7ba568bb5b0206 100644 (file)
@@ -16,11 +16,11 @@ import (
 )
 
 // godefs returns the output for -godefs mode.
-func (p *Package) godefs(f *File) string {
+func (p *Package) godefs(f *File, args []string) string {
        var buf bytes.Buffer
 
        fmt.Fprintf(&buf, "// Code generated by cmd/cgo -godefs; DO NOT EDIT.\n")
-       fmt.Fprintf(&buf, "// %s %s\n", filepath.Base(os.Args[0]), strings.Join(os.Args[1:], " "))
+       fmt.Fprintf(&buf, "// %s %s\n", filepath.Base(args[0]), strings.Join(args[1:], " "))
        fmt.Fprintf(&buf, "\n")
 
        override := make(map[string]string)
index 14642b7576b0122e0252960c498fe9ed11a21826..364d8b81fbb3c9cac2491a6edc22c31a32b60668 100644 (file)
@@ -291,6 +291,10 @@ func main() {
                usage()
        }
 
+       // Save original command line arguments for the godefs generated comment. Relative file
+       // paths in os.Args will be rewritten to absolute file paths in the loop below.
+       osArgs := make([]string, len(os.Args))
+       copy(osArgs, os.Args[:])
        goFiles := args[i:]
 
        for _, arg := range args[:i] {
@@ -390,7 +394,7 @@ func main() {
                p.PackagePath = f.Package
                p.Record(f)
                if *godefs {
-                       os.Stdout.WriteString(p.godefs(f))
+                       os.Stdout.WriteString(p.godefs(f, osArgs))
                } else {
                        p.writeOutput(f, input)
                }