Skip to content

Commit 530b0e2

Browse files
authored
Fix indexing error in guessDirectorySymlink (#46105)
* Fix indexing error in guessDirectorySymlink * Add test
1 parent aabba1a commit 530b0e2

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/compiler/utilities.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6381,9 +6381,12 @@ namespace ts {
63816381
const aParts = getPathComponents(getNormalizedAbsolutePath(a, cwd));
63826382
const bParts = getPathComponents(getNormalizedAbsolutePath(b, cwd));
63836383
let isDirectory = false;
6384-
while (!isNodeModulesOrScopedPackageDirectory(aParts[aParts.length - 2], getCanonicalFileName) &&
6384+
while (
6385+
aParts.length >= 2 && bParts.length >= 2 &&
6386+
!isNodeModulesOrScopedPackageDirectory(aParts[aParts.length - 2], getCanonicalFileName) &&
63856387
!isNodeModulesOrScopedPackageDirectory(bParts[bParts.length - 2], getCanonicalFileName) &&
6386-
getCanonicalFileName(aParts[aParts.length - 1]) === getCanonicalFileName(bParts[bParts.length - 1])) {
6388+
getCanonicalFileName(aParts[aParts.length - 1]) === getCanonicalFileName(bParts[bParts.length - 1])
6389+
) {
63876390
aParts.pop();
63886391
bParts.pop();
63896392
isDirectory = true;
@@ -6393,8 +6396,8 @@ namespace ts {
63936396

63946397
// KLUDGE: Don't assume one 'node_modules' links to another. More likely a single directory inside the node_modules is the symlink.
63956398
// ALso, don't assume that an `@foo` directory is linked. More likely the contents of that are linked.
6396-
function isNodeModulesOrScopedPackageDirectory(s: string, getCanonicalFileName: GetCanonicalFileName): boolean {
6397-
return getCanonicalFileName(s) === "node_modules" || startsWith(s, "@");
6399+
function isNodeModulesOrScopedPackageDirectory(s: string | undefined, getCanonicalFileName: GetCanonicalFileName): boolean {
6400+
return s !== undefined && (getCanonicalFileName(s) === "node_modules" || startsWith(s, "@"));
63986401
}
63996402

64006403
function stripLeadingDirectorySeparator(s: string): string | undefined {

src/testRunner/unittests/tsserver/symlinkCache.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ namespace ts.projectSystem {
5757
{ real: "/packages/dep/", realPath: "/packages/dep/" as Path }
5858
);
5959
});
60+
61+
it("works for paths close to the root", () => {
62+
const cache = createSymlinkCache("/", createGetCanonicalFileName(/*useCaseSensitiveFileNames*/ false));
63+
cache.setSymlinkedDirectoryFromSymlinkedFile("/foo", "/one/two/foo"); // Used to crash, #44953
64+
});
6065
});
6166

6267
function setup() {

0 commit comments

Comments
 (0)