From 3b0dfaa73cddccc868ffc3671bffd6d1660b39ae Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Tue, 15 Oct 2024 14:01:12 -0700 Subject: [PATCH] Handle auto-import when `paths` pattern is absolute (#60236) --- src/compiler/moduleSpecifiers.ts | 9 ++++++--- .../fourslash/autoImportPathsConfigDir.ts | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 tests/cases/fourslash/autoImportPathsConfigDir.ts diff --git a/src/compiler/moduleSpecifiers.ts b/src/compiler/moduleSpecifiers.ts index ac433ba8fed..5af3aab7d0f 100644 --- a/src/compiler/moduleSpecifiers.ts +++ b/src/compiler/moduleSpecifiers.ts @@ -613,7 +613,7 @@ function getLocalModuleSpecifier(moduleFileName: string, info: Info, compilerOpt prefersTsExtension(allowedEndings), ); - const fromPaths = pathsOnly || fromPackageJsonImports === undefined ? paths && tryGetModuleNameFromPaths(relativeToBaseUrl, paths, allowedEndings, host, compilerOptions) : undefined; + const fromPaths = pathsOnly || fromPackageJsonImports === undefined ? paths && tryGetModuleNameFromPaths(relativeToBaseUrl, paths, allowedEndings, baseDirectory, getCanonicalFileName, host, compilerOptions) : undefined; if (pathsOnly) { return fromPaths; } @@ -925,10 +925,11 @@ function tryGetModuleNameFromAmbientModule(moduleSymbol: Symbol, checker: TypeCh } } -function tryGetModuleNameFromPaths(relativeToBaseUrl: string, paths: MapLike, allowedEndings: ModuleSpecifierEnding[], host: ModuleSpecifierResolutionHost, compilerOptions: CompilerOptions): string | undefined { +function tryGetModuleNameFromPaths(relativeToBaseUrl: string, paths: MapLike, allowedEndings: ModuleSpecifierEnding[], baseDirectory: string, getCanonicalFileName: GetCanonicalFileName, host: ModuleSpecifierResolutionHost, compilerOptions: CompilerOptions): string | undefined { for (const key in paths) { for (const patternText of paths[key]) { - const pattern = normalizePath(patternText); + const normalized = normalizePath(patternText); + const pattern = getRelativePathIfInSameVolume(normalized, baseDirectory, getCanonicalFileName) ?? normalized; const indexOfStar = pattern.indexOf("*"); // In module resolution, if `pattern` itself has an extension, a file with that extension is looked up directly, // meaning a '.ts' or '.d.ts' extension is allowed to resolve. This is distinct from the case where a '*' substitution @@ -1290,6 +1291,8 @@ function tryGetModuleNameAsNodeModule({ path, isRedirect }: ModulePath, { getCan subModuleName, versionPaths.paths, allowedEndings, + packageRootPath, + getCanonicalFileName, host, options, ); diff --git a/tests/cases/fourslash/autoImportPathsConfigDir.ts b/tests/cases/fourslash/autoImportPathsConfigDir.ts new file mode 100644 index 00000000000..0b451767b32 --- /dev/null +++ b/tests/cases/fourslash/autoImportPathsConfigDir.ts @@ -0,0 +1,18 @@ +/// + +// @Filename: tsconfig.json +//// { +//// "compilerOptions": { +//// "paths": { +//// "@root/*": ["${configDir}/src/*"] +//// } +//// } +//// } + +// @Filename: src/one.ts +//// export const one = 1; + +// @Filename: src/foo/two.ts +//// one/**/ + +verify.importFixModuleSpecifiers("", ["@root/one"]);