Debian Codegraph @

internal/gocommand/invoke_test.go

raw symbols SCIP
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_testscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand_test`/
6
7 import (
8 "contextscip-go gomod github.com/golang/go/src go1.25.0 context/"
9 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
10 "io/fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/"
11 "osscip-go gomod github.com/golang/go/src go1.25.0 os/"
12 "os/execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/"
13 "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/"
14 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
15 "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/"
16 "timescip-go gomod github.com/golang/go/src go1.25.0 time/"
17
18 "golang.org/x/sync/errgroupscip-go gomod golang.org/x/sync v0.20.0 `golang.org/x/sync/errgroup`/"
19 "golang.org/x/tools/internal/gocommandscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/"
20 "golang.org/x/tools/internal/testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/"
21 )
22
23 func TestGoVersionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand_test`/TestGoVersion().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
24 testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsToolscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsTool().(t, "go")
25
26 inv := gocommandscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/.Invocationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Invocation#{
27 Verbscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Invocation#Verb.: "version",
28 }
29 gocmdRunner := &gocommandscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/.Runnerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Runner#{}
30 if _, err := gocmdRunner.Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Runner#Run().(contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Backgroundscip-go gomod github.com/golang/go/src go1.25.0 context/Background().(), inv); err != nil {
31 t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#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_Runnerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand_test`/TestRmdirAfterGoList_Runner().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
45 t.Skipscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skip().("flaky; see https://github.com/golang/go/issues/73736#issuecomment-2885407104")
46
47 testRmdirAfterGoListscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand_test`/testRmdirAfterGoList().(t, func(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, dir string) {
48 var runner gocommandscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/.Runnerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Runner#
49 stdout, stderr, friendlyErr, err := runner.RunRawscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Runner#RunRaw().(ctx, gocommandscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/.Invocationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Invocation#{
50 Verbscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Invocation#Verb.: "list",
51 Argsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Invocation#Args.: []string{"-json", "example.com/p"},
52 WorkingDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Invocation#WorkingDir.: dir,
53 })
54 if ctx.Errscip-go gomod github.com/golang/go/src go1.25.0 context/Context#Err().() != nil {
55 return // don't report error if canceled
56 }
57 if err != nil || friendlyErr != nil {
58 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#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_Directscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand_test`/TestRmdirAfterGoList_Direct().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
77 for _, delay := range []timescip-go gomod github.com/golang/go/src go1.25.0 time/.Durationscip-go gomod github.com/golang/go/src go1.25.0 time/Duration#{0, 30 * timescip-go gomod github.com/golang/go/src go1.25.0 time/.Secondscip-go gomod github.com/golang/go/src go1.25.0 time/Second.} {
78 t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(delay.Stringscip-go gomod github.com/golang/go/src go1.25.0 time/Duration#String().(), func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
79 testRmdirAfterGoListscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand_test`/testRmdirAfterGoList().(t, func(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, dir string) {
80 cmd := execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Command().("go", "list", "-json", "example.com/p")
81 cmd.Dirscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Dir. = dir
82 cmd.Stdoutscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Stdout. = new(stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Builderscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#)
83 cmd.Stderrscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Stderr. = new(stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Builderscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#)
84 cmd.WaitDelayscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#WaitDelay. = delay
85 err := cmd.Runscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Run().()
86 if ctx.Errscip-go gomod github.com/golang/go/src go1.25.0 context/Context#Err().() != nil {
87 return // don't report error if canceled
88 }
89 if err != nil {
90 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("go list failed: %v (stdout=%s stderr=%s)",
91 err, cmd.Stdoutscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Stdout., cmd.Stderrscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Stderr.)
92 }
93 })
94 })
95 }
96 }
97
98 func testRmdirAfterGoListscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand_test`/testRmdirAfterGoList().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, f func(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, dir string)) {
99 testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsExecscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsExec().(t)
100
101 dir := t.TempDirscip-go gomod github.com/golang/go/src go1.25.0 testing/common#TempDir().()
102 if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Mkdirscip-go gomod github.com/golang/go/src go1.25.0 os/Mkdir().(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(dir, "p"), 0777); err != nil {
103 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#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 := osscip-go gomod github.com/golang/go/src go1.25.0 os/.WriteFilescip-go gomod github.com/golang/go/src go1.25.0 os/WriteFile().(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(dir, "go.mod"), []byte("module example.com"), 0666); err != nil {
108 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
109 }
110 for i := range 100 {
111 filename := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(dir, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("p/%d.go", i))
112 if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.WriteFilescip-go gomod github.com/golang/go/src go1.25.0 os/WriteFile().(filename, []byte("package p"), 0666); err != nil {
113 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
114 }
115 }
116
117 t0 := timescip-go gomod github.com/golang/go/src go1.25.0 time/.Nowscip-go gomod github.com/golang/go/src go1.25.0 time/Now().()
118 g, ctx := errgroupscip-go gomod golang.org/x/sync v0.20.0 `golang.org/x/sync/errgroup`/.WithContextscip-go gomod golang.org/x/sync v0.20.0 `golang.org/x/sync/errgroup`/WithContext().(contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Backgroundscip-go gomod github.com/golang/go/src go1.25.0 context/Background().())
119 for range 10 {
120 g.Goscip-go gomod golang.org/x/sync v0.20.0 `golang.org/x/sync/errgroup`/Group#Go().(func() error {
121 f(ctx, dir)
122 // Return an error so that concurrent invocations are canceled.
123 return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("oops")
124 })
125 }
126 g.Waitscip-go gomod golang.org/x/sync v0.20.0 `golang.org/x/sync/errgroup`/Group#Wait().() // ignore error (expected)
127
128 t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("10 concurrent executions (9 canceled) took %v", timescip-go gomod github.com/golang/go/src go1.25.0 time/.Sincescip-go gomod github.com/golang/go/src go1.25.0 time/Since().(t0))
129
130 // This is the critical operation.
131 if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.RemoveAllscip-go gomod github.com/golang/go/src go1.25.0 os/RemoveAll().(dir); err != nil {
132 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("failed to remove temp dir: %v", err)
133 // List the contents of the directory, for clues.
134 filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.WalkDirscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/WalkDir().(dir, func(path string, d fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.DirEntryscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/DirEntry#, err error) error {
135 t.Logscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Log().(path, d, err)
136 return nil
137 }) // ignore error
138 }
139 }