Skip to content

Commit 3b0dfaa

Browse files
authored
Handle auto-import when paths pattern is absolute (#60236)
1 parent 31de163 commit 3b0dfaa

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/compiler/moduleSpecifiers.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ function getLocalModuleSpecifier(moduleFileName: string, info: Info, compilerOpt
613613
prefersTsExtension(allowedEndings),
614614
);
615615

616-
const fromPaths = pathsOnly || fromPackageJsonImports === undefined ? paths && tryGetModuleNameFromPaths(relativeToBaseUrl, paths, allowedEndings, host, compilerOptions) : undefined;
616+
const fromPaths = pathsOnly || fromPackageJsonImports === undefined ? paths && tryGetModuleNameFromPaths(relativeToBaseUrl, paths, allowedEndings, baseDirectory, getCanonicalFileName, host, compilerOptions) : undefined;
617617
if (pathsOnly) {
618618
return fromPaths;
619619
}
@@ -925,10 +925,11 @@ function tryGetModuleNameFromAmbientModule(moduleSymbol: Symbol, checker: TypeCh
925925
}
926926
}
927927

928-
function tryGetModuleNameFromPaths(relativeToBaseUrl: string, paths: MapLike<readonly string[]>, allowedEndings: ModuleSpecifierEnding[], host: ModuleSpecifierResolutionHost, compilerOptions: CompilerOptions): string | undefined {
928+
function tryGetModuleNameFromPaths(relativeToBaseUrl: string, paths: MapLike<readonly string[]>, allowedEndings: ModuleSpecifierEnding[], baseDirectory: string, getCanonicalFileName: GetCanonicalFileName, host: ModuleSpecifierResolutionHost, compilerOptions: CompilerOptions): string | undefined {
929929
for (const key in paths) {
930930
for (const patternText of paths[key]) {
931-
const pattern = normalizePath(patternText);
931+
const normalized = normalizePath(patternText);
932+
const pattern = getRelativePathIfInSameVolume(normalized, baseDirectory, getCanonicalFileName) ?? normalized;
932933
const indexOfStar = pattern.indexOf("*");
933934
// In module resolution, if `pattern` itself has an extension, a file with that extension is looked up directly,
934935
// 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
12901291
subModuleName,
12911292
versionPaths.paths,
12921293
allowedEndings,
1294+
packageRootPath,
1295+
getCanonicalFileName,
12931296
host,
12941297
options,
12951298
);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: tsconfig.json
4+
//// {
5+
//// "compilerOptions": {
6+
//// "paths": {
7+
//// "@root/*": ["${configDir}/src/*"]
8+
//// }
9+
//// }
10+
//// }
11+
12+
// @Filename: src/one.ts
13+
//// export const one = 1;
14+
15+
// @Filename: src/foo/two.ts
16+
//// one/**/
17+
18+
verify.importFixModuleSpecifiers("", ["@root/one"]);

0 commit comments

Comments
 (0)