Skip to content

Commit

Permalink
works
Browse files Browse the repository at this point in the history
  • Loading branch information
xytan0056 committed May 18, 2022
1 parent 8b0dde2 commit 0c12fc9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 33 deletions.
5 changes: 4 additions & 1 deletion go/tools/builders/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)

Expand Down
9 changes: 1 addition & 8 deletions go/tools/builders/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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"
Expand Down
19 changes: 9 additions & 10 deletions go/tools/builders/stdliblist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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")) {
Expand Down
19 changes: 5 additions & 14 deletions go/tools/builders/stdliblist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 0c12fc9

Please sign in to comment.