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 = `

0 commit comments

Comments
 (0)