Skip to content

Commit 55cbead

Browse files
authored
Forbid platform specific package uses in agnostic files (microsoft#1911)
1 parent 0a61d1a commit 55cbead

File tree

5 files changed

+42
-14
lines changed

5 files changed

+42
-14
lines changed

.golangci.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ linters:
2222
- errname
2323
- errorlint
2424
- fatcontext
25+
- forbidigo
2526
- gocheckcompilerdirectives
2627
- goprintffuncname
2728
- govet
@@ -70,11 +71,30 @@ linters:
7071
- pkg: 'encoding/json$'
7172
desc: 'Use "github.com/go-json-experiment/json" instead.'
7273

74+
forbidigo:
75+
analyze-types: true
76+
forbid:
77+
- pattern: '.*'
78+
msg: tspath should likely be used instead
79+
pkg: ^(path|path/filepath)$
80+
- pattern: '.*'
81+
msg: a host implementation should likely be used instead
82+
pkg: ^os/
83+
- pattern: 'GOOS'
84+
msg: a host implementation should likely be used instead
85+
pkg: ^runtime$
86+
7387
exclusions:
7488
rules:
7589
- path: internal/fourslash/tests/gen/
7690
linters:
7791
- misspell
92+
- path: 'internal/(repo|testutil|testrunner|vfs|pprof|execute/tsctests|bundled)|cmd/tsgo'
93+
text: should likely be used instead
94+
- path: '(.+)_test\.go$'
95+
text: should likely be used instead
96+
- path: '_tools'
97+
text: should likely be used instead
7898

7999
presets:
80100
- comments

cmd/tsgo/lsp.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package main
22

33
import (
4+
"context"
45
"flag"
56
"fmt"
67
"os"
8+
"os/exec"
9+
"os/signal"
710
"runtime"
11+
"syscall"
812

913
"github.com/microsoft/typescript-go/internal/bundled"
1014
"github.com/microsoft/typescript-go/internal/core"
@@ -49,9 +53,17 @@ func runLSP(args []string) int {
4953
FS: fs,
5054
DefaultLibraryPath: defaultLibraryPath,
5155
TypingsLocation: typingsLocation,
56+
NpmInstall: func(cwd string, args []string) ([]byte, error) {
57+
cmd := exec.Command("npm", args...)
58+
cmd.Dir = cwd
59+
return cmd.Output()
60+
},
5261
})
5362

54-
if err := s.Run(); err != nil {
63+
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
64+
defer stop()
65+
66+
if err := s.Run(ctx); err != nil {
5567
return 1
5668
}
5769
return 0

internal/fourslash/fourslash.go

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

33
import (
4+
"context"
45
"fmt"
56
"io"
67
"maps"
@@ -164,7 +165,7 @@ func NewFourslash(t *testing.T, capabilities *lsproto.ClientCapabilities, conten
164165
defer func() {
165166
outputWriter.Close()
166167
}()
167-
err := server.Run()
168+
err := server.Run(context.TODO())
168169
if err != nil {
169170
t.Error("server error:", err)
170171
}

internal/lsp/server.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@ import (
55
"errors"
66
"fmt"
77
"io"
8-
"os"
9-
"os/exec"
10-
"os/signal"
118
"runtime/debug"
129
"slices"
1310
"sync"
1411
"sync/atomic"
15-
"syscall"
1612
"time"
1713

1814
"github.com/go-json-experiment/json"
@@ -39,6 +35,7 @@ type ServerOptions struct {
3935
DefaultLibraryPath string
4036
TypingsLocation string
4137
ParseCache *project.ParseCache
38+
NpmInstall func(cwd string, args []string) ([]byte, error)
4239
}
4340

4441
func NewServer(opts *ServerOptions) *Server {
@@ -59,6 +56,7 @@ func NewServer(opts *ServerOptions) *Server {
5956
defaultLibraryPath: opts.DefaultLibraryPath,
6057
typingsLocation: opts.TypingsLocation,
6158
parseCache: opts.ParseCache,
59+
npmInstall: opts.NpmInstall,
6260
}
6361
}
6462

@@ -157,6 +155,8 @@ type Server struct {
157155
compilerOptionsForInferredProjects *core.CompilerOptions
158156
// parseCache can be passed in so separate tests can share ASTs
159157
parseCache *project.ParseCache
158+
159+
npmInstall func(cwd string, args []string) ([]byte, error)
160160
}
161161

162162
// WatchFiles implements project.Client.
@@ -218,10 +218,7 @@ func (s *Server) RefreshDiagnostics(ctx context.Context) error {
218218
return nil
219219
}
220220

221-
func (s *Server) Run() error {
222-
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
223-
defer stop()
224-
221+
func (s *Server) Run(ctx context.Context) error {
225222
g, ctx := errgroup.WithContext(ctx)
226223
g.Go(func() error { return s.dispatchLoop(ctx) })
227224
g.Go(func() error { return s.writeLoop(ctx) })
@@ -877,9 +874,7 @@ func (s *Server) SetCompilerOptionsForInferredProjects(ctx context.Context, opti
877874

878875
// NpmInstall implements ata.NpmExecutor
879876
func (s *Server) NpmInstall(cwd string, args []string) ([]byte, error) {
880-
cmd := exec.Command("npm", args...)
881-
cmd.Dir = cwd
882-
return cmd.Output()
877+
return s.npmInstall(cwd, args)
883878
}
884879

885880
func isBlockingMethod(method lsproto.Method) bool {

internal/tspath/extension.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func RemoveFileExtension(path string) string {
5151
}
5252
}
5353
// Otherwise just remove single dot extension, if any
54-
return path[:len(path)-len(filepath.Ext(path))]
54+
return path[:len(path)-len(filepath.Ext(path))] //nolint:forbidigo
5555
}
5656

5757
func TryGetExtensionFromPath(p string) string {

0 commit comments

Comments
 (0)