From e773f94abc4dcfc310ec6690c99f133b00f5efe9 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Fri, 4 Dec 2026 10:02:33 -0800 Subject: [PATCH] compiler, symlinks: look up known directory symlinks by ancestor path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Checking whether a node_modules path falls under a known symlink previously called Size() and Range() across every known directory symlink on each probe. Track whether any directory symlinks exist with an atomic bool and walk parent PathKey ancestors directly in the directory map. benchstat (-pgo=off, parent vs commit, n=6): goos: linux goarch: amd64 │ old │ new │ │ sec/op │ sec/op vs base │ Tsc/vscode 6.515 ± 1% 6.390 ± 3% ~ (p=0.065 n=6) Tsc/mui-docs 3.917 ± 3% 3.883 ± 2% ~ (p=0.310 n=6) geomean 5.052 4.981 -1.39% │ old │ new │ │ B-rss │ B-rss vs base │ Tsc/vscode 4.651Gi ± 0% 4.845Gi ± 4% +4.17% (p=0.026 n=6) Tsc/mui-docs 2.215Gi ± 8% 2.199Gi ± 6% ~ (p=0.818 n=6) geomean 3.210Gi 3.264Gi +1.70% --- .../compiler/projectreferencedtsfakinghost.go | 60 +++++++++++++------ tsc/internal/symlinks/knownsymlinks.go | 31 ++++++++++ 2 files changed, 73 insertions(+), 18 deletions(-) diff --git a/tsc/internal/compiler/projectreferencedtsfakinghost.go b/tsc/internal/compiler/projectreferencedtsfakinghost.go index 9eacfa3e64318..a8c1c059720b8 100644 --- a/tsc/internal/compiler/projectreferencedtsfakinghost.go +++ b/tsc/internal/compiler/projectreferencedtsfakinghost.go @@ -200,10 +200,10 @@ func (fs *projectReferenceDtsFakingVfs) fileOrDirectoryExistsUsingSource( if packageRoot != "" { fs.handleDirectoryCouldBeSymlink(packageRoot) } - knownDirectoryLinks := fs.knownSymlinks.Directories() - if knownDirectoryLinks.Size() == 0 { + if !fs.knownSymlinks.HasAnyDirectory() { return false } + knownDirectoryLinks := fs.knownSymlinks.Directories() if onFileExists != nil { _, ok := fs.knownSymlinks.Files().Load(fileOrDirectoryPath) if ok { @@ -211,25 +211,49 @@ func (fs *projectReferenceDtsFakingVfs) fileOrDirectoryExistsUsingSource( } } - // If it contains node_modules check if its one of the symlinked path we know of - var exists bool - knownDirectoryLinks.Range(func(directoryPath tspath.PathKey, knownDirectoryLink *symlinks.KnownDirectoryLink) bool { - if directoryPath == fileOrDirectoryPath || !directoryPath.ContainsPath(fileOrDirectoryPath) { + // If a directory link was stored with an uppercase non-dynamic root on a case-sensitive host, + // fall back to ContainsPath which compares path roots case-insensitively. + if fs.knownSymlinks.HasUppercaseRoot() { + var exists bool + knownDirectoryLinks.Range(func(directoryPath tspath.PathKey, knownDirectoryLink *symlinks.KnownDirectoryLink) bool { + if knownDirectoryLink == nil || directoryPath == fileOrDirectoryPath || !directoryPath.ContainsPath(fileOrDirectoryPath) { + return true + } + realFileOrDirectory, ok := knownDirectoryLink.ResolveFilePath(tspath.RootedFilePathFromPath(fileOrDirectory), fs.CaseSensitivity()) + if !ok { + panic("canonical symlink path did not match its presentation path") + } + if exists = existsUsingSource(tspath.RootedPath(realFileOrDirectory)).IsTrue(); exists { + if onFileExists != nil { + onFileExists(realFileOrDirectory) + } + return false + } return true - } - realFileOrDirectory, ok := knownDirectoryLink.ResolveFilePath(tspath.RootedFilePathFromPath(fileOrDirectory), fs.CaseSensitivity()) - if !ok { - panic("canonical symlink path did not match its presentation path") - } - if exists = existsUsingSource(tspath.RootedPath(realFileOrDirectory)).IsTrue(); exists { - if onFileExists != nil { - onFileExists(realFileOrDirectory) + }) + return exists + } + // If it contains node_modules check if any ancestor directory is a known symlink + for dir := fileOrDirectoryPath.Parent(); ; { + if knownDirectoryLink, ok := knownDirectoryLinks.Load(dir); ok && knownDirectoryLink != nil { + realFileOrDirectory, ok := knownDirectoryLink.ResolveFilePath(tspath.RootedFilePathFromPath(fileOrDirectory), fs.CaseSensitivity()) + if !ok { + panic("canonical symlink path did not match its presentation path") + } + if existsUsingSource(tspath.RootedPath(realFileOrDirectory)).IsTrue() { + if onFileExists != nil { + onFileExists(realFileOrDirectory) + } + return true } - return false } - return true - }) - return exists + parent := dir.Parent() + if parent == dir { + break + } + dir = parent + } + return false } func (fs *projectReferenceDtsFakingVfs) fileExistsIfProjectReferenceDts(file tspath.RootedFilePath) core.Tristate { diff --git a/tsc/internal/symlinks/knownsymlinks.go b/tsc/internal/symlinks/knownsymlinks.go index fa20eeef50f84..a0e6e25e497ec 100644 --- a/tsc/internal/symlinks/knownsymlinks.go +++ b/tsc/internal/symlinks/knownsymlinks.go @@ -2,6 +2,7 @@ package symlinks import ( "strings" + "sync/atomic" "github.com/microsoft/TypeScript/tsc/internal/ast" "github.com/microsoft/TypeScript/tsc/internal/collections" @@ -26,6 +27,30 @@ type KnownSymlinks struct { files collections.SyncMap[tspath.PathKey, tspath.RootedFilePath] filesByRealpath collections.SyncMap[tspath.PathKey, *collections.SyncSet[tspath.RootedFilePath]] caseSensitivity tspath.CaseSensitivity + hasDirectories atomic.Bool + hasUppercaseRoot atomic.Bool +} + +func (cache *KnownSymlinks) HasAnyDirectory() bool { + return cache.hasDirectories.Load() +} + +func (cache *KnownSymlinks) HasUppercaseRoot() bool { + return cache.hasUppercaseRoot.Load() +} + +func hasNonDynamicUppercaseRoot(p tspath.PathKey) bool { + s := string(p) + if tspath.IsEncodedDynamicFileName(s) { + return false + } + root := s[:tspath.GetRootLength(s)] + for i := range len(root) { + if 'A' <= root[i] && root[i] <= 'Z' { + return true + } + } + return false } func (cache *KnownSymlinks) HasDirectory(symlinkPath tspath.PathKey) bool { @@ -62,6 +87,12 @@ func (cache *KnownSymlinks) SetDirectory(symlink tspath.RootedDirectoryPath, sym set.Add(symlink) } } + if realDirectory != nil { + if hasNonDynamicUppercaseRoot(symlinkPath) { + cache.hasUppercaseRoot.Store(true) + } + cache.hasDirectories.Store(true) + } cache.directories.Store(symlinkPath, realDirectory) }