Skip to content

Commit 18bc220

Browse files
authored
Encode only libFile name instead of path to it in buildInfo (#1567)
1 parent 8e8766a commit 18bc220

32 files changed

+349
-320
lines changed

internal/compiler/fileloader.go

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ import (
1515
"github.com/microsoft/typescript-go/internal/tspath"
1616
)
1717

18+
type LibFile struct {
19+
Name string
20+
path string
21+
Replaced bool
22+
}
23+
1824
type fileLoader struct {
1925
opts ProgramOptions
2026
resolver *module.Resolver
@@ -34,7 +40,7 @@ type fileLoader struct {
3440
projectReferenceFileMapper *projectReferenceFileMapper
3541
dtsDirectories collections.Set[tspath.Path]
3642

37-
pathForLibFileCache collections.SyncMap[string, string]
43+
pathForLibFileCache collections.SyncMap[string, *LibFile]
3844
pathForLibFileResolutions collections.SyncMap[tspath.Path, module.ModeAwareCache[*module.ResolvedModule]]
3945
}
4046

@@ -49,7 +55,7 @@ type processedFiles struct {
4955
sourceFileMetaDatas map[tspath.Path]ast.SourceFileMetaData
5056
jsxRuntimeImportSpecifiers map[tspath.Path]*jsxRuntimeImportSpecifier
5157
importHelpersImportSpecifiers map[tspath.Path]*ast.Node
52-
libFiles collections.Set[tspath.Path]
58+
libFiles map[tspath.Path]*LibFile
5359
// List of present unsupported extensions
5460
unsupportedExtensions []string
5561
sourceFilesFoundSearchingNodeModules collections.Set[tspath.Path]
@@ -89,24 +95,26 @@ func processAllProgramFiles(
8995
loader.addProjectReferenceTasks(singleThreaded)
9096
loader.resolver = module.NewResolver(loader.projectReferenceFileMapper.host, compilerOptions, opts.TypingsLocation, opts.ProjectName)
9197

92-
var libs []string
98+
for _, file := range rootFiles {
99+
loader.addRootTask(file, nil)
100+
}
101+
93102
if len(rootFiles) > 0 && compilerOptions.NoLib.IsFalseOrUnknown() {
94103
if compilerOptions.Lib == nil {
95104
name := tsoptions.GetDefaultLibFileName(compilerOptions)
96-
libs = append(libs, loader.pathForLibFile(name))
105+
libFile := loader.pathForLibFile(name)
106+
loader.addRootTask(libFile.path, libFile)
97107
} else {
98108
for _, lib := range compilerOptions.Lib {
99109
if name, ok := tsoptions.GetLibFileName(lib); ok {
100-
libs = append(libs, loader.pathForLibFile(name))
110+
libFile := loader.pathForLibFile(name)
111+
loader.addRootTask(libFile.path, libFile)
101112
}
102113
// !!! error on unknown name
103114
}
104115
}
105116
}
106117

107-
loader.addRootTasks(rootFiles, false)
108-
loader.addRootTasks(libs, true)
109-
110118
if len(rootFiles) > 0 {
111119
loader.addAutomaticTypeDirectiveTasks()
112120
}
@@ -131,7 +139,7 @@ func processAllProgramFiles(
131139
var importHelpersImportSpecifiers map[tspath.Path]*ast.Node
132140
var unsupportedExtensions []string
133141
var sourceFilesFoundSearchingNodeModules collections.Set[tspath.Path]
134-
var libFileSet collections.Set[tspath.Path]
142+
libFilesMap := make(map[tspath.Path]*LibFile, libFileCount)
135143
fileLoadDiagnostics := &ast.DiagnosticsCollection{}
136144

137145
loader.filesParser.collect(&loader, loader.rootTasks, func(task *parseTask) {
@@ -149,9 +157,9 @@ func processAllProgramFiles(
149157
missingFiles = append(missingFiles, task.normalizedFilePath)
150158
return
151159
}
152-
if task.isLib {
160+
if task.libFile != nil {
153161
libFiles = append(libFiles, file)
154-
libFileSet.Add(path)
162+
libFilesMap[path] = task.libFile
155163
} else {
156164
files = append(files, file)
157165
}
@@ -222,7 +230,7 @@ func processAllProgramFiles(
222230
importHelpersImportSpecifiers: importHelpersImportSpecifiers,
223231
unsupportedExtensions: unsupportedExtensions,
224232
sourceFilesFoundSearchingNodeModules: sourceFilesFoundSearchingNodeModules,
225-
libFiles: libFileSet,
233+
libFiles: libFilesMap,
226234
fileLoadDiagnostics: fileLoadDiagnostics,
227235
}
228236
}
@@ -231,12 +239,10 @@ func (p *fileLoader) toPath(file string) tspath.Path {
231239
return tspath.ToPath(file, p.opts.Host.GetCurrentDirectory(), p.opts.Host.FS().UseCaseSensitiveFileNames())
232240
}
233241

234-
func (p *fileLoader) addRootTasks(files []string, isLib bool) {
235-
for _, fileName := range files {
236-
absPath := tspath.GetNormalizedAbsolutePath(fileName, p.opts.Host.GetCurrentDirectory())
237-
if core.Tristate.IsTrue(p.opts.Config.CompilerOptions().AllowNonTsExtensions) || slices.Contains(p.supportedExtensions, tspath.TryGetExtensionFromPath(absPath)) {
238-
p.rootTasks = append(p.rootTasks, &parseTask{normalizedFilePath: absPath, isLib: isLib, root: true})
239-
}
242+
func (p *fileLoader) addRootTask(fileName string, libFile *LibFile) {
243+
absPath := tspath.GetNormalizedAbsolutePath(fileName, p.opts.Host.GetCurrentDirectory())
244+
if core.Tristate.IsTrue(p.opts.Config.CompilerOptions().AllowNonTsExtensions) || slices.Contains(p.supportedExtensions, tspath.TryGetExtensionFromPath(absPath)) {
245+
p.rootTasks = append(p.rootTasks, &parseTask{normalizedFilePath: absPath, libFile: libFile, root: true})
240246
}
241247
}
242248

@@ -249,7 +255,7 @@ func (p *fileLoader) addAutomaticTypeDirectiveTasks() {
249255
containingDirectory = p.opts.Host.GetCurrentDirectory()
250256
}
251257
containingFileName := tspath.CombinePaths(containingDirectory, module.InferredTypesContainingFile)
252-
p.rootTasks = append(p.rootTasks, &parseTask{normalizedFilePath: containingFileName, isLib: false, isForAutomaticTypeDirective: true})
258+
p.rootTasks = append(p.rootTasks, &parseTask{normalizedFilePath: containingFileName, isForAutomaticTypeDirective: true})
253259
}
254260

255261
func (p *fileLoader) resolveAutomaticTypeDirectives(containingFileName string) (
@@ -304,12 +310,12 @@ func (p *fileLoader) addProjectReferenceTasks(singleThreaded bool) {
304310
}
305311
if p.opts.canUseProjectReferenceSource() {
306312
for _, fileName := range resolved.FileNames() {
307-
p.rootTasks = append(p.rootTasks, &parseTask{normalizedFilePath: fileName, isLib: false})
313+
p.rootTasks = append(p.rootTasks, &parseTask{normalizedFilePath: fileName})
308314
}
309315
} else {
310316
for outputDts := range resolved.GetOutputDeclarationFileNames() {
311317
if outputDts != "" {
312-
p.rootTasks = append(p.rootTasks, &parseTask{normalizedFilePath: outputDts, isLib: false})
318+
p.rootTasks = append(p.rootTasks, &parseTask{normalizedFilePath: outputDts})
313319
}
314320
}
315321
}
@@ -405,7 +411,7 @@ func (p *fileLoader) resolveTypeReferenceDirectives(t *parseTask) {
405411
increaseDepth: resolved.IsExternalLibraryImport,
406412
elideOnDepth: false,
407413
isFromExternalLibrary: resolved.IsExternalLibraryImport,
408-
}, false)
414+
}, nil)
409415
}
410416
}
411417

@@ -496,7 +502,7 @@ func (p *fileLoader) resolveImportsAndModuleAugmentations(t *parseTask) {
496502
increaseDepth: resolvedModule.IsExternalLibraryImport,
497503
elideOnDepth: isJsFileFromNodeModules,
498504
isFromExternalLibrary: resolvedModule.IsExternalLibraryImport,
499-
}, false)
505+
}, nil)
500506
}
501507
}
502508

@@ -517,26 +523,28 @@ func (p *fileLoader) createSyntheticImport(text string, file *ast.SourceFile) *a
517523
return externalHelpersModuleReference
518524
}
519525

520-
func (p *fileLoader) pathForLibFile(name string) string {
526+
func (p *fileLoader) pathForLibFile(name string) *LibFile {
521527
if cached, ok := p.pathForLibFileCache.Load(name); ok {
522528
return cached
523529
}
524530

525531
path := tspath.CombinePaths(p.defaultLibraryPath, name)
532+
replaced := false
526533
if p.opts.Config.CompilerOptions().LibReplacement.IsTrue() {
527534
libraryName := getLibraryNameFromLibFileName(name)
528535
resolveFrom := getInferredLibraryNameResolveFrom(p.opts.Config.CompilerOptions(), p.opts.Host.GetCurrentDirectory(), name)
529536
resolution := p.resolver.ResolveModuleName(libraryName, resolveFrom, core.ModuleKindCommonJS, nil)
530537
if resolution.IsResolved() {
531538
path = resolution.ResolvedFileName
539+
replaced = true
532540
p.pathForLibFileResolutions.LoadOrStore(p.toPath(resolveFrom), module.ModeAwareCache[*module.ResolvedModule]{
533541
module.ModeAwareCacheKey{Name: libraryName, Mode: core.ModuleKindCommonJS}: resolution,
534542
})
535543
}
536544
}
537545

538-
path, _ = p.pathForLibFileCache.LoadOrStore(name, path)
539-
return path
546+
libPath, _ := p.pathForLibFileCache.LoadOrStore(name, &LibFile{name, path, replaced})
547+
return libPath
540548
}
541549

542550
func getLibraryNameFromLibFileName(libFileName string) string {

internal/compiler/filesparser.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type parseTask struct {
1616
normalizedFilePath string
1717
path tspath.Path
1818
file *ast.SourceFile
19-
isLib bool
19+
libFile *LibFile
2020
isRedirected bool
2121
subTasks []*parseTask
2222
loaded bool
@@ -59,7 +59,7 @@ func (t *parseTask) load(loader *fileLoader) {
5959
}
6060

6161
loader.totalFileCount.Add(1)
62-
if t.isLib {
62+
if t.libFile != nil {
6363
loader.libFileCount.Add(1)
6464
}
6565

@@ -74,7 +74,7 @@ func (t *parseTask) load(loader *fileLoader) {
7474

7575
for _, ref := range file.ReferencedFiles {
7676
resolvedPath := loader.resolveTripleslashPathReference(ref.FileName, file.FileName())
77-
t.addSubTask(resolvedPath, false)
77+
t.addSubTask(resolvedPath, nil)
7878
}
7979

8080
compilerOptions := loader.opts.Config.CompilerOptions()
@@ -83,7 +83,8 @@ func (t *parseTask) load(loader *fileLoader) {
8383
if compilerOptions.NoLib != core.TSTrue {
8484
for _, lib := range file.LibReferenceDirectives {
8585
if name, ok := tsoptions.GetLibFileName(lib.FileName); ok {
86-
t.addSubTask(resolvedRef{fileName: loader.pathForLibFile(name)}, true)
86+
libFile := loader.pathForLibFile(name)
87+
t.addSubTask(resolvedRef{fileName: libFile.path}, libFile)
8788
}
8889
}
8990
}
@@ -94,14 +95,14 @@ func (t *parseTask) load(loader *fileLoader) {
9495
func (t *parseTask) redirect(loader *fileLoader, fileName string) {
9596
t.isRedirected = true
9697
// increaseDepth and elideOnDepth are not copied to redirects, otherwise their depth would be double counted.
97-
t.subTasks = []*parseTask{{normalizedFilePath: tspath.NormalizePath(fileName), isLib: t.isLib, fromExternalLibrary: t.fromExternalLibrary}}
98+
t.subTasks = []*parseTask{{normalizedFilePath: tspath.NormalizePath(fileName), libFile: t.libFile, fromExternalLibrary: t.fromExternalLibrary}}
9899
}
99100

100101
func (t *parseTask) loadAutomaticTypeDirectives(loader *fileLoader) {
101102
toParseTypeRefs, typeResolutionsInFile := loader.resolveAutomaticTypeDirectives(t.normalizedFilePath)
102103
t.typeResolutionsInFile = typeResolutionsInFile
103104
for _, typeResolution := range toParseTypeRefs {
104-
t.addSubTask(typeResolution, false)
105+
t.addSubTask(typeResolution, nil)
105106
}
106107
}
107108

@@ -112,11 +113,11 @@ type resolvedRef struct {
112113
isFromExternalLibrary bool
113114
}
114115

115-
func (t *parseTask) addSubTask(ref resolvedRef, isLib bool) {
116+
func (t *parseTask) addSubTask(ref resolvedRef, libFile *LibFile) {
116117
normalizedFilePath := tspath.NormalizePath(ref.fileName)
117118
subTask := &parseTask{
118119
normalizedFilePath: normalizedFilePath,
119-
isLib: isLib,
120+
libFile: libFile,
120121
increaseDepth: ref.increaseDepth,
121122
elideOnDepth: ref.elideOnDepth,
122123
fromExternalLibrary: ref.isFromExternalLibrary,

internal/compiler/program.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,15 @@ func (p *Program) GetDefaultResolutionModeForFile(sourceFile ast.HasFileName) co
12331233
}
12341234

12351235
func (p *Program) IsSourceFileDefaultLibrary(path tspath.Path) bool {
1236-
return p.libFiles.Has(path)
1236+
_, ok := p.libFiles[path]
1237+
return ok
1238+
}
1239+
1240+
func (p *Program) GetDefaultLibFile(path tspath.Path) *LibFile {
1241+
if libFile, ok := p.libFiles[path]; ok {
1242+
return libFile
1243+
}
1244+
return nil
12371245
}
12381246

12391247
func (p *Program) CommonSourceDirectory() string {

internal/execute/tsc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func performIncrementalCompilation(
233233
) CommandLineResult {
234234
host := compiler.NewCachedFSCompilerHost(sys.GetCurrentDirectory(), sys.FS(), sys.DefaultLibraryPath(), extendedConfigCache)
235235
buildInfoReadStart := sys.Now()
236-
oldProgram := incremental.ReadBuildInfoProgram(config, incremental.NewBuildInfoReader(host))
236+
oldProgram := incremental.ReadBuildInfoProgram(config, incremental.NewBuildInfoReader(host), host)
237237
buildInfoReadTime := sys.Now().Sub(buildInfoReadStart)
238238
// todo: cache, statistics, tracing
239239
parseStart := sys.Now()

internal/execute/watcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func createWatcher(sys System, configParseResult *tsoptions.ParsedCommandLine, r
4343

4444
func (w *Watcher) start() {
4545
w.host = compiler.NewCompilerHost(w.sys.GetCurrentDirectory(), w.sys.FS(), w.sys.DefaultLibraryPath(), nil)
46-
w.program = incremental.ReadBuildInfoProgram(w.options, incremental.NewBuildInfoReader(w.host))
46+
w.program = incremental.ReadBuildInfoProgram(w.options, incremental.NewBuildInfoReader(w.host), w.host)
4747

4848
if !w.testing {
4949
watchInterval := 1000 * time.Millisecond

internal/incremental/buildinfotosnapshot.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
package incremental
22

33
import (
4+
"strings"
5+
46
"github.com/microsoft/typescript-go/internal/collections"
7+
"github.com/microsoft/typescript-go/internal/compiler"
58
"github.com/microsoft/typescript-go/internal/core"
69
"github.com/microsoft/typescript-go/internal/tsoptions"
710
"github.com/microsoft/typescript-go/internal/tspath"
811
)
912

10-
func buildInfoToSnapshot(buildInfo *BuildInfo, buildInfoFileName string, config *tsoptions.ParsedCommandLine) *snapshot {
13+
func buildInfoToSnapshot(buildInfo *BuildInfo, buildInfoFileName string, config *tsoptions.ParsedCommandLine, host compiler.CompilerHost) *snapshot {
1114
to := &toSnapshot{
1215
buildInfo: buildInfo,
1316
buildInfoDirectory: tspath.GetDirectoryPath(tspath.GetNormalizedAbsolutePath(buildInfoFileName, config.GetCurrentDirectory())),
1417
filePaths: make([]tspath.Path, 0, len(buildInfo.FileNames)),
1518
filePathSet: make([]*collections.Set[tspath.Path], 0, len(buildInfo.FileIdsList)),
1619
}
1720
to.filePaths = core.Map(buildInfo.FileNames, func(fileName string) tspath.Path {
21+
if !strings.HasPrefix(fileName, ".") {
22+
return tspath.ToPath(tspath.CombinePaths(host.DefaultLibraryPath(), fileName), host.GetCurrentDirectory(), host.FS().UseCaseSensitiveFileNames())
23+
}
1824
return tspath.ToPath(fileName, to.buildInfoDirectory, config.UseCaseSensitiveFileNames())
1925
})
2026
to.filePathSet = core.Map(buildInfo.FileIdsList, func(fileIdList []BuildInfoFileId) *collections.Set[tspath.Path] {

internal/incremental/incremental.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func NewBuildInfoReader(
3636
return &buildInfoReader{host: host}
3737
}
3838

39-
func ReadBuildInfoProgram(config *tsoptions.ParsedCommandLine, reader BuildInfoReader) *Program {
39+
func ReadBuildInfoProgram(config *tsoptions.ParsedCommandLine, reader BuildInfoReader, host compiler.CompilerHost) *Program {
4040
buildInfoFileName := config.GetBuildInfoFileName()
4141
if buildInfoFileName == "" {
4242
return nil
@@ -50,7 +50,7 @@ func ReadBuildInfoProgram(config *tsoptions.ParsedCommandLine, reader BuildInfoR
5050

5151
// Convert to information that can be used to create incremental program
5252
incrementalProgram := &Program{
53-
snapshot: buildInfoToSnapshot(buildInfo, buildInfoFileName, config),
53+
snapshot: buildInfoToSnapshot(buildInfo, buildInfoFileName, config, host),
5454
}
5555
return incrementalProgram
5656
}

internal/incremental/snapshottobuildinfo.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ func (t *toBuildInfo) relativeToBuildInfo(path string) string {
6767
func (t *toBuildInfo) toFileId(path tspath.Path) BuildInfoFileId {
6868
fileId := t.fileNameToFileId[string(path)]
6969
if fileId == 0 {
70-
t.buildInfo.FileNames = append(t.buildInfo.FileNames, t.relativeToBuildInfo(string(path)))
70+
if libFile := t.program.GetDefaultLibFile(path); libFile != nil && !libFile.Replaced {
71+
t.buildInfo.FileNames = append(t.buildInfo.FileNames, libFile.Name)
72+
} else {
73+
t.buildInfo.FileNames = append(t.buildInfo.FileNames, t.relativeToBuildInfo(string(path)))
74+
}
7175
fileId = BuildInfoFileId(len(t.buildInfo.FileNames))
7276
t.fileNameToFileId[string(path)] = fileId
7377
}
@@ -176,7 +180,9 @@ func (t *toBuildInfo) setFileInfoAndEmitSignatures() {
176180
fileId := t.toFileId(file.Path())
177181
// tryAddRoot(key, fileId);
178182
if t.buildInfo.FileNames[fileId-1] != t.relativeToBuildInfo(string(file.Path())) {
179-
panic(fmt.Sprintf("File name at index %d does not match expected relative path: %s != %s", fileId-1, t.buildInfo.FileNames[fileId-1], t.relativeToBuildInfo(string(file.Path()))))
183+
if libFile := t.program.GetDefaultLibFile(file.Path()); libFile == nil || libFile.Replaced || t.buildInfo.FileNames[fileId-1] != libFile.Name {
184+
panic(fmt.Sprintf("File name at index %d does not match expected relative path or libName: %s != %s", fileId-1, t.buildInfo.FileNames[fileId-1], t.relativeToBuildInfo(string(file.Path()))))
185+
}
180186
}
181187
if t.snapshot.options.Composite.IsTrue() {
182188
if !ast.IsJsonSourceFile(file) && t.program.SourceFileMayBeEmitted(file, false) {

0 commit comments

Comments
 (0)