Skip to content

Commit 8518989

Browse files
committed
Sanitize trace with fs caching traces as they are not deterministic
1 parent 6704de6 commit 8518989

File tree

1 file changed

+46
-7
lines changed

1 file changed

+46
-7
lines changed

internal/testutil/harnessutil/harnessutil.go

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func CompileFilesEx(
223223
Errors: errors,
224224
}, harnessOptions)
225225
result.Symlinks = symlinks
226-
result.Trace = host.tracer.String()
226+
result.Trace = host.tracer.string()
227227
result.Repeat = func(testConfig TestConfiguration) *CompilationResult {
228228
newHarnessOptions := *harnessOptions
229229
newCompilerOptions := compilerOptions.Clone()
@@ -474,7 +474,7 @@ func getOptionValue(t *testing.T, option *tsoptions.CommandLineOption, value str
474474

475475
type cachedCompilerHost struct {
476476
compiler.CompilerHost
477-
tracer *strings.Builder
477+
tracer *tracer
478478
}
479479

480480
var sourceFileCache collections.SyncMap[SourceFileCacheKey, *ast.SourceFile]
@@ -515,13 +515,52 @@ func (h *cachedCompilerHost) GetSourceFile(opts ast.SourceFileParseOptions) *ast
515515
return result
516516
}
517517

518-
func createCompilerHost(fs vfs.FS, defaultLibraryPath string, currentDirectory string) *cachedCompilerHost {
519-
var tracer strings.Builder
520-
trace := func(msg string) {
521-
fmt.Fprintln(&tracer, strings.Replace(msg, "'"+core.Version()+"'", "'"+FakeTSVersion+"'", 1))
518+
type tracer struct {
519+
fs vfs.FS
520+
currentDirectory string
521+
packageJsonCache map[tspath.Path]bool
522+
builder strings.Builder
523+
}
524+
525+
func (t *tracer) trace(msg string) {
526+
fmt.Fprintln(&t.builder, t.sanitizeTrace(msg))
527+
}
528+
529+
func (t *tracer) sanitizeTrace(msg string) string {
530+
// Version
531+
if str := strings.Replace(msg, "'"+core.Version()+"'", "'"+FakeTSVersion+"'", 1); str != msg {
532+
return str
533+
}
534+
// caching of fs in trace to be replaces with non caching version
535+
if str := strings.TrimSuffix(msg, "' does not exist according to earlier cached lookups"); str != msg {
536+
filePath := tspath.ToPath(strings.TrimPrefix(str, "File '"), t.currentDirectory, t.fs.UseCaseSensitiveFileNames())
537+
if _, has := t.packageJsonCache[filePath]; has {
538+
return msg
539+
} else {
540+
t.packageJsonCache[filePath] = false
541+
return fmt.Sprintf("File '%s' does not exist.", filePath)
542+
}
522543
}
544+
if str := strings.TrimSuffix(msg, "' exists according to earlier cached lookups"); str != msg {
545+
filePath := tspath.ToPath(strings.TrimPrefix(str, "File '"), t.currentDirectory, t.fs.UseCaseSensitiveFileNames())
546+
if _, has := t.packageJsonCache[filePath]; has {
547+
return msg
548+
} else {
549+
t.packageJsonCache[filePath] = true
550+
return fmt.Sprintf("Found 'package.json' at '%s'.", filePath)
551+
}
552+
}
553+
return msg
554+
}
555+
556+
func (t *tracer) string() string {
557+
return t.builder.String()
558+
}
559+
560+
func createCompilerHost(fs vfs.FS, defaultLibraryPath string, currentDirectory string) *cachedCompilerHost {
561+
tracer := tracer{fs: fs, currentDirectory: currentDirectory, packageJsonCache: make(map[tspath.Path]bool)}
523562
return &cachedCompilerHost{
524-
CompilerHost: compiler.NewCompilerHost(currentDirectory, fs, defaultLibraryPath, nil, trace),
563+
CompilerHost: compiler.NewCompilerHost(currentDirectory, fs, defaultLibraryPath, nil, tracer.trace),
525564
tracer: &tracer,
526565
}
527566
}

0 commit comments

Comments
 (0)