From 461fb6562356c0b1431c9a5e12e01f44cb16efb7 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Wed, 12 Jan 2022 13:45:06 -0800 Subject: [PATCH] Fix for crash for auto import completions with a rooted rootDirs entry (#47411) * Add failing test case. * Guard against undefined relative path. --- src/compiler/moduleSpecifiers.ts | 4 ++-- .../autoImportsWithRootDirsAndRootedPath01.ts | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 tests/cases/fourslash/autoImportsWithRootDirsAndRootedPath01.ts diff --git a/src/compiler/moduleSpecifiers.ts b/src/compiler/moduleSpecifiers.ts index c17dfeabcf1..db0b49f4436 100644 --- a/src/compiler/moduleSpecifiers.ts +++ b/src/compiler/moduleSpecifiers.ts @@ -854,8 +854,8 @@ namespace ts.moduleSpecifiers { function getPathRelativeToRootDirs(path: string, rootDirs: readonly string[], getCanonicalFileName: GetCanonicalFileName): string | undefined { return firstDefined(rootDirs, rootDir => { - const relativePath = getRelativePathIfInDirectory(path, rootDir, getCanonicalFileName)!; // TODO: GH#18217 - return isPathRelativeToParent(relativePath) ? undefined : relativePath; + const relativePath = getRelativePathIfInDirectory(path, rootDir, getCanonicalFileName); + return relativePath !== undefined && isPathRelativeToParent(relativePath) ? undefined : relativePath; }); } diff --git a/tests/cases/fourslash/autoImportsWithRootDirsAndRootedPath01.ts b/tests/cases/fourslash/autoImportsWithRootDirsAndRootedPath01.ts new file mode 100644 index 00000000000..08073349cb1 --- /dev/null +++ b/tests/cases/fourslash/autoImportsWithRootDirsAndRootedPath01.ts @@ -0,0 +1,24 @@ +/// + +// @Filename: /dir/foo.ts +//// export function foo() {} + +// @Filename: /dir/bar.ts +//// /*$*/ + +// @Filename: /dir/tsconfig.json +////{ +//// "compilerOptions": { +//// "module": "amd", +//// "moduleResolution": "classic", +//// "rootDirs": ["D:/"] +//// } +////} + +goTo.marker("$"); +verify.completions({ + preferences: { + includeCompletionsForModuleExports: true, + allowIncompleteCompletions: true, + } +});