Skip to content

Commit fe2fa17

Browse files
committed
WIP Initialize the pnp api at project level, allowing multiple pnp roots within a single LSP
1 parent d63a57e commit fe2fa17

14 files changed

Lines changed: 244 additions & 44 deletions

File tree

‎tsc/internal/api/server.go‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ import (
99
"github.com/microsoft/TypeScript/tsc/internal/contentmapper"
1010
"github.com/microsoft/TypeScript/tsc/internal/ipc"
1111
"github.com/microsoft/TypeScript/tsc/internal/lsp/lsproto"
12-
"github.com/microsoft/TypeScript/tsc/internal/pnp"
1312
"github.com/microsoft/TypeScript/tsc/internal/project"
1413
"github.com/microsoft/TypeScript/tsc/internal/vfs"
1514
"github.com/microsoft/TypeScript/tsc/internal/vfs/osvfs"
16-
"github.com/microsoft/TypeScript/tsc/internal/vfs/pnpvfs"
1715
)
1816

1917
// StdioServerOptions configures the STDIO-based API server.
@@ -79,11 +77,6 @@ func (s *StdioServer) Run(ctx context.Context) error {
7977

8078
var fs vfs.FS = osvfs.FS()
8179

82-
pnpApi := pnp.InitPnpApi(fs, s.options.Cwd)
83-
if pnpApi != nil {
84-
fs = pnpvfs.From(fs)
85-
}
86-
8780
fs = bundled.WrapFS(fs)
8881

8982
// Wrap the base FS with callbackFS if callbacks are requested
@@ -97,7 +90,6 @@ func (s *StdioServer) Run(ctx context.Context) error {
9790
BackgroundCtx: ctx,
9891
Logger: nil, // TODO: Add logging support
9992
FS: fs,
100-
PnpApi: pnpApi,
10193
Options: &project.SessionOptions{
10294
CurrentDirectory: s.options.Cwd,
10395
DefaultLibraryPath: s.options.DefaultLibraryPath,

‎tsc/internal/ls/autoimport/registry.go‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,8 +1230,18 @@ func (b *registryBuilder) buildProjectBucket(
12301230
var mu sync.Mutex
12311231
fileExcludePatterns := b.userPreferences.ParsedAutoImportFileExcludePatterns(b.host.FS().UseCaseSensitiveFileNames())
12321232
result.bucket = &RegistryBucket{}
1233-
moduleResolver := module.NewResolverWithOptions(b.host, core.EmptyCompilerOptions, "", "", b.resolverOptions)
12341233
program := b.host.GetProgramForProject(projectPath)
1234+
pnpApi := b.host.PnpApi()
1235+
if host, ok := b.host.(interface {
1236+
PnpApiForProject(tspath.Path) *pnp.PnpApi
1237+
}); ok {
1238+
pnpApi = host.PnpApiForProject(projectPath)
1239+
}
1240+
moduleResolver := module.NewResolverWithOptions(&resolutionHost{
1241+
fs: b.host.FS(),
1242+
currentDirectory: program.GetCurrentDirectory(),
1243+
pnpApi: pnpApi,
1244+
}, core.EmptyCompilerOptions, "", "", b.resolverOptions)
12351245
projectRootPath := b.base.toPath(program.GetCurrentDirectory())
12361246
symlinkCache := program.GetSymlinkCache()
12371247
getChecker, closePool, checkerCount := createCheckerPool(program)
@@ -1252,7 +1262,6 @@ func (b *registryBuilder) buildProjectBucket(
12521262

12531263
// If pnp is available, node_modules buckets won't be built as all packages are located in `.yarn/cache`
12541264
// This is why we need to handle all files, except the ones that are not importable in the project
1255-
pnpApi := b.host.PnpApi()
12561265
if pnpApi != nil {
12571266
if !pnpApi.IsImportable(string(projectPath), file.FileName()) {
12581267
continue

‎tsc/internal/lsp/server.go‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@ import (
3030
"github.com/microsoft/TypeScript/tsc/internal/ls/lsutil"
3131
"github.com/microsoft/TypeScript/tsc/internal/lsp/lsproto"
3232
"github.com/microsoft/TypeScript/tsc/internal/lsp/lspwatcher"
33-
"github.com/microsoft/TypeScript/tsc/internal/pnp"
3433
"github.com/microsoft/TypeScript/tsc/internal/pprof"
3534
"github.com/microsoft/TypeScript/tsc/internal/project"
3635
"github.com/microsoft/TypeScript/tsc/internal/project/ata"
3736
"github.com/microsoft/TypeScript/tsc/internal/tsoptions"
3837
"github.com/microsoft/TypeScript/tsc/internal/tspath"
3938
"github.com/microsoft/TypeScript/tsc/internal/vfs"
40-
"github.com/microsoft/TypeScript/tsc/internal/vfs/pnpvfs"
4139
"golang.org/x/sync/errgroup"
4240
)
4341

@@ -1729,11 +1727,6 @@ func (s *Server) handleInitialized(ctx context.Context, params *lsproto.Initiali
17291727
cwd = s.cwd
17301728
}
17311729

1732-
pnpApi := pnp.InitPnpApi(s.fs, cwd)
1733-
if pnpApi != nil {
1734-
s.fs = pnpvfs.From(s.fs)
1735-
}
1736-
17371730
s.telemetryEnabled = enableTelemetry
17381731

17391732
s.session = project.NewSession(&project.SessionInit{
@@ -1751,7 +1744,6 @@ func (s *Server) handleInitialized(ctx context.Context, params *lsproto.Initiali
17511744
RunExternalCode: runExternalCode,
17521745
},
17531746
FS: s.fs,
1754-
PnpApi: pnpApi,
17551747
Logger: s.logger,
17561748
Client: s,
17571749
NpmExecutor: s,

‎tsc/internal/modulespecifiers/specifiers.go‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,22 @@ import (
1212
"github.com/microsoft/TypeScript/tsc/internal/module"
1313
"github.com/microsoft/TypeScript/tsc/internal/outputpaths"
1414
"github.com/microsoft/TypeScript/tsc/internal/packagejson"
15+
"github.com/microsoft/TypeScript/tsc/internal/pnp"
1516
"github.com/microsoft/TypeScript/tsc/internal/stringutil"
1617
"github.com/microsoft/TypeScript/tsc/internal/tspath"
1718
)
1819

20+
type pnpApiForPathHost interface {
21+
PnpApiForPath(fileName string) *pnp.PnpApi
22+
}
23+
24+
func getPnpApi(host ModuleSpecifierGenerationHost, fileName string) *pnp.PnpApi {
25+
if host, ok := host.(pnpApiForPathHost); ok {
26+
return host.PnpApiForPath(fileName)
27+
}
28+
return host.PnpApi()
29+
}
30+
1931
func GetModuleSpecifiers(
2032
moduleSymbol *ast.Symbol,
2133
checker CheckerShape,
@@ -286,7 +298,7 @@ func GetEachFileNameOfModule(
286298
for _, p := range targets {
287299
if !(shouldFilterIgnoredPaths && containsIgnoredPath(p)) {
288300
IsInNodeModules := ContainsNodeModules(p)
289-
if pnpApi := host.PnpApi(); pnpApi != nil {
301+
if pnpApi := getPnpApi(host, importingFileName); pnpApi != nil {
290302
IsInNodeModules = IsInNodeModules || pnpApi.IsInPnpModule(p, importingFileName)
291303
}
292304

@@ -350,7 +362,7 @@ func GetEachFileNameOfModule(
350362
for _, p := range targets {
351363
if !(shouldFilterIgnoredPaths && containsIgnoredPath(p)) {
352364
IsInNodeModules := ContainsNodeModules(p)
353-
if pnpApi := host.PnpApi(); pnpApi != nil {
365+
if pnpApi := getPnpApi(host, importingFileName); pnpApi != nil {
354366
IsInNodeModules = IsInNodeModules || pnpApi.IsInPnpModule(p, importingFileName)
355367
}
356368

@@ -761,7 +773,7 @@ func tryGetModuleNameAsPnpPackage(
761773
packageNameOnly bool,
762774
overrideMode core.ResolutionMode,
763775
) string {
764-
pnpApi := host.PnpApi()
776+
pnpApi := getPnpApi(host, importingSourceFile.FileName())
765777
if pnpApi == nil {
766778
return ""
767779
}

‎tsc/internal/project/autoimport.go‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/microsoft/TypeScript/tsc/internal/pnp"
1212
"github.com/microsoft/TypeScript/tsc/internal/tspath"
1313
"github.com/microsoft/TypeScript/tsc/internal/vfs"
14+
"github.com/microsoft/TypeScript/tsc/internal/vfs/pnpvfs"
1415
)
1516

1617
type autoImportBuilderFS struct {
@@ -89,7 +90,7 @@ func newAutoImportRegistryCloneHost(
8990
return &autoImportRegistryCloneHost{
9091
projectCollection: projectCollection,
9192
parseCache: parseCache,
92-
fs: newSourceFS(false, &autoImportBuilderFS{snapshotFSBuilder: snapshotFSBuilder}, toPath),
93+
fs: newSourceFSWithFS(false, &autoImportBuilderFS{snapshotFSBuilder: snapshotFSBuilder}, toPath, pnpvfs.From(snapshotFSBuilder.fs)),
9394
pnpApi: pnpApi,
9495
currentDirectory: currentDirectory,
9596
}
@@ -110,6 +111,18 @@ func (a *autoImportRegistryCloneHost) PnpApi() *pnp.PnpApi {
110111
return a.pnpApi
111112
}
112113

114+
// PnpApiForProject returns the API selected for a particular project. The
115+
// RegistryCloneHost interface retains PnpApi for global callers that have no
116+
// project context, while project-index construction uses this method when it
117+
// is available.
118+
func (a *autoImportRegistryCloneHost) PnpApiForProject(projectPath tspath.Path) *pnp.PnpApi {
119+
project := a.projectCollection.GetProjectByPath(projectPath)
120+
if project == nil {
121+
return nil
122+
}
123+
return project.PnpApi()
124+
}
125+
113126
// GetDefaultProject implements autoimport.RegistryCloneHost.
114127
func (a *autoImportRegistryCloneHost) GetDefaultProject(path tspath.Path) (tspath.Path, *compiler.Program) {
115128
project := a.projectCollection.GetDefaultProject(path)

‎tsc/internal/project/compilerhost.go‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/microsoft/TypeScript/tsc/internal/tsoptions"
1414
"github.com/microsoft/TypeScript/tsc/internal/tspath"
1515
"github.com/microsoft/TypeScript/tsc/internal/vfs"
16+
"github.com/microsoft/TypeScript/tsc/internal/vfs/pnpvfs"
1617
"github.com/zeebo/xxh3"
1718
)
1819

@@ -40,13 +41,16 @@ func newCompilerHost(
4041
builder *ProjectCollectionBuilder,
4142
logger *logging.LogTree,
4243
) *compilerHost {
44+
// A rebuilt project gets a fresh immutable API from its own issuer
45+
// directory. Existing snapshots keep the API pointer they were built with.
46+
project.pnpApi = pnp.InitPnpApi(pnpvfs.From(builder.fs.fs), currentDirectory)
4347
return &compilerHost{
4448
configFilePath: project.configFilePath,
4549
currentDirectory: currentDirectory,
4650
sessionOptions: builder.sessionOptions,
4751

48-
pnpApi: builder.pnpApi,
49-
sourceFS: newSourceFS(true, builder.fs, builder.toPath),
52+
pnpApi: project.pnpApi,
53+
sourceFS: newSourceFSWithFS(true, builder.fs, builder.toPath, pnpvfs.From(builder.fs.fs)),
5054

5155
project: project,
5256
builder: builder,
@@ -61,6 +65,7 @@ func (c *compilerHost) freeze(snapshotFS *SnapshotFS, configFileRegistry *Config
6165
panic("freeze can only be called once")
6266
}
6367
c.sourceFS.source = snapshotFS
68+
c.sourceFS.fileSystem = pnpvfs.From(snapshotFS.fs)
6469
c.sourceFS.DisableTracking()
6570
c.configFileRegistry = configFileRegistry
6671
c.builder = nil

‎tsc/internal/project/configfileregistrybuilder.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/microsoft/TypeScript/tsc/internal/tsoptions"
1616
"github.com/microsoft/TypeScript/tsc/internal/tspath"
1717
"github.com/microsoft/TypeScript/tsc/internal/vfs"
18+
"github.com/microsoft/TypeScript/tsc/internal/vfs/pnpvfs"
1819
)
1920

2021
var (
@@ -57,7 +58,7 @@ func newConfigFileRegistryBuilder(
5758
) *configFileRegistryBuilder {
5859
return &configFileRegistryBuilder{
5960
hasRelativePatternCapability: hasRelativePatternCapability,
60-
fs: newSourceFS(false, fs, fs.toPath),
61+
fs: newSourceFSWithFS(false, fs, fs.toPath, pnpvfs.From(fs.fs)),
6162
isOpenFile: fs.isOpenFile,
6263
base: oldConfigFileRegistry,
6364
sessionOptions: sessionOptions,
@@ -782,6 +783,12 @@ func (c *configFileRegistryBuilder) PnpApi() *pnp.PnpApi {
782783
return c.pnpApi
783784
}
784785

786+
// PnpApiForPath selects the manifest for the config file's issuer directory.
787+
// PnpApi remains the session-level fallback for context-free consumers.
788+
func (c *configFileRegistryBuilder) PnpApiForPath(fileName string) *pnp.PnpApi {
789+
return pnp.InitPnpApi(c.fs, fileName)
790+
}
791+
785792
// GetExtendedConfig implements tsoptions.ExtendedConfigCache.
786793
func (c *configFileRegistryBuilder) GetExtendedConfig(fileName string, path tspath.Path, resolutionStack []tspath.Path, host tsoptions.ParseConfigHost) *tsoptions.ExtendedConfigCacheEntry {
787794
var content string

‎tsc/internal/project/project.go‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ import (
1414
"github.com/microsoft/TypeScript/tsc/internal/core"
1515
"github.com/microsoft/TypeScript/tsc/internal/ls"
1616
"github.com/microsoft/TypeScript/tsc/internal/lsp/lsproto"
17+
"github.com/microsoft/TypeScript/tsc/internal/pnp"
1718
"github.com/microsoft/TypeScript/tsc/internal/project/ata"
1819
"github.com/microsoft/TypeScript/tsc/internal/project/logging"
1920
"github.com/microsoft/TypeScript/tsc/internal/tsoptions"
2021
"github.com/microsoft/TypeScript/tsc/internal/tspath"
22+
"github.com/microsoft/TypeScript/tsc/internal/vfs/pnpvfs"
2123
)
2224

2325
const (
@@ -59,6 +61,7 @@ type Project struct {
5961
currentDirectory string
6062
configFileName string
6163
configFilePath tspath.Path
64+
pnpApi *pnp.PnpApi
6265

6366
dirty bool
6467
dirtyFilePath tspath.Path
@@ -165,6 +168,7 @@ func NewProject(
165168
currentDirectory: currentDirectory,
166169
dirty: true,
167170
}
171+
project.pnpApi = pnp.InitPnpApi(pnpvfs.From(builder.fs.fs), currentDirectory)
168172

169173
project.configFilePath = tspath.ToPath(configFileName, currentDirectory, builder.fs.fs.UseCaseSensitiveFileNames())
170174
project.programFilesWatch = NewWatchedFiles(
@@ -189,7 +193,7 @@ func NewProject(
189193
builder.sessionOptions.CurrentDirectory,
190194
builder.fs.fs.UseCaseSensitiveFileNames(),
191195
)
192-
if builder.pnpApi != nil {
196+
if project.pnpApi != nil {
193197
project.pnpManifestWatch = NewWatchedFiles(
194198
"pnp manifest files for "+configFileName,
195199
lsproto.WatchKindChange,
@@ -221,6 +225,11 @@ func (p *Project) ID() tspath.Path {
221225
return p.configFilePath
222226
}
223227

228+
// PnpApi returns the immutable PnP API selected for this project.
229+
func (p *Project) PnpApi() *pnp.PnpApi {
230+
return p.pnpApi
231+
}
232+
224233
// ConfigFileName panics if Kind() is not KindConfigured.
225234
func (p *Project) ConfigFileName() string {
226235
if p.Kind != KindConfigured {
@@ -278,6 +287,7 @@ func (p *Project) Clone() *Project {
278287
currentDirectory: p.currentDirectory,
279288
configFileName: p.configFileName,
280289
configFilePath: p.configFilePath,
290+
pnpApi: p.pnpApi,
281291

282292
dirty: p.dirty,
283293
dirtyFilePath: p.dirtyFilePath,

‎tsc/internal/project/projectcollectionbuilder.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,8 +1249,8 @@ func (b *ProjectCollectionBuilder) updateProgram(entry dirty.Value[*Project], lo
12491249
if result.UpdateKind == ProgramUpdateKindNewFiles {
12501250
filesChanged = true
12511251
project.programFilesWatch = project.CloneWatchers()
1252-
if project.pnpManifestWatch != nil && b.pnpApi != nil {
1253-
pnpManifestPath := b.pnpApi.GetManifestPath()
1252+
if project.pnpManifestWatch != nil && project.pnpApi != nil {
1253+
pnpManifestPath := project.pnpApi.GetManifestPath()
12541254
if pnpManifestPath != "" {
12551255
project.pnpManifestWatch = project.pnpManifestWatch.Clone(PatternsAndIgnored{
12561256
patternsInsideWorkspace: []string{pnpManifestPath},

‎tsc/internal/project/session.go‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/microsoft/TypeScript/tsc/internal/project/logging"
3333
"github.com/microsoft/TypeScript/tsc/internal/tspath"
3434
"github.com/microsoft/TypeScript/tsc/internal/vfs"
35+
"github.com/microsoft/TypeScript/tsc/internal/vfs/pnpvfs"
3536
)
3637

3738
type UpdateReason int
@@ -89,7 +90,6 @@ type SessionInit struct {
8990
Client Client
9091
Logger logging.Logger
9192
NpmExecutor ata.NpmExecutor
92-
PnpApi *pnp.PnpApi
9393
// Spawner launches content mapper processes. It is nil when the host cannot spawn processes.
9494
Spawner contentmapper.Spawner
9595
ContentMapperLogger contentmapper.Logger
@@ -233,11 +233,18 @@ func newContentMapperHost(init *SessionInit) contentmapper.Host {
233233

234234
func NewSession(init *SessionInit) *Session {
235235
currentDirectory := init.Options.CurrentDirectory
236-
useCaseSensitiveFileNames := init.FS.UseCaseSensitiveFileNames()
236+
fs := init.FS
237+
// Keep a cwd-based API for hosts that do not have a project context yet.
238+
// Project-specific hosts select their own API when the project is created.
239+
pnpApi := pnp.InitPnpApi(fs, currentDirectory)
240+
if pnpApi != nil {
241+
fs = pnpvfs.From(fs)
242+
}
243+
useCaseSensitiveFileNames := fs.UseCaseSensitiveFileNames()
237244
toPath := func(fileName string) tspath.Path {
238245
return tspath.ToPath(fileName, currentDirectory, useCaseSensitiveFileNames)
239246
}
240-
overlayFS := newOverlayFS(init.FS, make(map[tspath.Path]*Overlay), init.Options.PositionEncoding, toPath)
247+
overlayFS := newOverlayFS(fs, make(map[tspath.Path]*Overlay), init.Options.PositionEncoding, toPath)
241248
parseCache := init.ParseCache
242249
if parseCache == nil {
243250
parseCache = NewParseCache(RefCountCacheOptions{})
@@ -261,7 +268,7 @@ func NewSession(init *SessionInit) *Session {
261268
npmExecutor: init.NpmExecutor,
262269
contentMapperHost: newContentMapperHost(init),
263270
fs: overlayFS,
264-
pnpApi: init.PnpApi,
271+
pnpApi: pnpApi,
265272
parseCache: parseCache,
266273
contentMappedParseCache: contentMappedParseCache,
267274
extendedConfigCache: extendedConfigCache,
@@ -272,7 +279,7 @@ func NewSession(init *SessionInit) *Session {
272279
uint64(0),
273280
&SnapshotFS{
274281
toPath: toPath,
275-
fs: init.FS,
282+
fs: fs,
276283
},
277284
init.Options,
278285
&ConfigFileRegistry{},
@@ -295,7 +302,7 @@ func NewSession(init *SessionInit) *Session {
295302
},
296303
),
297304
toPath,
298-
init.PnpApi,
305+
pnpApi,
299306
),
300307
initialUserPreferences: lsutil.NewDefaultUserPreferences(),
301308
workspaceUserPreferences: lsutil.NewDefaultUserPreferences(),
@@ -504,7 +511,7 @@ func (s *Session) DidChangeWatchedFiles(ctx context.Context, changes []*lsproto.
504511
case lsproto.FileChangeTypeCreated:
505512
kind = FileChangeKindWatchCreate
506513
case lsproto.FileChangeTypeChanged:
507-
if s.pnpApi != nil && strings.HasSuffix(change.Uri.FileName(), ".pnp.cjs") {
514+
if strings.HasSuffix(change.Uri.FileName(), ".pnp.cjs") {
508515
kind = FileChangeKindPnpInstall
509516
} else {
510517
kind = FileChangeKindWatchChange

0 commit comments

Comments
 (0)