Skip to content

Commit 141423a

Browse files
committed
Port --traceResolution
1 parent 2e3d923 commit 141423a

File tree

17 files changed

+412
-202
lines changed

17 files changed

+412
-202
lines changed

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)
39+
host := compiler.NewCompilerHost(cd, fs, bundled.LibPath(), nil, func(msg string) {})
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)
73+
host := compiler.NewCompilerHost(rootPath, fs, bundled.LibPath(), nil, func(msg string) {})
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)
90+
host := compiler.NewCompilerHost(rootPath, fs, bundled.LibPath(), nil, func(msg string) {})
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/fileloader.go

Lines changed: 33 additions & 14 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 libResolution struct {
19+
libraryName string
20+
resolution *module.ResolvedModule
21+
trace []string
22+
}
23+
1824
type fileLoader struct {
1925
opts ProgramOptions
2026
resolver *module.Resolver
@@ -35,7 +41,7 @@ type fileLoader struct {
3541
dtsDirectories collections.Set[tspath.Path]
3642

3743
pathForLibFileCache collections.SyncMap[string, string]
38-
pathForLibFileResolutions collections.SyncMap[tspath.Path, module.ModeAwareCache[*module.ResolvedModule]]
44+
pathForLibFileResolutions collections.SyncMap[tspath.Path, *libResolution]
3945
}
4046

4147
type processedFiles struct {
@@ -200,12 +206,15 @@ func processAllProgramFiles(
200206
}
201207
}
202208

203-
loader.pathForLibFileResolutions.Range(func(key tspath.Path, value module.ModeAwareCache[*module.ResolvedModule]) bool {
204-
resolvedModules[key] = value
205-
for _, resolvedModule := range value {
206-
for _, diag := range resolvedModule.ResolutionDiagnostics {
207-
fileLoadDiagnostics.Add(diag)
208-
}
209+
loader.pathForLibFileResolutions.Range(func(key tspath.Path, value *libResolution) bool {
210+
resolvedModules[key] = module.ModeAwareCache[*module.ResolvedModule]{
211+
module.ModeAwareCacheKey{Name: value.libraryName, Mode: core.ModuleKindCommonJS}: value.resolution,
212+
}
213+
for _, trace := range value.trace {
214+
opts.Host.Trace(trace)
215+
}
216+
for _, diag := range value.resolution.ResolutionDiagnostics {
217+
fileLoadDiagnostics.Add(diag)
209218
}
210219
return true
211220
})
@@ -255,15 +264,17 @@ func (p *fileLoader) addAutomaticTypeDirectiveTasks() {
255264
func (p *fileLoader) resolveAutomaticTypeDirectives(containingFileName string) (
256265
toParse []resolvedRef,
257266
typeResolutionsInFile module.ModeAwareCache[*module.ResolvedTypeReferenceDirective],
267+
typeResolutionsTrace []string,
258268
) {
259269
automaticTypeDirectiveNames := module.GetAutomaticTypeDirectiveNames(p.opts.Config.CompilerOptions(), p.opts.Host)
260270
if len(automaticTypeDirectiveNames) != 0 {
261271
toParse = make([]resolvedRef, 0, len(automaticTypeDirectiveNames))
262272
typeResolutionsInFile = make(module.ModeAwareCache[*module.ResolvedTypeReferenceDirective], len(automaticTypeDirectiveNames))
263273
for _, name := range automaticTypeDirectiveNames {
264274
resolutionMode := core.ModuleKindNodeNext
265-
resolved := p.resolver.ResolveTypeReferenceDirective(name, containingFileName, resolutionMode, nil)
275+
resolved, trace := p.resolver.ResolveTypeReferenceDirective(name, containingFileName, resolutionMode, nil)
266276
typeResolutionsInFile[module.ModeAwareCacheKey{Name: name, Mode: resolutionMode}] = resolved
277+
typeResolutionsTrace = append(typeResolutionsTrace, trace...)
267278
if resolved.IsResolved() {
268279
toParse = append(toParse, resolvedRef{
269280
fileName: resolved.ResolvedFileName,
@@ -273,7 +284,7 @@ func (p *fileLoader) resolveAutomaticTypeDirectives(containingFileName string) (
273284
}
274285
}
275286
}
276-
return toParse, typeResolutionsInFile
287+
return toParse, typeResolutionsInFile, typeResolutionsTrace
277288
}
278289

279290
func (p *fileLoader) addProjectReferenceTasks(singleThreaded bool) {
@@ -393,11 +404,13 @@ func (p *fileLoader) resolveTypeReferenceDirectives(t *parseTask) {
393404
meta := t.metadata
394405

395406
typeResolutionsInFile := make(module.ModeAwareCache[*module.ResolvedTypeReferenceDirective], len(file.TypeReferenceDirectives))
407+
var typeResolutionsTrace []string
396408
for _, ref := range file.TypeReferenceDirectives {
397409
redirect := p.projectReferenceFileMapper.getRedirectForResolution(file)
398410
resolutionMode := getModeForTypeReferenceDirectiveInFile(ref, file, meta, module.GetCompilerOptionsWithRedirect(p.opts.Config.CompilerOptions(), redirect))
399-
resolved := p.resolver.ResolveTypeReferenceDirective(ref.FileName, file.FileName(), resolutionMode, redirect)
411+
resolved, trace := p.resolver.ResolveTypeReferenceDirective(ref.FileName, file.FileName(), resolutionMode, redirect)
400412
typeResolutionsInFile[module.ModeAwareCacheKey{Name: ref.FileName, Mode: resolutionMode}] = resolved
413+
typeResolutionsTrace = append(typeResolutionsTrace, trace...)
401414

402415
if resolved.IsResolved() {
403416
t.addSubTask(resolvedRef{
@@ -410,6 +423,7 @@ func (p *fileLoader) resolveTypeReferenceDirectives(t *parseTask) {
410423
}
411424

412425
t.typeResolutionsInFile = typeResolutionsInFile
426+
t.typeResolutionsTrace = typeResolutionsTrace
413427
}
414428

415429
const externalHelpersModuleNameText = "tslib" // TODO(jakebailey): dedupe
@@ -455,6 +469,7 @@ func (p *fileLoader) resolveImportsAndModuleAugmentations(t *parseTask) {
455469

456470
if len(moduleNames) != 0 {
457471
resolutionsInFile := make(module.ModeAwareCache[*module.ResolvedModule], len(moduleNames))
472+
var resolutionsTrace []string
458473

459474
for index, entry := range moduleNames {
460475
moduleName := entry.Text()
@@ -463,8 +478,9 @@ func (p *fileLoader) resolveImportsAndModuleAugmentations(t *parseTask) {
463478
}
464479

465480
mode := getModeForUsageLocation(file.FileName(), meta, entry, optionsForFile)
466-
resolvedModule := p.resolver.ResolveModuleName(moduleName, file.FileName(), mode, redirect)
481+
resolvedModule, trace := p.resolver.ResolveModuleName(moduleName, file.FileName(), mode, redirect)
467482
resolutionsInFile[module.ModeAwareCacheKey{Name: moduleName, Mode: mode}] = resolvedModule
483+
resolutionsTrace = append(resolutionsTrace, trace...)
468484

469485
if !resolvedModule.IsResolved() {
470486
continue
@@ -501,6 +517,7 @@ func (p *fileLoader) resolveImportsAndModuleAugmentations(t *parseTask) {
501517
}
502518

503519
t.resolutionsInFile = resolutionsInFile
520+
t.resolutionsTrace = resolutionsTrace
504521
}
505522
}
506523

@@ -526,11 +543,13 @@ func (p *fileLoader) pathForLibFile(name string) string {
526543
if p.opts.Config.CompilerOptions().LibReplacement.IsTrue() {
527544
libraryName := getLibraryNameFromLibFileName(name)
528545
resolveFrom := getInferredLibraryNameResolveFrom(p.opts.Config.CompilerOptions(), p.opts.Host.GetCurrentDirectory(), name)
529-
resolution := p.resolver.ResolveModuleName(libraryName, resolveFrom, core.ModuleKindCommonJS, nil)
546+
resolution, trace := p.resolver.ResolveModuleName(libraryName, resolveFrom, core.ModuleKindCommonJS, nil)
530547
if resolution.IsResolved() {
531548
path = resolution.ResolvedFileName
532-
p.pathForLibFileResolutions.LoadOrStore(p.toPath(resolveFrom), module.ModeAwareCache[*module.ResolvedModule]{
533-
module.ModeAwareCacheKey{Name: libraryName, Mode: core.ModuleKindCommonJS}: resolution,
549+
p.pathForLibFileResolutions.LoadOrStore(p.toPath(resolveFrom), &libResolution{
550+
libraryName: libraryName,
551+
resolution: resolution,
552+
trace: trace,
534553
})
535554
}
536555
}

internal/compiler/filesparser.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ type parseTask struct {
2525

2626
metadata ast.SourceFileMetaData
2727
resolutionsInFile module.ModeAwareCache[*module.ResolvedModule]
28+
resolutionsTrace []string
2829
typeResolutionsInFile module.ModeAwareCache[*module.ResolvedTypeReferenceDirective]
30+
typeResolutionsTrace []string
2931
resolutionDiagnostics []*ast.Diagnostic
3032
importHelpersImportSpecifier *ast.Node
3133
jsxRuntimeImportSpecifier *jsxRuntimeImportSpecifier
@@ -98,8 +100,9 @@ func (t *parseTask) redirect(loader *fileLoader, fileName string) {
98100
}
99101

100102
func (t *parseTask) loadAutomaticTypeDirectives(loader *fileLoader) {
101-
toParseTypeRefs, typeResolutionsInFile := loader.resolveAutomaticTypeDirectives(t.normalizedFilePath)
103+
toParseTypeRefs, typeResolutionsInFile, typeResolutionsTrace := loader.resolveAutomaticTypeDirectives(t.normalizedFilePath)
102104
t.typeResolutionsInFile = typeResolutionsInFile
105+
t.typeResolutionsTrace = typeResolutionsTrace
103106
for _, typeResolution := range toParseTypeRefs {
104107
t.addSubTask(typeResolution, false)
105108
}
@@ -193,30 +196,32 @@ func (w *filesParser) start(loader *fileLoader, tasks []*parseTask, depth int, i
193196
}
194197
}
195198

196-
func (w *filesParser) collect(loader *fileLoader, tasks []*parseTask, iterate func(*parseTask)) []tspath.Path {
199+
func (w *filesParser) collect(loader *fileLoader, tasks []*parseTask, iterate func(*parseTask)) {
197200
// Mark all tasks we saw as external after the fact.
198201
w.tasksByFileName.Range(func(key string, value *queuedParseTask) bool {
199202
if value.fromExternalLibrary {
200203
value.task.fromExternalLibrary = true
201204
}
202205
return true
203206
})
204-
return w.collectWorker(loader, tasks, iterate, collections.Set[*parseTask]{})
207+
w.collectWorker(loader, tasks, iterate, collections.Set[*parseTask]{})
205208
}
206209

207-
func (w *filesParser) collectWorker(loader *fileLoader, tasks []*parseTask, iterate func(*parseTask), seen collections.Set[*parseTask]) []tspath.Path {
208-
var results []tspath.Path
210+
func (w *filesParser) collectWorker(loader *fileLoader, tasks []*parseTask, iterate func(*parseTask), seen collections.Set[*parseTask]) {
209211
for _, task := range tasks {
210212
// ensure we only walk each task once
211-
if !task.loaded || seen.Has(task) {
213+
if !task.loaded || !seen.AddIfAbsent(task) {
212214
continue
213215
}
214-
seen.Add(task)
216+
for _, trace := range task.typeResolutionsTrace {
217+
loader.opts.Host.Trace(trace)
218+
}
219+
for _, trace := range task.resolutionsTrace {
220+
loader.opts.Host.Trace(trace)
221+
}
215222
if subTasks := task.subTasks; len(subTasks) > 0 {
216223
w.collectWorker(loader, subTasks, iterate, seen)
217224
}
218225
iterate(task)
219-
results = append(results, loader.toPath(task.FileName()))
220226
}
221-
return results
222227
}

internal/compiler/host.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,32 @@ type compilerHost struct {
3030
defaultLibraryPath string
3131
extendedConfigCache *collections.SyncMap[tspath.Path, *tsoptions.ExtendedConfigCacheEntry]
3232
extendedConfigCacheOnce sync.Once
33+
trace func(msg string)
3334
}
3435

3536
func NewCachedFSCompilerHost(
3637
currentDirectory string,
3738
fs vfs.FS,
3839
defaultLibraryPath string,
3940
extendedConfigCache *collections.SyncMap[tspath.Path, *tsoptions.ExtendedConfigCacheEntry],
41+
trace func(msg string),
4042
) CompilerHost {
41-
return NewCompilerHost(currentDirectory, cachedvfs.From(fs), defaultLibraryPath, extendedConfigCache)
43+
return NewCompilerHost(currentDirectory, cachedvfs.From(fs), defaultLibraryPath, extendedConfigCache, trace)
4244
}
4345

4446
func NewCompilerHost(
4547
currentDirectory string,
4648
fs vfs.FS,
4749
defaultLibraryPath string,
4850
extendedConfigCache *collections.SyncMap[tspath.Path, *tsoptions.ExtendedConfigCacheEntry],
51+
trace func(msg string),
4952
) CompilerHost {
5053
return &compilerHost{
5154
currentDirectory: currentDirectory,
5255
fs: fs,
5356
defaultLibraryPath: defaultLibraryPath,
5457
extendedConfigCache: extendedConfigCache,
58+
trace: trace,
5559
}
5660
}
5761

@@ -68,7 +72,7 @@ func (h *compilerHost) GetCurrentDirectory() string {
6872
}
6973

7074
func (h *compilerHost) Trace(msg string) {
71-
//!!! TODO: implement
75+
h.trace(msg)
7276
}
7377

7478
func (h *compilerHost) GetSourceFile(opts ast.SourceFileParseOptions) *ast.SourceFile {

internal/compiler/program_test.go

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

246246
actualFiles := []string{}
@@ -277,7 +277,7 @@ func BenchmarkNewProgram(b *testing.B) {
277277
CompilerOptions: &opts,
278278
},
279279
},
280-
Host: NewCompilerHost("c:/dev/src", fs, bundled.LibPath(), nil),
280+
Host: NewCompilerHost("c:/dev/src", fs, bundled.LibPath(), nil, func(msg string) {}),
281281
}
282282

283283
for b.Loop() {
@@ -294,7 +294,7 @@ func BenchmarkNewProgram(b *testing.B) {
294294
fs := osvfs.FS()
295295
fs = bundled.WrapFS(fs)
296296

297-
host := NewCompilerHost(rootPath, fs, bundled.LibPath(), nil)
297+
host := NewCompilerHost(rootPath, fs, bundled.LibPath(), nil, func(msg string) {})
298298

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

internal/compiler/projectreferencedtsfakinghost.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ func (h *projectReferenceDtsFakingHost) GetCurrentDirectory() string {
4141
return h.host.GetCurrentDirectory()
4242
}
4343

44-
// Trace implements module.ResolutionHost.
45-
func (h *projectReferenceDtsFakingHost) Trace(msg string) {
46-
h.host.Trace(msg)
47-
}
48-
4944
type projectReferenceDtsFakingVfs struct {
5045
projectReferenceFileMapper *projectReferenceFileMapper
5146
dtsDirectories collections.Set[tspath.Path]

internal/execute/tsc.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,12 @@ func findConfigFile(searchPath string, fileExists func(string) bool, configName
223223
return result
224224
}
225225

226+
func getTraceFromSys(sys System) func(msg string) {
227+
return func(msg string) {
228+
fmt.Fprintln(sys.Writer(), msg)
229+
}
230+
}
231+
226232
func performIncrementalCompilation(
227233
sys System,
228234
config *tsoptions.ParsedCommandLine,
@@ -231,7 +237,7 @@ func performIncrementalCompilation(
231237
configTime time.Duration,
232238
testing bool,
233239
) CommandLineResult {
234-
host := compiler.NewCachedFSCompilerHost(sys.GetCurrentDirectory(), sys.FS(), sys.DefaultLibraryPath(), extendedConfigCache)
240+
host := compiler.NewCachedFSCompilerHost(sys.GetCurrentDirectory(), sys.FS(), sys.DefaultLibraryPath(), extendedConfigCache, getTraceFromSys(sys))
235241
buildInfoReadStart := sys.Now()
236242
oldProgram := incremental.ReadBuildInfoProgram(config, incremental.NewBuildInfoReader(host))
237243
buildInfoReadTime := sys.Now().Sub(buildInfoReadStart)
@@ -270,7 +276,7 @@ func performCompilation(
270276
extendedConfigCache *collections.SyncMap[tspath.Path, *tsoptions.ExtendedConfigCacheEntry],
271277
configTime time.Duration,
272278
) CommandLineResult {
273-
host := compiler.NewCachedFSCompilerHost(sys.GetCurrentDirectory(), sys.FS(), sys.DefaultLibraryPath(), extendedConfigCache)
279+
host := compiler.NewCachedFSCompilerHost(sys.GetCurrentDirectory(), sys.FS(), sys.DefaultLibraryPath(), extendedConfigCache, getTraceFromSys(sys))
274280
// todo: cache, statistics, tracing
275281
parseStart := sys.Now()
276282
program := compiler.NewProgram(compiler.ProgramOptions{

internal/execute/watcher.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func createWatcher(sys System, configParseResult *tsoptions.ParsedCommandLine, r
4242
}
4343

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

4848
if !w.testing {
@@ -109,7 +109,7 @@ func (w *Watcher) hasErrorsInTsConfig() bool {
109109
w.configModified = true
110110
}
111111
w.options = configParseResult
112-
w.host = compiler.NewCompilerHost(w.sys.GetCurrentDirectory(), w.sys.FS(), w.sys.DefaultLibraryPath(), &extendedConfigCache)
112+
w.host = compiler.NewCompilerHost(w.sys.GetCurrentDirectory(), w.sys.FS(), w.sys.DefaultLibraryPath(), &extendedConfigCache, getTraceFromSys(w.sys))
113113
}
114114
return false
115115
}

0 commit comments

Comments
 (0)