Skip to content

Commit fad87fa

Browse files
committed
Insert pnpApi in Host for LSP and CLI usages
1 parent 134f3dc commit fad87fa

37 files changed

+779
-37
lines changed

cmd/tsgo/sys.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import (
88

99
"github.com/microsoft/typescript-go/internal/bundled"
1010
"github.com/microsoft/typescript-go/internal/execute/tsc"
11+
"github.com/microsoft/typescript-go/internal/pnp"
1112
"github.com/microsoft/typescript-go/internal/tspath"
1213
"github.com/microsoft/typescript-go/internal/vfs"
1314
"github.com/microsoft/typescript-go/internal/vfs/osvfs"
15+
"github.com/microsoft/typescript-go/internal/vfs/pnpvfs"
1416
"golang.org/x/term"
1517
)
1618

@@ -20,6 +22,7 @@ type osSys struct {
2022
defaultLibraryPath string
2123
cwd string
2224
start time.Time
25+
pnpApi *pnp.PnpApi
2326
}
2427

2528
func (s *osSys) SinceStart() time.Duration {
@@ -59,18 +62,30 @@ func (s *osSys) GetEnvironmentVariable(name string) string {
5962
return os.Getenv(name)
6063
}
6164

65+
func (s *osSys) PnpApi() *pnp.PnpApi {
66+
return s.pnpApi
67+
}
68+
6269
func newSystem() *osSys {
6370
cwd, err := os.Getwd()
6471
if err != nil {
6572
fmt.Fprintf(os.Stderr, "Error getting current directory: %v\n", err)
6673
os.Exit(int(tsc.ExitStatusInvalidProject_OutputsSkipped))
6774
}
6875

76+
var fs vfs.FS = osvfs.FS()
77+
78+
pnpApi := pnp.InitPnpApi(fs, tspath.NormalizePath(cwd))
79+
if pnpApi != nil {
80+
fs = pnpvfs.From(fs)
81+
}
82+
6983
return &osSys{
7084
cwd: tspath.NormalizePath(cwd),
71-
fs: bundled.WrapFS(osvfs.FS()),
85+
fs: bundled.WrapFS(fs),
7286
defaultLibraryPath: bundled.LibPath(),
7387
writer: os.Stdout,
7488
start: time.Now(),
89+
pnpApi: pnpApi,
7590
}
7691
}

internal/api/server.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ import (
1515
"github.com/microsoft/typescript-go/internal/bundled"
1616
"github.com/microsoft/typescript-go/internal/core"
1717
"github.com/microsoft/typescript-go/internal/lsp/lsproto"
18+
"github.com/microsoft/typescript-go/internal/pnp"
1819
"github.com/microsoft/typescript-go/internal/project"
1920
"github.com/microsoft/typescript-go/internal/project/logging"
2021
"github.com/microsoft/typescript-go/internal/vfs"
2122
"github.com/microsoft/typescript-go/internal/vfs/osvfs"
23+
"github.com/microsoft/typescript-go/internal/vfs/pnpvfs"
2224
)
2325

2426
//go:generate go tool golang.org/x/tools/cmd/stringer -type=MessageType -output=stringer_generated.go
@@ -93,12 +95,19 @@ func NewServer(options *ServerOptions) *Server {
9395
panic("Cwd is required")
9496
}
9597

98+
var fs vfs.FS = osvfs.FS()
99+
100+
pnpApi := pnp.InitPnpApi(fs, options.Cwd)
101+
if pnpApi != nil {
102+
fs = pnpvfs.From(fs)
103+
}
104+
96105
server := &Server{
97106
r: bufio.NewReader(options.In),
98107
w: bufio.NewWriter(options.Out),
99108
stderr: options.Err,
100109
cwd: options.Cwd,
101-
fs: bundled.WrapFS(osvfs.FS()),
110+
fs: bundled.WrapFS(fs),
102111
defaultLibraryPath: options.DefaultLibraryPath,
103112
}
104113
logger := logging.NewLogger(options.Err)

internal/checker/checker_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ foo.bar;`
3636
fs = bundled.WrapFS(fs)
3737

3838
cd := "/"
39-
host := compiler.NewCompilerHost(cd, fs, bundled.LibPath(), nil, nil)
39+
host := compiler.NewCompilerHost(cd, fs, bundled.LibPath(), nil, nil, nil)
4040

4141
parsed, errors := tsoptions.GetParsedCommandLineOfConfigFile("/tsconfig.json", &core.CompilerOptions{}, host, nil)
4242
assert.Equal(t, len(errors), 0, "Expected no errors in parsed command line")
@@ -70,7 +70,7 @@ func TestCheckSrcCompiler(t *testing.T) {
7070

7171
rootPath := tspath.CombinePaths(tspath.NormalizeSlashes(repo.TypeScriptSubmodulePath), "src", "compiler")
7272

73-
host := compiler.NewCompilerHost(rootPath, fs, bundled.LibPath(), nil, nil)
73+
host := compiler.NewCompilerHost(rootPath, fs, bundled.LibPath(), nil, nil, nil)
7474
parsed, errors := tsoptions.GetParsedCommandLineOfConfigFile(tspath.CombinePaths(rootPath, "tsconfig.json"), &core.CompilerOptions{}, host, nil)
7575
assert.Equal(t, len(errors), 0, "Expected no errors in parsed command line")
7676
p := compiler.NewProgram(compiler.ProgramOptions{
@@ -87,7 +87,7 @@ func BenchmarkNewChecker(b *testing.B) {
8787

8888
rootPath := tspath.CombinePaths(tspath.NormalizeSlashes(repo.TypeScriptSubmodulePath), "src", "compiler")
8989

90-
host := compiler.NewCompilerHost(rootPath, fs, bundled.LibPath(), nil, nil)
90+
host := compiler.NewCompilerHost(rootPath, fs, bundled.LibPath(), nil, nil, nil)
9191
parsed, errors := tsoptions.GetParsedCommandLineOfConfigFile(tspath.CombinePaths(rootPath, "tsconfig.json"), &core.CompilerOptions{}, host, nil)
9292
assert.Equal(b, len(errors), 0, "Expected no errors in parsed command line")
9393
p := compiler.NewProgram(compiler.ProgramOptions{

internal/compiler/emitHost.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/microsoft/typescript-go/internal/module"
99
"github.com/microsoft/typescript-go/internal/modulespecifiers"
1010
"github.com/microsoft/typescript-go/internal/outputpaths"
11+
"github.com/microsoft/typescript-go/internal/pnp"
1112
"github.com/microsoft/typescript-go/internal/printer"
1213
"github.com/microsoft/typescript-go/internal/transformers/declarations"
1314
"github.com/microsoft/typescript-go/internal/tsoptions"
@@ -24,6 +25,7 @@ type EmitHost interface {
2425
GetCurrentDirectory() string
2526
CommonSourceDirectory() string
2627
IsEmitBlocked(file string) bool
28+
PnpApi() *pnp.PnpApi
2729
}
2830

2931
var _ EmitHost = (*emitHost)(nil)
@@ -107,6 +109,7 @@ func (host *emitHost) Options() *core.CompilerOptions { return host.program.Opti
107109
func (host *emitHost) SourceFiles() []*ast.SourceFile { return host.program.SourceFiles() }
108110
func (host *emitHost) GetCurrentDirectory() string { return host.program.GetCurrentDirectory() }
109111
func (host *emitHost) CommonSourceDirectory() string { return host.program.CommonSourceDirectory() }
112+
func (host *emitHost) PnpApi() *pnp.PnpApi { return host.program.PnpApi() }
110113
func (host *emitHost) UseCaseSensitiveFileNames() bool {
111114
return host.program.UseCaseSensitiveFileNames()
112115
}

internal/compiler/host.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/microsoft/typescript-go/internal/ast"
55
"github.com/microsoft/typescript-go/internal/core"
66
"github.com/microsoft/typescript-go/internal/parser"
7+
"github.com/microsoft/typescript-go/internal/pnp"
78
"github.com/microsoft/typescript-go/internal/tsoptions"
89
"github.com/microsoft/typescript-go/internal/tspath"
910
"github.com/microsoft/typescript-go/internal/vfs"
@@ -17,6 +18,7 @@ type CompilerHost interface {
1718
Trace(msg string)
1819
GetSourceFile(opts ast.SourceFileParseOptions) *ast.SourceFile
1920
GetResolvedProjectReference(fileName string, path tspath.Path) *tsoptions.ParsedCommandLine
21+
PnpApi() *pnp.PnpApi
2022
}
2123

2224
var _ CompilerHost = (*compilerHost)(nil)
@@ -27,34 +29,39 @@ type compilerHost struct {
2729
defaultLibraryPath string
2830
extendedConfigCache tsoptions.ExtendedConfigCache
2931
trace func(msg string)
32+
pnpApi *pnp.PnpApi
3033
}
3134

3235
func NewCachedFSCompilerHost(
3336
currentDirectory string,
3437
fs vfs.FS,
3538
defaultLibraryPath string,
3639
extendedConfigCache tsoptions.ExtendedConfigCache,
40+
pnpApi *pnp.PnpApi,
3741
trace func(msg string),
3842
) CompilerHost {
39-
return NewCompilerHost(currentDirectory, cachedvfs.From(fs), defaultLibraryPath, extendedConfigCache, trace)
43+
return NewCompilerHost(currentDirectory, cachedvfs.From(fs), defaultLibraryPath, extendedConfigCache, pnpApi, trace)
4044
}
4145

4246
func NewCompilerHost(
4347
currentDirectory string,
4448
fs vfs.FS,
4549
defaultLibraryPath string,
4650
extendedConfigCache tsoptions.ExtendedConfigCache,
51+
pnpApi *pnp.PnpApi,
4752
trace func(msg string),
4853
) CompilerHost {
4954
if trace == nil {
5055
trace = func(msg string) {}
5156
}
57+
5258
return &compilerHost{
5359
currentDirectory: currentDirectory,
5460
fs: fs,
5561
defaultLibraryPath: defaultLibraryPath,
5662
extendedConfigCache: extendedConfigCache,
5763
trace: trace,
64+
pnpApi: pnpApi,
5865
}
5966
}
6067

@@ -70,6 +77,10 @@ func (h *compilerHost) GetCurrentDirectory() string {
7077
return h.currentDirectory
7178
}
7279

80+
func (h *compilerHost) PnpApi() *pnp.PnpApi {
81+
return h.pnpApi
82+
}
83+
7384
func (h *compilerHost) Trace(msg string) {
7485
h.trace(msg)
7586
}

internal/compiler/program.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/microsoft/typescript-go/internal/modulespecifiers"
2121
"github.com/microsoft/typescript-go/internal/outputpaths"
2222
"github.com/microsoft/typescript-go/internal/parser"
23+
"github.com/microsoft/typescript-go/internal/pnp"
2324
"github.com/microsoft/typescript-go/internal/printer"
2425
"github.com/microsoft/typescript-go/internal/scanner"
2526
"github.com/microsoft/typescript-go/internal/sourcemap"
@@ -78,6 +79,11 @@ func (p *Program) GetCurrentDirectory() string {
7879
return p.Host().GetCurrentDirectory()
7980
}
8081

82+
// PnpApi implements checker.Program.
83+
func (p *Program) PnpApi() *pnp.PnpApi {
84+
return p.Host().PnpApi()
85+
}
86+
8187
// GetGlobalTypingsCacheLocation implements checker.Program.
8288
func (p *Program) GetGlobalTypingsCacheLocation() string {
8389
return "" // !!! see src/tsserver/nodeServer.ts for strada's node-specific implementation

internal/compiler/program_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func TestProgram(t *testing.T) {
243243
CompilerOptions: &opts,
244244
},
245245
},
246-
Host: compiler.NewCompilerHost("c:/dev/src", fs, bundled.LibPath(), nil, nil),
246+
Host: compiler.NewCompilerHost("c:/dev/src", fs, bundled.LibPath(), nil, nil, nil),
247247
})
248248

249249
actualFiles := []string{}
@@ -280,7 +280,7 @@ func BenchmarkNewProgram(b *testing.B) {
280280
CompilerOptions: &opts,
281281
},
282282
},
283-
Host: compiler.NewCompilerHost("c:/dev/src", fs, bundled.LibPath(), nil, nil),
283+
Host: compiler.NewCompilerHost("c:/dev/src", fs, bundled.LibPath(), nil, nil, nil),
284284
}
285285

286286
for b.Loop() {
@@ -297,7 +297,7 @@ func BenchmarkNewProgram(b *testing.B) {
297297
fs := osvfs.FS()
298298
fs = bundled.WrapFS(fs)
299299

300-
host := compiler.NewCompilerHost(rootPath, fs, bundled.LibPath(), nil, nil)
300+
host := compiler.NewCompilerHost(rootPath, fs, bundled.LibPath(), nil, nil, nil)
301301

302302
parsed, errors := tsoptions.GetParsedCommandLineOfConfigFile(tspath.CombinePaths(rootPath, "tsconfig.json"), nil, host, nil)
303303
assert.Equal(b, len(errors), 0, "Expected no errors in parsed command line")

internal/compiler/projectreferencedtsfakinghost.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/microsoft/typescript-go/internal/collections"
88
"github.com/microsoft/typescript-go/internal/core"
99
"github.com/microsoft/typescript-go/internal/module"
10+
"github.com/microsoft/typescript-go/internal/pnp"
1011
"github.com/microsoft/typescript-go/internal/tspath"
1112
"github.com/microsoft/typescript-go/internal/vfs"
1213
"github.com/microsoft/typescript-go/internal/vfs/cachedvfs"
@@ -42,6 +43,11 @@ func (h *projectReferenceDtsFakingHost) GetCurrentDirectory() string {
4243
return h.host.GetCurrentDirectory()
4344
}
4445

46+
// PnpApi implements module.ResolutionHost.
47+
func (h *projectReferenceDtsFakingHost) PnpApi() *pnp.PnpApi {
48+
return h.host.PnpApi()
49+
}
50+
4551
type projectReferenceDtsFakingVfs struct {
4652
projectReferenceFileMapper *projectReferenceFileMapper
4753
dtsDirectories collections.Set[tspath.Path]

internal/core/compileroptions.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"sync"
77

88
"github.com/microsoft/typescript-go/internal/collections"
9+
"github.com/microsoft/typescript-go/internal/pnp"
910
"github.com/microsoft/typescript-go/internal/tspath"
1011
)
1112

@@ -300,7 +301,7 @@ func (options *CompilerOptions) GetStrictOptionValue(value Tristate) bool {
300301
return options.Strict == TSTrue
301302
}
302303

303-
func (options *CompilerOptions) GetEffectiveTypeRoots(currentDirectory string) (result []string, fromConfig bool) {
304+
func (options *CompilerOptions) GetEffectiveTypeRoots(currentDirectory string, pnpApi *pnp.PnpApi) (result []string, fromConfig bool) {
304305
if options.TypeRoots != nil {
305306
return options.TypeRoots, true
306307
}
@@ -316,6 +317,17 @@ func (options *CompilerOptions) GetEffectiveTypeRoots(currentDirectory string) (
316317
}
317318
}
318319

320+
nmTypes, nmFromConfig := options.GetNodeModulesTypeRoots(baseDir)
321+
322+
if pnpApi != nil {
323+
typeRoots, fromConfig := pnpApi.AppendPnpTypeRoots(nmTypes, baseDir, nmFromConfig)
324+
return typeRoots, fromConfig
325+
}
326+
327+
return nmTypes, nmFromConfig
328+
}
329+
330+
func (options *CompilerOptions) GetNodeModulesTypeRoots(baseDir string) (result []string, fromConfig bool) {
319331
typeRoots := make([]string, 0, strings.Count(baseDir, "/"))
320332
tspath.ForEachAncestorDirectory(baseDir, func(dir string) (any, bool) {
321333
typeRoots = append(typeRoots, tspath.CombinePaths(dir, "node_modules", "@types"))

internal/execute/build/compilerHost.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package build
33
import (
44
"github.com/microsoft/typescript-go/internal/ast"
55
"github.com/microsoft/typescript-go/internal/compiler"
6+
"github.com/microsoft/typescript-go/internal/pnp"
67
"github.com/microsoft/typescript-go/internal/tsoptions"
78
"github.com/microsoft/typescript-go/internal/tspath"
89
"github.com/microsoft/typescript-go/internal/vfs"
@@ -27,6 +28,10 @@ func (h *compilerHost) GetCurrentDirectory() string {
2728
return h.host.GetCurrentDirectory()
2829
}
2930

31+
func (h *compilerHost) PnpApi() *pnp.PnpApi {
32+
return h.host.PnpApi()
33+
}
34+
3035
func (h *compilerHost) Trace(msg string) {
3136
h.trace(msg)
3237
}

0 commit comments

Comments
 (0)