diff --git a/go/tools/builders/BUILD.bazel b/go/tools/builders/BUILD.bazel index 192c67993c..762983af13 100644 --- a/go/tools/builders/BUILD.bazel +++ b/go/tools/builders/BUILD.bazel @@ -34,7 +34,10 @@ go_test( "stdliblist_test.go", ], data = ["@go_sdk//:files"], - rundir = ".", + # -sdk in stdliblist needs to be a relative path, otherwise it breaks + # assumptions of cloning go_sdk, thus we need to set up the test in a + # way that go_sdk is under the directory where test is run. + rundir = "..", deps = ["//go/tools/bazel"], ) diff --git a/go/tools/builders/env.go b/go/tools/builders/env.go index 694754c90b..3ed12e9745 100644 --- a/go/tools/builders/env.go +++ b/go/tools/builders/env.go @@ -44,9 +44,6 @@ var ( // See ./README.rst for more information about handling arguments and // environment variables. type env struct { - // wd is the path to the working directory - wd string - // sdk is the path to the Go SDK, which contains tools for the host // platform. This may be different than GOROOT. sdk string @@ -69,12 +66,11 @@ type env struct { // configured with those flags. func envFlags(flags *flag.FlagSet) *env { env := &env{} - flags.StringVar(&env.sdk, "sdk", "", "Path to the Go SDK.") + flags.StringVar(&env.sdk, "sdk", "", "Relative path to the Go SDK.") flags.Var(&tagFlag{}, "tags", "List of build tags considered true.") flags.StringVar(&env.installSuffix, "installsuffix", "", "Standard library under GOROOT/pkg") flags.BoolVar(&env.verbose, "v", false, "Whether subprocess command lines should be printed") flags.BoolVar(&env.shouldPreserveWorkDir, "work", false, "if true, the temporary work directory will be preserved") - flags.StringVar(&env.wd, "wd", ".", "working director default to dot") return env } @@ -125,9 +121,6 @@ func (e *env) goTool(tool string, args ...string) []string { // and additional arguments. func (e *env) goCmd(cmd string, args ...string) []string { exe := filepath.Join(e.sdk, "bin", "go") - if len(e.wd) > 0 { - exe = filepath.Join(e.wd, exe) - } if runtime.GOOS == "windows" { exe += ".exe" diff --git a/go/tools/builders/stdliblist.go b/go/tools/builders/stdliblist.go index 571a2a4b81..46f86d1103 100644 --- a/go/tools/builders/stdliblist.go +++ b/go/tools/builders/stdliblist.go @@ -157,22 +157,22 @@ func flatPackageForStd(cloneBase string, pkg *goListPackage) *flatPackage { // go:embed directives will refuse to include the symlinks in the sandbox. // // To work around this, cloneGoRoot creates a copy of a subset of external/go_sdk -// that is sufficient to call "go list" into a new cloneBase directory while -// retaining its path relative to the root directory, e.g. "go list" needs to -// call "compile", which needs "pkg/tool". -// So "$OUTPUT_BASE/external/go_sdk" becomes +// that is sufficient to call "go list" into a new cloneBase directory, e.g. +// "go list" needs to call "compile", which needs "pkg/tool". +// We also need to retain the same relative path to the root directory, e.g. +// "$OUTPUT_BASE/external/go_sdk" becomes // {cloneBase}/external/go_sdk", which will be set at GOROOT later. This ensures // that file paths in the generated JSON are still valid. // // cloneGoRoot returns the new GOROOT we should run under. -func cloneGoRoot(execRoot, relativeGoroot, cloneBase string) (newGoRoot string, err error) { - goroot := filepath.Join(execRoot, relativeGoroot) - newGoRoot = filepath.Join(cloneBase, relativeGoroot) +func cloneGoRoot(relativeGoRoot, cloneBase string) (newGoRoot string, err error) { + goRoot := abs(relativeGoRoot) + newGoRoot = filepath.Join(cloneBase, relativeGoRoot) if err := os.MkdirAll(newGoRoot, 01755); err != nil { return "", err } - if err := replicate(goroot, newGoRoot, replicatePaths("src", "pkg/tool", "pkg/include")); err != nil { + if err := replicate(goRoot, newGoRoot, replicatePaths("src", "pkg/tool", "pkg/include")); err != nil { return "", err } @@ -198,11 +198,10 @@ func stdliblist(args []string) error { } defer func() { cleanup() }() - cloneGoRoot, err := cloneGoRoot(goenv.wd, goenv.sdk, cloneBase) + cloneGoRoot, err := cloneGoRoot(goenv.sdk, cloneBase) if err != nil { return fmt.Errorf("failed to clone new go root %v", err) } - // Ensure paths are absolute. absPaths := []string{} for _, path := range filepath.SplitList(os.Getenv("PATH")) { diff --git a/go/tools/builders/stdliblist_test.go b/go/tools/builders/stdliblist_test.go index bcd713be27..0cc5b7e488 100644 --- a/go/tools/builders/stdliblist_test.go +++ b/go/tools/builders/stdliblist_test.go @@ -8,31 +8,22 @@ import ( "path/filepath" "strings" "testing" - - "github.com/bazelbuild/rules_go/go/tools/bazel" ) func Test_stdliblist(t *testing.T) { testDir := t.TempDir() outJSON := filepath.Join(testDir, "out.json") - // test files are at run file directory, but this test is run at - // {runfile directory}/bazel.TestWorkspace() - // since -sdk is assumed to be a relative path to execRoot - // (go.sdk.root_file.dirname), thus setting wd to - // {runfile directory} so that go_sdk is discoverable - // {runfile directory} is the parent directory of bazel.RunfilesPath() - runFilesPath, err := bazel.RunfilesPath() - if err != nil { - t.Error("failed to find runfiles path") - } + // test files are at {runfile directory}/go_sdk, + // this test is run at {runfile directory}/{workspace}/../ + // thus go_sdk is the relative path to current working + // directory test_args := []string{ fmt.Sprintf("-out=%s", outJSON), fmt.Sprintf("-sdk=%s", "go_sdk"), - fmt.Sprintf("-wd=%s", filepath.Dir(filepath.Clean(runFilesPath))), } - err = stdliblist(test_args) + err := stdliblist(test_args) if err != nil { t.Errorf("calling stdliblist got err: %v", err) }