Skip to content

Fix indexing error in guessDirectorySymlink - #46105

Merged
Andrew Branch (andrewbranch) merged 2 commits into
microsoft:mainfrom
andrewbranch:bug/44953
Sep 27, 2021
Merged

Andrew Branch (andrewbranch) merged 2 commits into
microsoft:mainfrom
andrewbranch:bug/44953

Conversation

@andrewbranch

@andrewbranch Andrew Branch (andrewbranch) commented Sep 27, 2021 •

Copy link
Copy Markdown
Member

Fixes #44953

I tried for about an hour to reverse engineer a failing test, but I wasn’t getting particularly close. I think some prerequisites of the failure are using project references, useSourceOfProjectReferenceRedirect (which I think requires watch/tsserver, not tsc?), and having some symlinks.

At any rate, I think the logical flaw this fixes is pretty obvious on its own and shouldn’t be held up by the fact that it’s only observable under pretty complicated conditions. This is something that would have been written correctly from the beginning, without tests, if written under --noUncheckedIndexedAccess.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather have a test, but looks like it hits a lot of projects and the code seems correct.

Comment thread src/compiler/utilities.ts Outdated
const bParts = getPathComponents(getNormalizedAbsolutePath(b, cwd));
let isDirectory = false;
while (!isNodeModulesOrScopedPackageDirectory(aParts[aParts.length - 2], getCanonicalFileName) &&
while (aParts.length && bParts.length &&

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also check if they both have a length >= 2, right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I forgot that / will be the first component all by itself, and / is never going to be a symlink itself, so that’s fine

Comment thread src/compiler/utilities.ts
Comment on lines +6399 to +6400
function isNodeModulesOrScopedPackageDirectory(s: string | undefined, getCanonicalFileName: GetCanonicalFileName): boolean {
return s !== undefined && (getCanonicalFileName(s) === "node_modules" || startsWith(s, "@"));

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) Sep 27, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically you don't need this with the >= 2 thing, but it's a fine guard.

@andrewbranch
Andrew Branch (andrewbranch) merged commit 530b0e2 into microsoft:main Sep 27, 2021
@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Author: Team For Milestone Bug PRs that fix a bug with a specific milestone

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TS Server fatal error: Cannot read property 'lastIndexOf' of undefined

3 participants