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 Oct 9, 2024
1 parent 6b9e1e5 commit e3d65d8
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions go/tools/builders/compilepkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,14 +321,24 @@ 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 := os.WriteFile(cgoExportHPath, nil, 0o666); 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))
}

importcfgPath, err := checkImportsAndBuildCfg(goenv, importPath, srcs, deps, packageListPath, recompileInternalDeps, compilingWithCgo, coverMode, workDir)
Expand Down Expand Up @@ -495,7 +505,7 @@ func compileGo(goenv *env, srcs []string, packagePath, importcfgPath, embedcfgPa
args = append(args, "-linkobj", outLinkobjPath)
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 e3d65d8

Please sign in to comment.