Skip to content

Commit c17402c

Browse files
findleyrgopherbot
authored andcommitted
gopls: fix a couple places where temporary files are not removed
Fix two places where gopls fails to clean up temporary files: - In telemetry tests, the deferred cleanup was not run after os.Exit. - In the GC details codelens, a persistent GC details directory is assumed; just use a temp directory instead. Change-Id: Icef5a4b612ac1727fee7d2c65e99a90f73123081 Reviewed-on: https://go-review.googlesource.com/c/tools/+/579755 Reviewed-by: Alan Donovan <[email protected]> Auto-Submit: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent ee61fb0 commit c17402c

File tree

14 files changed

+56
-36
lines changed

14 files changed

+56
-36
lines changed

gopls/internal/golang/gc_annotations.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"golang.org/x/tools/gopls/internal/cache/metadata"
1818
"golang.org/x/tools/gopls/internal/protocol"
1919
"golang.org/x/tools/gopls/internal/settings"
20+
"golang.org/x/tools/internal/event"
2021
"golang.org/x/tools/internal/gocommand"
2122
)
2223

@@ -25,11 +26,16 @@ func GCOptimizationDetails(ctx context.Context, snapshot *cache.Snapshot, mp *me
2526
return nil, nil
2627
}
2728
pkgDir := filepath.Dir(mp.CompiledGoFiles[0].Path())
28-
outDir := filepath.Join(os.TempDir(), fmt.Sprintf("gopls-%d.details", os.Getpid()))
29-
30-
if err := os.MkdirAll(outDir, 0700); err != nil {
29+
outDir, err := os.MkdirTemp("", fmt.Sprintf("gopls-%d.details", os.Getpid()))
30+
if err != nil {
3131
return nil, err
3232
}
33+
defer func() {
34+
if err := os.RemoveAll(outDir); err != nil {
35+
event.Error(ctx, "cleaning gcdetails dir", err)
36+
}
37+
}()
38+
3339
tmpFile, err := os.CreateTemp(os.TempDir(), "gopls-x")
3440
if err != nil {
3541
return nil, err

gopls/internal/telemetry/telemetry_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ func TestMain(m *testing.M) {
3131
panic(err)
3232
}
3333
countertest.Open(tmp)
34-
defer os.RemoveAll(tmp)
35-
Main(m)
34+
code := Main(m)
35+
os.RemoveAll(tmp)
36+
os.Exit(code)
3637
}
3738

3839
func TestTelemetry(t *testing.T) {

gopls/internal/test/integration/codelens/codelens_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package codelens
66

77
import (
88
"fmt"
9+
"os"
910
"testing"
1011

1112
"golang.org/x/tools/gopls/internal/server"
@@ -20,7 +21,7 @@ import (
2021

2122
func TestMain(m *testing.M) {
2223
bug.PanicOnBugs = true
23-
Main(m)
24+
os.Exit(Main(m))
2425
}
2526

2627
func TestDisablingCodeLens(t *testing.T) {

gopls/internal/test/integration/completion/completion_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package completion
66

77
import (
88
"fmt"
9+
"os"
910
"sort"
1011
"strings"
1112
"testing"
@@ -24,7 +25,7 @@ import (
2425

2526
func TestMain(m *testing.M) {
2627
bug.PanicOnBugs = true
27-
Main(m)
28+
os.Exit(Main(m))
2829
}
2930

3031
const proxy = `

gopls/internal/test/integration/debug/debug_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"encoding/json"
1010
"io"
1111
"net/http"
12+
"os"
1213
"strings"
1314
"testing"
1415

@@ -19,7 +20,7 @@ import (
1920
)
2021

2122
func TestMain(m *testing.M) {
22-
Main(m)
23+
os.Exit(Main(m))
2324
}
2425

2526
func TestBugNotification(t *testing.T) {

gopls/internal/test/integration/diagnostics/diagnostics_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package diagnostics
77
import (
88
"context"
99
"fmt"
10+
"os"
1011
"os/exec"
1112
"testing"
1213

@@ -20,7 +21,7 @@ import (
2021

2122
func TestMain(m *testing.M) {
2223
bug.PanicOnBugs = true
23-
Main(m)
24+
os.Exit(Main(m))
2425
}
2526

2627
// Use mod.com for all go.mod files due to golang/go#35230.

gopls/internal/test/integration/doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
// )
3434
//
3535
// func TestMain(m *testing.M) {
36-
// Main(m, hooks.Options)
36+
// os.Exit(Main(m, hooks.Options))
3737
// }
3838
//
3939
// # Writing a simple integration test

gopls/internal/test/integration/inlayhints/inlayhints_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package inlayhint
55

66
import (
7+
"os"
78
"testing"
89

910
"golang.org/x/tools/gopls/internal/golang"
@@ -13,7 +14,7 @@ import (
1314

1415
func TestMain(m *testing.M) {
1516
bug.PanicOnBugs = true
16-
Main(m)
17+
os.Exit(Main(m))
1718
}
1819

1920
func TestEnablingInlayHints(t *testing.T) {

gopls/internal/test/integration/misc/misc_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package misc
66

77
import (
8+
"os"
89
"strings"
910
"testing"
1011

@@ -16,7 +17,7 @@ import (
1617

1718
func TestMain(m *testing.M) {
1819
bug.PanicOnBugs = true
19-
integration.Main(m)
20+
os.Exit(integration.Main(m))
2021
}
2122

2223
// TestDocumentURIFix ensures that a DocumentURI supplied by the

gopls/internal/test/integration/modfile/modfile_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package modfile
66

77
import (
8+
"os"
89
"path/filepath"
910
"runtime"
1011
"strings"
@@ -19,7 +20,7 @@ import (
1920

2021
func TestMain(m *testing.M) {
2122
bug.PanicOnBugs = true
22-
Main(m)
23+
os.Exit(Main(m))
2324
}
2425

2526
const workspaceProxy = `

gopls/internal/test/integration/regtest.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,24 @@ func DefaultModes() Mode {
111111
var runFromMain = false // true if Main has been called
112112

113113
// Main sets up and tears down the shared integration test state.
114-
func Main(m *testing.M) {
114+
func Main(m *testing.M) (code int) {
115+
defer func() {
116+
if runner != nil {
117+
if err := runner.Close(); err != nil {
118+
fmt.Fprintf(os.Stderr, "closing test runner: %v\n", err)
119+
// Cleanup is broken in go1.12 and earlier, and sometimes flakes on
120+
// Windows due to file locking, but this is OK for our CI.
121+
//
122+
// Fail on go1.13+, except for windows and android which have shutdown problems.
123+
if testenv.Go1Point() >= 13 && runtime.GOOS != "windows" && runtime.GOOS != "android" {
124+
if code == 0 {
125+
code = 1
126+
}
127+
}
128+
}
129+
}
130+
}()
131+
115132
runFromMain = true
116133

117134
// golang/go#54461: enable additional debugging around hanging Go commands.
@@ -121,12 +138,12 @@ func Main(m *testing.M) {
121138
// suite. See the documentation for runTestAsGoplsEnvvar for more details.
122139
if os.Getenv(runTestAsGoplsEnvvar) == "true" {
123140
tool.Main(context.Background(), cmd.New(), os.Args[1:])
124-
os.Exit(0)
141+
return 0
125142
}
126143

127144
if !testenv.HasExec() {
128145
fmt.Printf("skipping all tests: exec not supported on %s/%s\n", runtime.GOOS, runtime.GOARCH)
129-
os.Exit(0)
146+
return 0
130147
}
131148
testenv.ExitIfSmallMachine()
132149

@@ -137,12 +154,12 @@ func Main(m *testing.M) {
137154

138155
if skipReason := checkBuilder(); skipReason != "" {
139156
fmt.Printf("Skipping all tests: %s\n", skipReason)
140-
os.Exit(0)
157+
return 0
141158
}
142159

143160
if err := testenv.HasTool("go"); err != nil {
144161
fmt.Println("Missing go command")
145-
os.Exit(1)
162+
return 1
146163
}
147164

148165
runner = &Runner{
@@ -168,19 +185,5 @@ func Main(m *testing.M) {
168185
}
169186
runner.tempDir = dir
170187

171-
var code int
172-
defer func() {
173-
if err := runner.Close(); err != nil {
174-
fmt.Fprintf(os.Stderr, "closing test runner: %v\n", err)
175-
// Cleanup is broken in go1.12 and earlier, and sometimes flakes on
176-
// Windows due to file locking, but this is OK for our CI.
177-
//
178-
// Fail on go1.13+, except for windows and android which have shutdown problems.
179-
if testenv.Go1Point() >= 13 && runtime.GOOS != "windows" && runtime.GOOS != "android" {
180-
os.Exit(1)
181-
}
182-
}
183-
os.Exit(code)
184-
}()
185-
code = m.Run()
188+
return m.Run()
186189
}

gopls/internal/test/integration/template/template_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package template
66

77
import (
8+
"os"
89
"strings"
910
"testing"
1011

@@ -15,7 +16,7 @@ import (
1516

1617
func TestMain(m *testing.M) {
1718
bug.PanicOnBugs = true
18-
Main(m)
19+
os.Exit(Main(m))
1920
}
2021

2122
func TestMultilineTokens(t *testing.T) {

gopls/internal/test/integration/watch/watch_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package watch
66

77
import (
8+
"os"
89
"testing"
910

1011
. "golang.org/x/tools/gopls/internal/test/integration"
@@ -16,7 +17,7 @@ import (
1617

1718
func TestMain(m *testing.M) {
1819
bug.PanicOnBugs = true
19-
Main(m)
20+
os.Exit(Main(m))
2021
}
2122

2223
func TestEditFile(t *testing.T) {

gopls/internal/test/integration/workspace/workspace_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package workspace
77
import (
88
"context"
99
"fmt"
10+
"os"
1011
"sort"
1112
"strings"
1213
"testing"
@@ -24,7 +25,7 @@ import (
2425

2526
func TestMain(m *testing.M) {
2627
bug.PanicOnBugs = true
27-
Main(m)
28+
os.Exit(Main(m))
2829
}
2930

3031
const workspaceProxy = `

0 commit comments

Comments
 (0)