Skip to content

Commit f6a78d6

Browse files
authored
Port --traceResolutions (#1537)
1 parent 18bc220 commit f6a78d6

File tree

196 files changed

+7727
-251
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

196 files changed

+7727
-251
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, 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)
73+
host := compiler.NewCompilerHost(rootPath, fs, bundled.LibPath(), 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)
90+
host := compiler.NewCompilerHost(rootPath, fs, bundled.LibPath(), 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/fileloader.go

Lines changed: 37 additions & 16 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 LibFile struct {
1925
Name string
2026
path string
@@ -41,7 +47,7 @@ type fileLoader struct {
4147
dtsDirectories collections.Set[tspath.Path]
4248

4349
pathForLibFileCache collections.SyncMap[string, *LibFile]
44-
pathForLibFileResolutions collections.SyncMap[tspath.Path, module.ModeAwareCache[*module.ResolvedModule]]
50+
pathForLibFileResolutions collections.SyncMap[tspath.Path, *libResolution]
4551
}
4652

4753
type processedFiles struct {
@@ -208,15 +214,20 @@ func processAllProgramFiles(
208214
}
209215
}
210216

211-
loader.pathForLibFileResolutions.Range(func(key tspath.Path, value module.ModeAwareCache[*module.ResolvedModule]) bool {
212-
resolvedModules[key] = value
213-
for _, resolvedModule := range value {
214-
for _, diag := range resolvedModule.ResolutionDiagnostics {
215-
fileLoadDiagnostics.Add(diag)
216-
}
217+
keys := slices.Collect(loader.pathForLibFileResolutions.Keys())
218+
slices.Sort(keys)
219+
for _, key := range keys {
220+
value, _ := loader.pathForLibFileResolutions.Load(key)
221+
resolvedModules[key] = module.ModeAwareCache[*module.ResolvedModule]{
222+
module.ModeAwareCacheKey{Name: value.libraryName, Mode: core.ModuleKindCommonJS}: value.resolution,
217223
}
218-
return true
219-
})
224+
for _, trace := range value.trace {
225+
opts.Host.Trace(trace)
226+
}
227+
for _, diag := range value.resolution.ResolutionDiagnostics {
228+
fileLoadDiagnostics.Add(diag)
229+
}
230+
}
220231

221232
return processedFiles{
222233
resolver: loader.resolver,
@@ -261,15 +272,17 @@ func (p *fileLoader) addAutomaticTypeDirectiveTasks() {
261272
func (p *fileLoader) resolveAutomaticTypeDirectives(containingFileName string) (
262273
toParse []resolvedRef,
263274
typeResolutionsInFile module.ModeAwareCache[*module.ResolvedTypeReferenceDirective],
275+
typeResolutionsTrace []string,
264276
) {
265277
automaticTypeDirectiveNames := module.GetAutomaticTypeDirectiveNames(p.opts.Config.CompilerOptions(), p.opts.Host)
266278
if len(automaticTypeDirectiveNames) != 0 {
267279
toParse = make([]resolvedRef, 0, len(automaticTypeDirectiveNames))
268280
typeResolutionsInFile = make(module.ModeAwareCache[*module.ResolvedTypeReferenceDirective], len(automaticTypeDirectiveNames))
269281
for _, name := range automaticTypeDirectiveNames {
270282
resolutionMode := core.ModuleKindNodeNext
271-
resolved := p.resolver.ResolveTypeReferenceDirective(name, containingFileName, resolutionMode, nil)
283+
resolved, trace := p.resolver.ResolveTypeReferenceDirective(name, containingFileName, resolutionMode, nil)
272284
typeResolutionsInFile[module.ModeAwareCacheKey{Name: name, Mode: resolutionMode}] = resolved
285+
typeResolutionsTrace = append(typeResolutionsTrace, trace...)
273286
if resolved.IsResolved() {
274287
toParse = append(toParse, resolvedRef{
275288
fileName: resolved.ResolvedFileName,
@@ -279,7 +292,7 @@ func (p *fileLoader) resolveAutomaticTypeDirectives(containingFileName string) (
279292
}
280293
}
281294
}
282-
return toParse, typeResolutionsInFile
295+
return toParse, typeResolutionsInFile, typeResolutionsTrace
283296
}
284297

285298
func (p *fileLoader) addProjectReferenceTasks(singleThreaded bool) {
@@ -399,11 +412,13 @@ func (p *fileLoader) resolveTypeReferenceDirectives(t *parseTask) {
399412
meta := t.metadata
400413

401414
typeResolutionsInFile := make(module.ModeAwareCache[*module.ResolvedTypeReferenceDirective], len(file.TypeReferenceDirectives))
415+
var typeResolutionsTrace []string
402416
for _, ref := range file.TypeReferenceDirectives {
403417
redirect := p.projectReferenceFileMapper.getRedirectForResolution(file)
404418
resolutionMode := getModeForTypeReferenceDirectiveInFile(ref, file, meta, module.GetCompilerOptionsWithRedirect(p.opts.Config.CompilerOptions(), redirect))
405-
resolved := p.resolver.ResolveTypeReferenceDirective(ref.FileName, file.FileName(), resolutionMode, redirect)
419+
resolved, trace := p.resolver.ResolveTypeReferenceDirective(ref.FileName, file.FileName(), resolutionMode, redirect)
406420
typeResolutionsInFile[module.ModeAwareCacheKey{Name: ref.FileName, Mode: resolutionMode}] = resolved
421+
typeResolutionsTrace = append(typeResolutionsTrace, trace...)
407422

408423
if resolved.IsResolved() {
409424
t.addSubTask(resolvedRef{
@@ -416,6 +431,7 @@ func (p *fileLoader) resolveTypeReferenceDirectives(t *parseTask) {
416431
}
417432

418433
t.typeResolutionsInFile = typeResolutionsInFile
434+
t.typeResolutionsTrace = typeResolutionsTrace
419435
}
420436

421437
const externalHelpersModuleNameText = "tslib" // TODO(jakebailey): dedupe
@@ -461,6 +477,7 @@ func (p *fileLoader) resolveImportsAndModuleAugmentations(t *parseTask) {
461477

462478
if len(moduleNames) != 0 {
463479
resolutionsInFile := make(module.ModeAwareCache[*module.ResolvedModule], len(moduleNames))
480+
var resolutionsTrace []string
464481

465482
for index, entry := range moduleNames {
466483
moduleName := entry.Text()
@@ -469,8 +486,9 @@ func (p *fileLoader) resolveImportsAndModuleAugmentations(t *parseTask) {
469486
}
470487

471488
mode := getModeForUsageLocation(file.FileName(), meta, entry, optionsForFile)
472-
resolvedModule := p.resolver.ResolveModuleName(moduleName, file.FileName(), mode, redirect)
489+
resolvedModule, trace := p.resolver.ResolveModuleName(moduleName, file.FileName(), mode, redirect)
473490
resolutionsInFile[module.ModeAwareCacheKey{Name: moduleName, Mode: mode}] = resolvedModule
491+
resolutionsTrace = append(resolutionsTrace, trace...)
474492

475493
if !resolvedModule.IsResolved() {
476494
continue
@@ -507,6 +525,7 @@ func (p *fileLoader) resolveImportsAndModuleAugmentations(t *parseTask) {
507525
}
508526

509527
t.resolutionsInFile = resolutionsInFile
528+
t.resolutionsTrace = resolutionsTrace
510529
}
511530
}
512531

@@ -533,12 +552,14 @@ func (p *fileLoader) pathForLibFile(name string) *LibFile {
533552
if p.opts.Config.CompilerOptions().LibReplacement.IsTrue() {
534553
libraryName := getLibraryNameFromLibFileName(name)
535554
resolveFrom := getInferredLibraryNameResolveFrom(p.opts.Config.CompilerOptions(), p.opts.Host.GetCurrentDirectory(), name)
536-
resolution := p.resolver.ResolveModuleName(libraryName, resolveFrom, core.ModuleKindCommonJS, nil)
555+
resolution, trace := p.resolver.ResolveModuleName(libraryName, resolveFrom, core.ModuleKindCommonJS, nil)
537556
if resolution.IsResolved() {
538557
path = resolution.ResolvedFileName
539558
replaced = true
540-
p.pathForLibFileResolutions.LoadOrStore(p.toPath(resolveFrom), module.ModeAwareCache[*module.ResolvedModule]{
541-
module.ModeAwareCacheKey{Name: libraryName, Mode: core.ModuleKindCommonJS}: resolution,
559+
p.pathForLibFileResolutions.LoadOrStore(p.toPath(resolveFrom), &libResolution{
560+
libraryName: libraryName,
561+
resolution: resolution,
562+
trace: trace,
542563
})
543564
}
544565
}

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
@@ -99,8 +101,9 @@ func (t *parseTask) redirect(loader *fileLoader, fileName string) {
99101
}
100102

101103
func (t *parseTask) loadAutomaticTypeDirectives(loader *fileLoader) {
102-
toParseTypeRefs, typeResolutionsInFile := loader.resolveAutomaticTypeDirectives(t.normalizedFilePath)
104+
toParseTypeRefs, typeResolutionsInFile, typeResolutionsTrace := loader.resolveAutomaticTypeDirectives(t.normalizedFilePath)
103105
t.typeResolutionsInFile = typeResolutionsInFile
106+
t.typeResolutionsTrace = typeResolutionsTrace
104107
for _, typeResolution := range toParseTypeRefs {
105108
t.addSubTask(typeResolution, nil)
106109
}
@@ -194,30 +197,32 @@ func (w *filesParser) start(loader *fileLoader, tasks []*parseTask, depth int, i
194197
}
195198
}
196199

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

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

internal/compiler/host.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,35 @@ 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 {
53+
if trace == nil {
54+
trace = func(msg string) {}
55+
}
5056
return &compilerHost{
5157
currentDirectory: currentDirectory,
5258
fs: fs,
5359
defaultLibraryPath: defaultLibraryPath,
5460
extendedConfigCache: extendedConfigCache,
61+
trace: trace,
5562
}
5663
}
5764

@@ -68,7 +75,7 @@ func (h *compilerHost) GetCurrentDirectory() string {
6875
}
6976

7077
func (h *compilerHost) Trace(msg string) {
71-
//!!! TODO: implement
78+
h.trace(msg)
7279
}
7380

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

internal/compiler/program_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package compiler
1+
package compiler_test
22

33
import (
44
"path/filepath"
@@ -7,6 +7,7 @@ import (
77
"testing"
88

99
"github.com/microsoft/typescript-go/internal/bundled"
10+
"github.com/microsoft/typescript-go/internal/compiler"
1011
"github.com/microsoft/typescript-go/internal/core"
1112
"github.com/microsoft/typescript-go/internal/repo"
1213
"github.com/microsoft/typescript-go/internal/tsoptions"
@@ -233,14 +234,14 @@ func TestProgram(t *testing.T) {
233234

234235
opts := core.CompilerOptions{Target: testCase.target}
235236

236-
program := NewProgram(ProgramOptions{
237+
program := compiler.NewProgram(compiler.ProgramOptions{
237238
Config: &tsoptions.ParsedCommandLine{
238239
ParsedConfig: &core.ParsedOptions{
239240
FileNames: []string{"c:/dev/src/index.ts"},
240241
CompilerOptions: &opts,
241242
},
242243
},
243-
Host: NewCompilerHost("c:/dev/src", fs, bundled.LibPath(), nil),
244+
Host: compiler.NewCompilerHost("c:/dev/src", fs, bundled.LibPath(), nil, nil),
244245
})
245246

246247
actualFiles := []string{}
@@ -270,18 +271,18 @@ func BenchmarkNewProgram(b *testing.B) {
270271
}
271272

272273
opts := core.CompilerOptions{Target: testCase.target}
273-
programOpts := ProgramOptions{
274+
programOpts := compiler.ProgramOptions{
274275
Config: &tsoptions.ParsedCommandLine{
275276
ParsedConfig: &core.ParsedOptions{
276277
FileNames: []string{"c:/dev/src/index.ts"},
277278
CompilerOptions: &opts,
278279
},
279280
},
280-
Host: NewCompilerHost("c:/dev/src", fs, bundled.LibPath(), nil),
281+
Host: compiler.NewCompilerHost("c:/dev/src", fs, bundled.LibPath(), nil, nil),
281282
}
282283

283284
for b.Loop() {
284-
NewProgram(programOpts)
285+
compiler.NewProgram(programOpts)
285286
}
286287
})
287288
}
@@ -294,18 +295,18 @@ func BenchmarkNewProgram(b *testing.B) {
294295
fs := osvfs.FS()
295296
fs = bundled.WrapFS(fs)
296297

297-
host := NewCompilerHost(rootPath, fs, bundled.LibPath(), nil)
298+
host := compiler.NewCompilerHost(rootPath, fs, bundled.LibPath(), nil, nil)
298299

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

302-
opts := ProgramOptions{
303+
opts := compiler.ProgramOptions{
303304
Config: parsed,
304305
Host: host,
305306
}
306307

307308
for b.Loop() {
308-
NewProgram(opts)
309+
compiler.NewProgram(opts)
309310
}
310311
})
311312
}

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/testsys_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/microsoft/typescript-go/internal/core"
1616
"github.com/microsoft/typescript-go/internal/execute"
1717
"github.com/microsoft/typescript-go/internal/incremental"
18+
"github.com/microsoft/typescript-go/internal/testutil/harnessutil"
1819
"github.com/microsoft/typescript-go/internal/testutil/incrementaltestutil"
1920
"github.com/microsoft/typescript-go/internal/testutil/stringtestutil"
2021
"github.com/microsoft/typescript-go/internal/tsoptions"
@@ -184,7 +185,7 @@ func (s *testSys) EndWrite() {
184185
// todo: revisit if improving tsc/build/watch unittest baselines
185186
output := s.currentWrite.String()
186187
s.currentWrite.Reset()
187-
output = sanitizeSysOutput(output, "Version "+core.Version(), "Version FakeTSVersion\n")
188+
output = sanitizeSysOutput(output, "Version "+core.Version(), "Version "+harnessutil.FakeTSVersion+"\n")
188189
output = sanitizeSysOutput(output, "build starting at ", "")
189190
output = sanitizeSysOutput(output, "build finished in ", "")
190191
s.output = append(s.output, output)

0 commit comments

Comments
 (0)