Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ linters:
- errname
- errorlint
- fatcontext
- forbidigo
- gocheckcompilerdirectives
- goprintffuncname
- govet
Expand Down Expand Up @@ -70,11 +71,30 @@ linters:
- pkg: 'encoding/json$'
desc: 'Use "github.com/go-json-experiment/json" instead.'

forbidigo:
analyze-types: true
forbid:
- pattern: '.*'
msg: tspath should likely be used instead
pkg: ^(path|path/filepath)$
- pattern: '.*'
msg: a host implementation should likely be used instead
pkg: ^os/
- pattern: 'GOOS'
msg: a host implementation should likely be used instead
pkg: ^runtime$

exclusions:
rules:
- path: internal/fourslash/tests/gen/
linters:
- misspell
- path: 'internal/(repo|testutil|testrunner|vfs|pprof|execute/tsctests|bundled)|cmd/tsgo'
text: should likely be used instead
- path: '(.+)_test\.go$'
text: should likely be used instead
- path: '_tools'
text: should likely be used instead

presets:
- comments
Expand Down
14 changes: 13 additions & 1 deletion cmd/tsgo/lsp.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package main

import (
"context"
"flag"
"fmt"
"os"
"os/exec"
"os/signal"
"runtime"
"syscall"

"github.com/microsoft/typescript-go/internal/bundled"
"github.com/microsoft/typescript-go/internal/core"
Expand Down Expand Up @@ -49,9 +53,17 @@ func runLSP(args []string) int {
FS: fs,
DefaultLibraryPath: defaultLibraryPath,
TypingsLocation: typingsLocation,
NpmInstall: func(cwd string, args []string) ([]byte, error) {
cmd := exec.Command("npm", args...)
cmd.Dir = cwd
Comment on lines +57 to +58
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit of a hack but I prefer this greatly to there being any chance that tests run npm install.

return cmd.Output()
},
})

if err := s.Run(); err != nil {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()

if err := s.Run(ctx); err != nil {
return 1
}
return 0
Expand Down
3 changes: 2 additions & 1 deletion internal/fourslash/fourslash.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fourslash

import (
"context"
"fmt"
"io"
"maps"
Expand Down Expand Up @@ -164,7 +165,7 @@ func NewFourslash(t *testing.T, capabilities *lsproto.ClientCapabilities, conten
defer func() {
outputWriter.Close()
}()
err := server.Run()
err := server.Run(context.TODO())
if err != nil {
t.Error("server error:", err)
}
Expand Down
17 changes: 6 additions & 11 deletions internal/lsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ import (
"errors"
"fmt"
"io"
"os"
"os/exec"
"os/signal"
"runtime/debug"
"slices"
"sync"
"sync/atomic"
"syscall"
"time"

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

func NewServer(opts *ServerOptions) *Server {
Expand All @@ -59,6 +56,7 @@ func NewServer(opts *ServerOptions) *Server {
defaultLibraryPath: opts.DefaultLibraryPath,
typingsLocation: opts.TypingsLocation,
parseCache: opts.ParseCache,
npmInstall: opts.NpmInstall,
}
}

Expand Down Expand Up @@ -157,6 +155,8 @@ type Server struct {
compilerOptionsForInferredProjects *core.CompilerOptions
// parseCache can be passed in so separate tests can share ASTs
parseCache *project.ParseCache

npmInstall func(cwd string, args []string) ([]byte, error)
}

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

func (s *Server) Run() error {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()

func (s *Server) Run(ctx context.Context) error {
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error { return s.dispatchLoop(ctx) })
g.Go(func() error { return s.writeLoop(ctx) })
Expand Down Expand Up @@ -877,9 +874,7 @@ func (s *Server) SetCompilerOptionsForInferredProjects(ctx context.Context, opti

// NpmInstall implements ata.NpmExecutor
func (s *Server) NpmInstall(cwd string, args []string) ([]byte, error) {
cmd := exec.Command("npm", args...)
cmd.Dir = cwd
return cmd.Output()
return s.npmInstall(cwd, args)
}

func isBlockingMethod(method lsproto.Method) bool {
Expand Down
2 changes: 1 addition & 1 deletion internal/tspath/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func RemoveFileExtension(path string) string {
}
}
// Otherwise just remove single dot extension, if any
return path[:len(path)-len(filepath.Ext(path))]
return path[:len(path)-len(filepath.Ext(path))] //nolint:forbidigo
}

func TryGetExtensionFromPath(p string) string {
Expand Down