Skip to content

Commit 3bb6981

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

File tree

1 file changed

+68
-7
lines changed

1 file changed

+68
-7
lines changed

internal/testutil/harnessutil/harnessutil.go

Lines changed: 68 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,74 @@ 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+
file := strings.TrimPrefix(str, "File '")
537+
filePath := tspath.ToPath(file, t.currentDirectory, t.fs.UseCaseSensitiveFileNames())
538+
if _, has := t.packageJsonCache[filePath]; has {
539+
return msg
540+
} else {
541+
t.packageJsonCache[filePath] = false
542+
return fmt.Sprintf("File '%s' does not exist.", file)
543+
}
522544
}
545+
if str := strings.TrimSuffix(msg, "' does not exist."); str != msg {
546+
file := strings.TrimPrefix(str, "File '")
547+
filePath := tspath.ToPath(file, t.currentDirectory, t.fs.UseCaseSensitiveFileNames())
548+
if _, has := t.packageJsonCache[filePath]; !has {
549+
t.packageJsonCache[filePath] = false
550+
return msg
551+
} else {
552+
return fmt.Sprintf("File '%s' does not exist according to earlier cached lookups.", file)
553+
}
554+
}
555+
if str := strings.TrimSuffix(msg, "' exists according to earlier cached lookups."); str != msg {
556+
file := strings.TrimPrefix(str, "File '")
557+
filePath := tspath.ToPath(file, t.currentDirectory, t.fs.UseCaseSensitiveFileNames())
558+
if _, has := t.packageJsonCache[filePath]; has {
559+
return msg
560+
} else {
561+
t.packageJsonCache[filePath] = true
562+
return fmt.Sprintf("Found 'package.json' at '%s'.", file)
563+
}
564+
}
565+
if str := strings.TrimPrefix(msg, "Found 'package.json' at '"); str != msg {
566+
file := strings.TrimSuffix(str, "'.")
567+
filePath := tspath.ToPath(file, t.currentDirectory, t.fs.UseCaseSensitiveFileNames())
568+
if _, has := t.packageJsonCache[filePath]; !has {
569+
t.packageJsonCache[filePath] = true
570+
return msg
571+
} else {
572+
return fmt.Sprintf("File '%s' exists according to earlier cached lookups.", file)
573+
}
574+
}
575+
return msg
576+
}
577+
578+
func (t *tracer) string() string {
579+
return t.builder.String()
580+
}
581+
582+
func createCompilerHost(fs vfs.FS, defaultLibraryPath string, currentDirectory string) *cachedCompilerHost {
583+
tracer := tracer{fs: fs, currentDirectory: currentDirectory, packageJsonCache: make(map[tspath.Path]bool)}
523584
return &cachedCompilerHost{
524-
CompilerHost: compiler.NewCompilerHost(currentDirectory, fs, defaultLibraryPath, nil, trace),
585+
CompilerHost: compiler.NewCompilerHost(currentDirectory, fs, defaultLibraryPath, nil, tracer.trace),
525586
tracer: &tracer,
526587
}
527588
}

0 commit comments

Comments
 (0)