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) }