Skip to content

Commit 818c980

Browse files
authored
Ensure paths-based resolution does not generate module specifiers with .. in the middle (microsoft#53957)
1 parent 36b6325 commit 818c980

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

src/compiler/moduleSpecifiers.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ function getLocalModuleSpecifier(moduleFileName: string, info: Info, compilerOpt
459459
}
460460

461461
const baseDirectory = getNormalizedAbsolutePath(getPathsBasePath(compilerOptions, host) || baseUrl!, host.getCurrentDirectory());
462-
const relativeToBaseUrl = getRelativePathIfInDirectory(moduleFileName, baseDirectory, getCanonicalFileName);
462+
const relativeToBaseUrl = getRelativePathIfInSameVolume(moduleFileName, baseDirectory, getCanonicalFileName);
463463
if (!relativeToBaseUrl) {
464464
return pathsOnly ? undefined : relativePath;
465465
}
@@ -773,7 +773,7 @@ function tryGetModuleNameFromPaths(relativeToBaseUrl: string, paths: MapLike<rea
773773
validateEnding({ ending, value })
774774
) {
775775
const matchedStar = value.substring(prefix.length, value.length - suffix.length);
776-
return key.replace("*", matchedStar);
776+
return pathIsRelative(matchedStar) ? undefined : key.replace("*", matchedStar);
777777
}
778778
}
779779
}
@@ -1038,7 +1038,7 @@ function tryGetAnyFileFromPath(host: ModuleSpecifierResolutionHost, path: string
10381038

10391039
function getPathsRelativeToRootDirs(path: string, rootDirs: readonly string[], getCanonicalFileName: GetCanonicalFileName): string[] | undefined {
10401040
return mapDefined(rootDirs, rootDir => {
1041-
const relativePath = getRelativePathIfInDirectory(path, rootDir, getCanonicalFileName);
1041+
const relativePath = getRelativePathIfInSameVolume(path, rootDir, getCanonicalFileName);
10421042
return relativePath !== undefined && isPathRelativeToParent(relativePath) ? undefined : relativePath;
10431043
});
10441044
}
@@ -1133,7 +1133,7 @@ export function tryGetJSExtensionForFile(fileName: string, options: CompilerOpti
11331133
}
11341134
}
11351135

1136-
function getRelativePathIfInDirectory(path: string, directoryPath: string, getCanonicalFileName: GetCanonicalFileName): string | undefined {
1136+
function getRelativePathIfInSameVolume(path: string, directoryPath: string, getCanonicalFileName: GetCanonicalFileName): string | undefined {
11371137
const relativePath = getRelativePathToDirectoryOrUrl(directoryPath, path, directoryPath, getCanonicalFileName, /*isAbsolutePathAnUrl*/ false);
11381138
return isRootedDiskPath(relativePath) ? undefined : relativePath;
11391139
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/// <reference path="../fourslash.ts" />
2+
3+
// @Filename: /project/packages/common/package.json
4+
//// {
5+
//// "name": "@company/common",
6+
//// "version": "1.0.0",
7+
//// "main": "./lib/index.tsx"
8+
//// }
9+
10+
// @Filename: /project/packages/common/lib/index.tsx
11+
//// export function Tooltip {};
12+
13+
// @Filename: /project/packages/app/package.json
14+
//// {
15+
//// "name": "@company/app",
16+
//// "version": "1.0.0",
17+
//// "dependencies": {
18+
//// "@company/common": "1.0.0"
19+
//// }
20+
//// }
21+
22+
// @Filename: /project/packages/app/tsconfig.json
23+
//// {
24+
//// "compilerOptions": {
25+
//// "composite": true,
26+
//// "module": "esnext",
27+
//// "moduleResolution": "bundler",
28+
//// "paths": {
29+
//// "@/*": ["./*"]
30+
//// }
31+
//// }
32+
//// }
33+
34+
// @Filename: /project/packages/app/lib/index.ts
35+
//// Tooltip/**/
36+
37+
// @link: /project/packages/common -> /project/node_modules/@company/common
38+
39+
goTo.marker("");
40+
verify.importFixModuleSpecifiers("", ["@company/common"]);

0 commit comments

Comments
 (0)