Skip to content

Commit d757402

Browse files
authored
Resolve module specifier relative to moduleFile.originalFileName (#32722)
* Resolve module specifier relative to moduleFile.originalFileName * Use baseline testing for outputs * Added skipLibCheck to test * Switch to using vfs.formatPatch for output
1 parent 024193f commit d757402

File tree

6 files changed

+472
-4
lines changed

6 files changed

+472
-4
lines changed

src/compiler/moduleSpecifiers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ namespace ts.moduleSpecifiers {
9393

9494
const info = getInfo(importingSourceFile.path, host);
9595
const moduleSourceFile = getSourceFileOfNode(moduleSymbol.valueDeclaration || getNonAugmentationDeclaration(moduleSymbol));
96-
const modulePaths = getAllModulePaths(files, importingSourceFile.path, moduleSourceFile.fileName, info.getCanonicalFileName, host, redirectTargetsMap);
96+
const modulePaths = getAllModulePaths(files, importingSourceFile.path, moduleSourceFile.originalFileName, info.getCanonicalFileName, host, redirectTargetsMap);
9797

9898
const preferences = getPreferences(userPreferences, compilerOptions, importingSourceFile);
9999
const global = mapDefined(modulePaths, moduleFileName => tryGetModuleNameAsNodeModule(moduleFileName, info, host, compilerOptions));

src/harness/vfs.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,8 +1525,11 @@ namespace vfs {
15251525
return typeof value === "string" || Buffer.isBuffer(value) ? new File(value) : new Directory(value);
15261526
}
15271527

1528-
export function formatPatch(patch: FileSet) {
1529-
return formatPatchWorker("", patch);
1528+
export function formatPatch(patch: FileSet): string;
1529+
export function formatPatch(patch: FileSet | undefined): string | null;
1530+
export function formatPatch(patch: FileSet | undefined) {
1531+
// tslint:disable-next-line:no-null-keyword
1532+
return patch ? formatPatchWorker("", patch) : null;
15301533
}
15311534

15321535
function formatPatchWorker(dirname: string, container: FileSet): string {
@@ -1559,5 +1562,24 @@ namespace vfs {
15591562
}
15601563
return text;
15611564
}
1565+
1566+
export function iteratePatch(patch: FileSet | undefined): IterableIterator<[string, string]> | null {
1567+
// tslint:disable-next-line:no-null-keyword
1568+
return patch ? Harness.Compiler.iterateOutputs(iteratePatchWorker("", patch)) : null;
1569+
}
1570+
1571+
function* iteratePatchWorker(dirname: string, container: FileSet): IterableIterator<documents.TextDocument> {
1572+
for (const name of Object.keys(container)) {
1573+
const entry = normalizeFileSetEntry(container[name]);
1574+
const file = dirname ? vpath.combine(dirname, name) : name;
1575+
if (entry instanceof Directory) {
1576+
yield* ts.arrayFrom(iteratePatchWorker(file, entry.files));
1577+
}
1578+
else if (entry instanceof File) {
1579+
const content = typeof entry.data === "string" ? entry.data : entry.data.toString("utf8");
1580+
yield new documents.TextDocument(file, content);
1581+
}
1582+
}
1583+
}
15621584
}
15631585
// tslint:enable:no-null-keyword

src/testRunner/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
"unittests/tsbuild/inferredTypeFromTransitiveModule.ts",
9999
"unittests/tsbuild/lateBoundSymbol.ts",
100100
"unittests/tsbuild/missingExtendedFile.ts",
101+
"unittests/tsbuild/moduleSpecifiers.ts",
101102
"unittests/tsbuild/outFile.ts",
102103
"unittests/tsbuild/referencesWithRootDirInParent.ts",
103104
"unittests/tsbuild/resolveJsonModule.ts",

src/testRunner/unittests/config/projectReferences.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,5 +348,4 @@ namespace ts {
348348
});
349349
});
350350
});
351-
352351
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
namespace ts {
2+
// https://github.com/microsoft/TypeScript/issues/31696
3+
it("unittests:: tsbuild:: moduleSpecifiers:: synthesized module specifiers to referenced projects resolve correctly", () => {
4+
const baseFs = vfs.createFromFileSystem(Harness.IO, /*ignoreCase*/ false, {
5+
files: {
6+
"/src/common/nominal.ts": utils.dedent`
7+
export declare type Nominal<T, Name extends string> = T & {
8+
[Symbol.species]: Name;
9+
};
10+
`,
11+
"/src/common/tsconfig.json": utils.dedent`
12+
{
13+
"extends": "../../tsconfig.base.json",
14+
"compilerOptions": {
15+
"composite": true
16+
},
17+
"include": ["nominal.ts"]
18+
}`,
19+
"/src/sub-project/index.ts": utils.dedent`
20+
import { Nominal } from '../common/nominal';
21+
22+
export type MyNominal = Nominal<string, 'MyNominal'>;
23+
`,
24+
"/src/sub-project/tsconfig.json": utils.dedent`
25+
{
26+
"extends": "../../tsconfig.base.json",
27+
"compilerOptions": {
28+
"composite": true
29+
},
30+
"references": [
31+
{ "path": "../common" }
32+
],
33+
"include": ["./index.ts"]
34+
}`,
35+
"/src/sub-project-2/index.ts": utils.dedent`
36+
import { MyNominal } from '../sub-project/index';
37+
38+
const variable = {
39+
key: 'value' as MyNominal,
40+
};
41+
42+
export function getVar(): keyof typeof variable {
43+
return 'key';
44+
}
45+
`,
46+
"/src/sub-project-2/tsconfig.json": utils.dedent`
47+
{
48+
"extends": "../../tsconfig.base.json",
49+
"compilerOptions": {
50+
"composite": true
51+
},
52+
"references": [
53+
{ "path": "../sub-project" }
54+
],
55+
"include": ["./index.ts"]
56+
}`,
57+
"/src/tsconfig.json": utils.dedent`
58+
{
59+
"compilerOptions": {
60+
"composite": true
61+
},
62+
"references": [
63+
{ "path": "./sub-project" },
64+
{ "path": "./sub-project-2" }
65+
],
66+
"include": []
67+
}`,
68+
"/tsconfig.base.json": utils.dedent`
69+
{
70+
"compilerOptions": {
71+
"skipLibCheck": true,
72+
"rootDir": "./",
73+
"outDir": "lib",
74+
"lib": ["dom", "es2015", "es2015.symbol.wellknown"]
75+
}
76+
}`,
77+
"/tsconfig.json": utils.dedent`{
78+
"compilerOptions": {
79+
"composite": true
80+
},
81+
"references": [
82+
{ "path": "./src" }
83+
],
84+
"include": []
85+
}`
86+
},
87+
cwd: "/"
88+
});
89+
const fs = baseFs.makeReadonly().shadow();
90+
const sys = new fakes.System(fs, { executingFilePath: "/", newLine: "\n" });
91+
const host = new fakes.SolutionBuilderHost(sys);
92+
const builder = createSolutionBuilder(host, ["/tsconfig.json"], { dry: false, force: false, verbose: false });
93+
builder.build();
94+
95+
// Prior to fixing GH31696 the import in `/lib/src/sub-project-2/index.d.ts` was `import("../../lib/src/common/nonterminal")`, which was invalid.
96+
Harness.Baseline.runBaseline("tsbuild/moduleSpecifiers/initial-build/resolves-correctly.js", vfs.formatPatch(fs.diff(baseFs)));
97+
});
98+
}

0 commit comments

Comments
 (0)