| 1 | // Copyright 2020 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package gocommand_test |
| 6 | |
| 7 | import ( |
| 8 | "context" |
| 9 | "fmt" |
| 10 | "io/fs" |
| 11 | "os" |
| 12 | "os/exec" |
| 13 | "path/filepath" |
| 14 | "strings" |
| 15 | "testing" |
| 16 | "time" |
| 17 | |
| 18 | "golang.org/x/sync/errgroup" |
| 19 | "golang.org/x/tools/internal/gocommand" |
| 20 | "golang.org/x/tools/internal/testenv" |
| 21 | ) |
| 22 | |
| 23 | func TestGoVersion(t *testing.T) { |
| 24 | testenv.NeedsTool(t, "go") |
| 25 | |
| 26 | inv := gocommand.Invocation{ |
| 27 | Verb: "version", |
| 28 | } |
| 29 | gocmdRunner := &gocommand.Runner{} |
| 30 | if _, err := gocmdRunner.Run(context.Background(), inv); err != nil { |
| 31 | t.Error(err) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // This is not a test of go/packages at all: it's a test of whether it |
| 36 | // is possible to delete the directory used by go list once it has |
| 37 | // finished. It is intended to evaluate the hypothesis (to explain |
| 38 | // issue #71544) that the go command, on Windows, occasionally fails |
| 39 | // to release all its handles to the temporary directory even when it |
| 40 | // should have finished. |
| 41 | // |
| 42 | // If this test ever fails, the combination of the gocommand package |
| 43 | // and the go command itself has a bug; this has been observed (#73503). |
| 44 | func TestRmdirAfterGoList_Runner(t *testing.T) { |
| 45 | t.Skip("flaky; see https://github.com/golang/go/issues/73736#issuecomment-2885407104") |
| 46 | |
| 47 | testRmdirAfterGoList(t, func(ctx context.Context, dir string) { |
| 48 | var runner gocommand.Runner |
| 49 | stdout, stderr, friendlyErr, err := runner.RunRaw(ctx, gocommand.Invocation{ |
| 50 | Verb: "list", |
| 51 | Args: []string{"-json", "example.com/p"}, |
| 52 | WorkingDir: dir, |
| 53 | }) |
| 54 | if ctx.Err() != nil { |
| 55 | return // don't report error if canceled |
| 56 | } |
| 57 | if err != nil || friendlyErr != nil { |
| 58 | t.Fatalf("go list failed: %v, %v (stdout=%s stderr=%s)", |
| 59 | err, friendlyErr, stdout, stderr) |
| 60 | } |
| 61 | }) |
| 62 | } |
| 63 | |
| 64 | // TestRmdirAfterGoList_Direct is a variant of |
| 65 | // TestRmdirAfterGoList_Runner that executes go list directly, to |
| 66 | // control for the substantial logic of the gocommand package. |
| 67 | // |
| 68 | // It has two variants: the first does not set WaitDelay; the second |
| 69 | // sets it to 30s. If the first variant ever fails, the go command |
| 70 | // itself has a bug; as of May 2025 this has never been observed. |
| 71 | // |
| 72 | // If the second variant fails, it indicates that the WaitDelay |
| 73 | // mechanism is responsible for causing Wait to return before the |
| 74 | // child process has naturally finished. This is to confirm the |
| 75 | // hypothesis at https://github.com/golang/go/issues/73736#issuecomment-2885407104. |
| 76 | func TestRmdirAfterGoList_Direct(t *testing.T) { |
| 77 | for _, delay := range []time.Duration{0, 30 * time.Second} { |
| 78 | t.Run(delay.String(), func(t *testing.T) { |
| 79 | testRmdirAfterGoList(t, func(ctx context.Context, dir string) { |
| 80 | cmd := exec.Command("go", "list", "-json", "example.com/p") |
| 81 | cmd.Dir = dir |
| 82 | cmd.Stdout = new(strings.Builder) |
| 83 | cmd.Stderr = new(strings.Builder) |
| 84 | cmd.WaitDelay = delay |
| 85 | err := cmd.Run() |
| 86 | if ctx.Err() != nil { |
| 87 | return // don't report error if canceled |
| 88 | } |
| 89 | if err != nil { |
| 90 | t.Fatalf("go list failed: %v (stdout=%s stderr=%s)", |
| 91 | err, cmd.Stdout, cmd.Stderr) |
| 92 | } |
| 93 | }) |
| 94 | }) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | func testRmdirAfterGoList(t *testing.T, f func(ctx context.Context, dir string)) { |
| 99 | testenv.NeedsExec(t) |
| 100 | |
| 101 | dir := t.TempDir() |
| 102 | if err := os.Mkdir(filepath.Join(dir, "p"), 0777); err != nil { |
| 103 | t.Fatalf("mkdir p: %v", err) |
| 104 | } |
| 105 | |
| 106 | // Create a go.mod file and 100 trivial Go files for the go command to read. |
| 107 | if err := os.WriteFile(filepath.Join(dir, "go.mod"), []byte("module example.com"), 0666); err != nil { |
| 108 | t.Fatal(err) |
| 109 | } |
| 110 | for i := range 100 { |
| 111 | filename := filepath.Join(dir, fmt.Sprintf("p/%d.go", i)) |
| 112 | if err := os.WriteFile(filename, []byte("package p"), 0666); err != nil { |
| 113 | t.Fatal(err) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | t0 := time.Now() |
| 118 | g, ctx := errgroup.WithContext(context.Background()) |
| 119 | for range 10 { |
| 120 | g.Go(func() error { |
| 121 | f(ctx, dir) |
| 122 | // Return an error so that concurrent invocations are canceled. |
| 123 | return fmt.Errorf("oops") |
| 124 | }) |
| 125 | } |
| 126 | g.Wait() // ignore error (expected) |
| 127 | |
| 128 | t.Logf("10 concurrent executions (9 canceled) took %v", time.Since(t0)) |
| 129 | |
| 130 | // This is the critical operation. |
| 131 | if err := os.RemoveAll(dir); err != nil { |
| 132 | t.Errorf("failed to remove temp dir: %v", err) |
| 133 | // List the contents of the directory, for clues. |
| 134 | filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { |
| 135 | t.Log(path, d, err) |
| 136 | return nil |
| 137 | }) // ignore error |
| 138 | } |
| 139 | } |