Skip to content

Commit

Permalink
compilepkg: fix stored file path truncation
Browse files Browse the repository at this point in the history
This fixes the issue described in cockroachdb/cockroach#64379 where
the internal paths of the files don't include the
`github.com/cockroachdb/cockroach` prefix.

We fix this by using the rewrite syntax of `-trimpath`, e.g.
`-trimpath=/sandbox/execroot=>github.com/cockroachdb/cockroach`.

Figuring out the replacement part is not trivial. We use the package
path and strip out the relative path of the source file directory.
  • Loading branch information
RaduBerinde authored and rickystewart committed Jun 3, 2022
1 parent a827cb1 commit 9e31cf1
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions go/tools/builders/compilepkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,23 @@ func compileArchive(
return err
}

gcFlags = append(gcFlags, createTrimPath(gcFlags, srcDir))
gcFlags = append(gcFlags, fmt.Sprintf("-trimpath=%s=>%s", abs(srcDir), packagePath))
} else {
if cgoExportHPath != "" {
if err := ioutil.WriteFile(cgoExportHPath, nil, 0666); err != nil {
return err
}
}
gcFlags = append(gcFlags, createTrimPath(gcFlags, "."))
// We want the source files to show up (e.g. in stack traces) with the package
// path. We use -trimpath to replace the root path with the correct prefix
// of the package path.
root := abs(".")
relSrcPath, err := filepath.Rel(root, srcs.goSrcs[0].filename)
if err != nil {
return err
}
rootPkgPath := filepath.Clean(strings.TrimSuffix(packagePath, filepath.Dir(relSrcPath)))
gcFlags = append(gcFlags, fmt.Sprintf("-trimpath=%s=>%s", root, rootPkgPath))
}

// Check that the filtered sources don't import anything outside of
Expand Down Expand Up @@ -495,7 +504,7 @@ func compileGo(goenv *env, srcs []string, packagePath, importcfgPath, embedcfgPa
args = append(args, "-o", outPath)
args = append(args, "--")
args = append(args, srcs...)
absArgs(args, []string{"-I", "-o", "-trimpath", "-importcfg"})
absArgs(args, []string{"-I", "-o", "-importcfg"})
return goenv.runCommand(args)
}

Expand Down

0 comments on commit 9e31cf1

Please sign in to comment.