Skip to content

Commit 0522ac5

Browse files
Fix incorrectly handled paths for file include reason (#1716)
Co-authored-by: Copilot <[email protected]>
1 parent 9275bc8 commit 0522ac5

10 files changed

+91
-15
lines changed

internal/compiler/fileloader.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ func processAllProgramFiles(
254254
unsupportedExtensions: unsupportedExtensions,
255255
sourceFilesFoundSearchingNodeModules: sourceFilesFoundSearchingNodeModules,
256256
libFiles: libFilesMap,
257+
missingFiles: missingFiles,
257258
includeProcessor: loader.includeProcessor,
258259
outputFileToProjectReferenceSource: outputFileToProjectReferenceSource,
259260
}

internal/compiler/filesparser.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,16 +240,15 @@ func (w *filesParser) collect(loader *fileLoader, tasks []*parseTask, iterate fu
240240

241241
func (w *filesParser) collectWorker(loader *fileLoader, tasks []*parseTask, iterate func(*parseTask), seen collections.Set[*parseTask]) {
242242
for _, task := range tasks {
243-
if task.redirectedParseTask == nil {
243+
// Exclude automatic type directive tasks from include reason processing,
244+
// as these are internal implementation details and should not contribute
245+
// to the reasons for including files.
246+
if task.redirectedParseTask == nil && !task.isForAutomaticTypeDirective {
244247
includeReason := task.includeReason
245248
if task.loadedTask != nil {
246249
task = task.loadedTask
247250
}
248-
if existing, ok := loader.includeProcessor.fileIncludeReasons[task.path]; ok {
249-
loader.includeProcessor.fileIncludeReasons[task.path] = append(existing, includeReason)
250-
} else {
251-
loader.includeProcessor.fileIncludeReasons[task.path] = []*fileIncludeReason{includeReason}
252-
}
251+
w.addIncludeReason(loader, task, includeReason)
253252
}
254253
// ensure we only walk each task once
255254
if !task.loaded || !seen.AddIfAbsent(task) {
@@ -267,3 +266,15 @@ func (w *filesParser) collectWorker(loader *fileLoader, tasks []*parseTask, iter
267266
iterate(task)
268267
}
269268
}
269+
270+
func (w *filesParser) addIncludeReason(loader *fileLoader, task *parseTask, reason *fileIncludeReason) {
271+
if task.redirectedParseTask != nil {
272+
w.addIncludeReason(loader, task.redirectedParseTask, reason)
273+
} else if task.loaded {
274+
if existing, ok := loader.includeProcessor.fileIncludeReasons[task.path]; ok {
275+
loader.includeProcessor.fileIncludeReasons[task.path] = append(existing, reason)
276+
} else {
277+
loader.includeProcessor.fileIncludeReasons[task.path] = []*fileIncludeReason{reason}
278+
}
279+
}
280+
}

internal/compiler/program.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,6 +1541,18 @@ func (p *Program) GetSourceFiles() []*ast.SourceFile {
15411541
return p.files
15421542
}
15431543

1544+
// Testing only
1545+
func (p *Program) GetIncludeReasons() map[tspath.Path][]*fileIncludeReason {
1546+
return p.includeProcessor.fileIncludeReasons
1547+
}
1548+
1549+
// Testing only
1550+
func (p *Program) IsMissingPath(path tspath.Path) bool {
1551+
return slices.ContainsFunc(p.missingFiles, func(missingPath string) bool {
1552+
return p.toPath(missingPath) == path
1553+
})
1554+
}
1555+
15441556
func (p *Program) ExplainFiles(w io.Writer) {
15451557
toRelativeFileName := func(fileName string) string {
15461558
return tspath.GetRelativePathFromDirectory(p.GetCurrentDirectory(), fileName, p.comparePathsOptions)

internal/execute/tsctests/runner.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ func (test *tscInput) run(t *testing.T, scenario string) {
8080
sys.baselineFSwithDiff(baselineBuilder)
8181
result := test.executeCommand(sys, baselineBuilder, test.commandLineArgs)
8282
sys.serializeState(baselineBuilder)
83-
sys.baselinePrograms(baselineBuilder)
84-
var unexpectedDiff string
83+
unexpectedDiff := sys.baselinePrograms(baselineBuilder, "Initial build")
8584

8685
for index, do := range test.edits {
8786
sys.clearOutput()
@@ -101,7 +100,7 @@ func (test *tscInput) run(t *testing.T, scenario string) {
101100
result.Watcher.DoCycle()
102101
}
103102
sys.serializeState(baselineBuilder)
104-
sys.baselinePrograms(baselineBuilder)
103+
unexpectedDiff += sys.baselinePrograms(baselineBuilder, fmt.Sprintf("Edit [%d]:: %s\n", index, do.caption))
105104
})
106105
wg.Queue(func() {
107106
// Compute build with all the edits

internal/execute/tsctests/sys.go

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ type snapshot struct {
141141
type testSys struct {
142142
currentWrite *strings.Builder
143143
programBaselines strings.Builder
144+
programIncludeBaselines strings.Builder
144145
tracer *harnessutil.TracerForBaselining
145146
serializedDiff *snapshot
146147
forIncrementalCorrectness bool
@@ -287,18 +288,23 @@ func (s *testSys) GetTrace(w io.Writer) func(str string) {
287288
}
288289
}
289290

290-
func (s *testSys) OnProgram(program *incremental.Program) {
291-
if s.programBaselines.Len() != 0 {
292-
s.programBaselines.WriteString("\n")
291+
func (s *testSys) writeHeaderToBaseline(builder *strings.Builder, program *incremental.Program) {
292+
if builder.Len() != 0 {
293+
builder.WriteString("\n")
293294
}
294295

295-
testingData := program.GetTestingData()
296296
if configFilePath := program.Options().ConfigFilePath; configFilePath != "" {
297-
s.programBaselines.WriteString(tspath.GetRelativePathFromDirectory(s.cwd, configFilePath, tspath.ComparePathsOptions{
297+
builder.WriteString(tspath.GetRelativePathFromDirectory(s.cwd, configFilePath, tspath.ComparePathsOptions{
298298
UseCaseSensitiveFileNames: s.FS().UseCaseSensitiveFileNames(),
299299
CurrentDirectory: s.GetCurrentDirectory(),
300300
}) + "::\n")
301301
}
302+
}
303+
304+
func (s *testSys) OnProgram(program *incremental.Program) {
305+
s.writeHeaderToBaseline(&s.programBaselines, program)
306+
307+
testingData := program.GetTestingData()
302308
s.programBaselines.WriteString("SemanticDiagnostics::\n")
303309
for _, file := range program.GetProgram().GetSourceFiles() {
304310
if diagnostics, ok := testingData.SemanticDiagnosticsPerFile.Load(file.Path()); ok {
@@ -324,11 +330,44 @@ func (s *testSys) OnProgram(program *incremental.Program) {
324330
}
325331
}
326332
}
333+
334+
var filesWithoutIncludeReason []string
335+
var fileNotInProgramWithIncludeReason []string
336+
includeReasons := program.GetProgram().GetIncludeReasons()
337+
for _, file := range program.GetProgram().GetSourceFiles() {
338+
if _, ok := includeReasons[file.Path()]; !ok {
339+
filesWithoutIncludeReason = append(filesWithoutIncludeReason, string(file.Path()))
340+
}
341+
}
342+
for path := range includeReasons {
343+
if program.GetProgram().GetSourceFileByPath(path) == nil && !program.GetProgram().IsMissingPath(path) {
344+
fileNotInProgramWithIncludeReason = append(fileNotInProgramWithIncludeReason, string(path))
345+
}
346+
}
347+
if len(filesWithoutIncludeReason) > 0 || len(fileNotInProgramWithIncludeReason) > 0 {
348+
s.writeHeaderToBaseline(&s.programIncludeBaselines, program)
349+
s.programIncludeBaselines.WriteString("!!! Expected all files to have include reasons\nfilesWithoutIncludeReason::\n")
350+
for _, file := range filesWithoutIncludeReason {
351+
s.programIncludeBaselines.WriteString(" " + file + "\n")
352+
}
353+
s.programIncludeBaselines.WriteString("filesNotInProgramWithIncludeReason::\n")
354+
for _, file := range fileNotInProgramWithIncludeReason {
355+
s.programIncludeBaselines.WriteString(" " + file + "\n")
356+
}
357+
}
327358
}
328359

329-
func (s *testSys) baselinePrograms(baseline *strings.Builder) {
360+
func (s *testSys) baselinePrograms(baseline *strings.Builder, header string) string {
330361
baseline.WriteString(s.programBaselines.String())
331362
s.programBaselines.Reset()
363+
var result string
364+
if s.programIncludeBaselines.Len() > 0 {
365+
result += fmt.Sprintf("\n\n%s\n!!! Include reasons expectations don't match pls review!!!\n", header)
366+
result += s.programIncludeBaselines.String()
367+
s.programIncludeBaselines.Reset()
368+
baseline.WriteString(result)
369+
}
370+
return result
332371
}
333372

334373
func (s *testSys) serializeState(baseline *strings.Builder) {

testdata/baselines/reference/tsbuild/roots/when-root-file-is-from-referenced-project-and-shared-is-first.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ projects/shared/dist/src/logging.d.ts
8888
File is output of project reference source 'projects/shared/src/logging.ts'
8989
projects/shared/dist/src/myClass.d.ts
9090
Matched by include pattern '../shared/src/**/*.ts' in 'projects/server/tsconfig.json'
91+
Imported via ':shared/myClass.js' from file 'projects/server/src/server.ts'
9192
File is output of project reference source 'projects/shared/src/myClass.ts'
9293
projects/shared/dist/src/random.d.ts
9394
Matched by include pattern '../shared/src/**/*.ts' in 'projects/server/tsconfig.json'
@@ -426,6 +427,7 @@ projects/shared/dist/src/logging.d.ts
426427
File is output of project reference source 'projects/shared/src/logging.ts'
427428
projects/shared/dist/src/myClass.d.ts
428429
Matched by include pattern '../shared/src/**/*.ts' in 'projects/server/tsconfig.json'
430+
Imported via ':shared/myClass.js' from file 'projects/server/src/server.ts'
429431
File is output of project reference source 'projects/shared/src/myClass.ts'
430432
projects/shared/dist/src/random.d.ts
431433
Matched by include pattern '../shared/src/**/*.ts' in 'projects/server/tsconfig.json'
@@ -699,6 +701,7 @@ projects/shared/dist/src/logging.d.ts
699701
File is output of project reference source 'projects/shared/src/logging.ts'
700702
projects/shared/dist/src/myClass.d.ts
701703
Matched by include pattern '../shared/src/**/*.ts' in 'projects/server/tsconfig.json'
704+
Imported via ':shared/myClass.js' from file 'projects/server/src/server.ts'
702705
File is output of project reference source 'projects/shared/src/myClass.ts'
703706
projects/server/src/server.ts
704707
Matched by include pattern 'src/**/*.ts' in 'projects/server/tsconfig.json'

testdata/baselines/reference/tsbuild/roots/when-root-file-is-from-referenced-project.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ File '/home/src/workspaces/solution/projects/shared/src/myClass.ts' exists - use
8484
../../tslibs/TS/Lib/lib.d.ts
8585
Default library for target 'ES5'
8686
projects/shared/dist/src/myClass.d.ts
87+
Imported via ':shared/myClass.js' from file 'projects/server/src/server.ts'
8788
Matched by include pattern '../shared/src/**/*.ts' in 'projects/server/tsconfig.json'
8889
File is output of project reference source 'projects/shared/src/myClass.ts'
8990
projects/server/src/server.ts
@@ -422,6 +423,7 @@ File '/home/src/workspaces/solution/projects/shared/src/myClass.ts' exists - use
422423
../../tslibs/TS/Lib/lib.d.ts
423424
Default library for target 'ES5'
424425
projects/shared/dist/src/myClass.d.ts
426+
Imported via ':shared/myClass.js' from file 'projects/server/src/server.ts'
425427
Matched by include pattern '../shared/src/**/*.ts' in 'projects/server/tsconfig.json'
426428
File is output of project reference source 'projects/shared/src/myClass.ts'
427429
projects/server/src/server.ts
@@ -695,6 +697,7 @@ File '/home/src/workspaces/solution/projects/shared/src/myClass.ts' exists - use
695697
../../tslibs/TS/Lib/lib.d.ts
696698
Default library for target 'ES5'
697699
projects/shared/dist/src/myClass.d.ts
700+
Imported via ':shared/myClass.js' from file 'projects/server/src/server.ts'
698701
Matched by include pattern '../shared/src/**/*.ts' in 'projects/server/tsconfig.json'
699702
File is output of project reference source 'projects/shared/src/myClass.ts'
700703
projects/server/src/server.ts

testdata/baselines/reference/tsbuild/sample/explainFiles.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ core/index.d.ts
104104
Imported via '../core/index' from file 'tests/index.ts'
105105
File is output of project reference source 'core/index.ts'
106106
core/anotherModule.d.ts
107+
Imported via '../core/anotherModule' from file 'logic/index.d.ts'
107108
Imported via '../core/anotherModule' from file 'tests/index.ts'
108109
File is output of project reference source 'core/anotherModule.ts'
109110
logic/index.d.ts
@@ -527,6 +528,7 @@ core/index.d.ts
527528
Imported via '../core/index' from file 'tests/index.ts'
528529
File is output of project reference source 'core/index.ts'
529530
core/anotherModule.d.ts
531+
Imported via '../core/anotherModule' from file 'logic/index.d.ts'
530532
Imported via '../core/anotherModule' from file 'tests/index.ts'
531533
File is output of project reference source 'core/anotherModule.ts'
532534
logic/index.d.ts

testdata/baselines/reference/tsbuildWatch/roots/when-root-file-is-from-referenced-project-and-shared-is-first.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ projects/shared/dist/src/logging.d.ts
9090
File is output of project reference source 'projects/shared/src/logging.ts'
9191
projects/shared/dist/src/myClass.d.ts
9292
Matched by include pattern '../shared/src/**/*.ts' in 'projects/server/tsconfig.json'
93+
Imported via ':shared/myClass.js' from file 'projects/server/src/server.ts'
9394
File is output of project reference source 'projects/shared/src/myClass.ts'
9495
projects/shared/dist/src/random.d.ts
9596
Matched by include pattern '../shared/src/**/*.ts' in 'projects/server/tsconfig.json'
@@ -422,6 +423,7 @@ projects/shared/dist/src/logging.d.ts
422423
File is output of project reference source 'projects/shared/src/logging.ts'
423424
projects/shared/dist/src/myClass.d.ts
424425
Matched by include pattern '../shared/src/**/*.ts' in 'projects/server/tsconfig.json'
426+
Imported via ':shared/myClass.js' from file 'projects/server/src/server.ts'
425427
File is output of project reference source 'projects/shared/src/myClass.ts'
426428
projects/shared/dist/src/random.d.ts
427429
Matched by include pattern '../shared/src/**/*.ts' in 'projects/server/tsconfig.json'
@@ -689,6 +691,7 @@ projects/shared/dist/src/logging.d.ts
689691
File is output of project reference source 'projects/shared/src/logging.ts'
690692
projects/shared/dist/src/myClass.d.ts
691693
Matched by include pattern '../shared/src/**/*.ts' in 'projects/server/tsconfig.json'
694+
Imported via ':shared/myClass.js' from file 'projects/server/src/server.ts'
692695
File is output of project reference source 'projects/shared/src/myClass.ts'
693696
projects/server/src/server.ts
694697
Matched by include pattern 'src/**/*.ts' in 'projects/server/tsconfig.json'

testdata/baselines/reference/tsbuildWatch/roots/when-root-file-is-from-referenced-project.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ File '/home/src/workspaces/solution/projects/shared/src/myClass.ts' exists - use
8686
../../tslibs/TS/Lib/lib.d.ts
8787
Default library for target 'ES5'
8888
projects/shared/dist/src/myClass.d.ts
89+
Imported via ':shared/myClass.js' from file 'projects/server/src/server.ts'
8990
Matched by include pattern '../shared/src/**/*.ts' in 'projects/server/tsconfig.json'
9091
File is output of project reference source 'projects/shared/src/myClass.ts'
9192
projects/server/src/server.ts
@@ -418,6 +419,7 @@ File '/home/src/workspaces/solution/projects/shared/src/myClass.ts' exists - use
418419
../../tslibs/TS/Lib/lib.d.ts
419420
Default library for target 'ES5'
420421
projects/shared/dist/src/myClass.d.ts
422+
Imported via ':shared/myClass.js' from file 'projects/server/src/server.ts'
421423
Matched by include pattern '../shared/src/**/*.ts' in 'projects/server/tsconfig.json'
422424
File is output of project reference source 'projects/shared/src/myClass.ts'
423425
projects/server/src/server.ts
@@ -685,6 +687,7 @@ File '/home/src/workspaces/solution/projects/shared/src/myClass.ts' exists - use
685687
../../tslibs/TS/Lib/lib.d.ts
686688
Default library for target 'ES5'
687689
projects/shared/dist/src/myClass.d.ts
690+
Imported via ':shared/myClass.js' from file 'projects/server/src/server.ts'
688691
Matched by include pattern '../shared/src/**/*.ts' in 'projects/server/tsconfig.json'
689692
File is output of project reference source 'projects/shared/src/myClass.ts'
690693
projects/server/src/server.ts

0 commit comments

Comments
 (0)