Skip to content

Commit 979bebe

Browse files
authored
Fix/paths vs project file (#1715)
* add failing test: paths mapping loses to sibling project file * add temporary TS <6.0 support * resolve paths mappings before sibling project files to match ts order * don't need ignoreDeprecations * remove redundant end-of-todo-block comment * drop baseUrl from paths-vs-project-file test fixture
1 parent ef946a3 commit 979bebe

6 files changed

Lines changed: 34 additions & 7 deletions

File tree

src/transpilation/resolve.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,9 @@ class ResolutionContext {
208208
if (resolvedNodeModulesFile) return resolvedNodeModulesFile;
209209
}
210210

211-
// Check if file is a file in the project
212-
const resolvedPath = this.formatPathToFile(dependencyPath, requiringFile);
213-
const fileFromPath = this.getFileFromPath(resolvedPath);
214-
if (fileFromPath) return fileFromPath;
215-
216-
if (this.options.paths) {
217-
// If no file found yet and paths are present, try to find project file via paths mappings
211+
// Bare specifiers: check paths mappings first, matching TypeScript's resolution order.
212+
// TS never applies paths to relative imports, so skip for those.
213+
if (!ts.isExternalModuleNameRelative(dependencyPath) && this.options.paths) {
218214
// When baseUrl is not set, resolve paths relative to the tsconfig directory (TS 6.0+ behavior)
219215
const pathsBase =
220216
this.options.baseUrl ??
@@ -225,6 +221,11 @@ class ResolutionContext {
225221
}
226222
}
227223

224+
// Check if file is a file in the project
225+
const resolvedPath = this.formatPathToFile(dependencyPath, requiringFile);
226+
const fileFromPath = this.getFileFromPath(resolvedPath);
227+
if (fileFromPath) return fileFromPath;
228+
228229
// Not a TS file in our project sources, use resolver to check if we can find dependency
229230
try {
230231
const resolveResult = resolver.resolveSync({}, fileDirectory, dependencyPath);

test/transpile/module-resolution.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,18 @@ test("supports complicated paths configuration", () => {
707707
.expectToEqual({ foo: 314, bar: 271 });
708708
});
709709

710+
test("paths mapping wins over sibling project file (TS resolution order)", () => {
711+
const baseProjectPath = path.resolve(__dirname, "module-resolution", "paths-vs-project-file");
712+
const projectPath = path.join(baseProjectPath, "program");
713+
const projectTsConfig = path.join(projectPath, "tsconfig.json");
714+
const mainFile = path.join(projectPath, "main.ts");
715+
716+
util.testProject(projectTsConfig)
717+
.setMainFileName(mainFile)
718+
.setOptions({ luaBundle: "bundle.lua", luaBundleEntry: mainFile })
719+
.expectToEqual({ value: "paths-mapped" });
720+
});
721+
710722
test("module resolution using plugin", () => {
711723
const baseProjectPath = path.resolve(__dirname, "module-resolution", "project-with-module-resolution-plugin");
712724
const projectTsConfig = path.join(baseProjectPath, "tsconfig.json");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const value = "paths-mapped";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const value = "project-file";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { value } from "alias";
2+
3+
export { value };
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"rootDir": "..",
4+
"outDir": "dist",
5+
"paths": {
6+
"alias": ["../other/alias"]
7+
}
8+
}
9+
}

0 commit comments

Comments
 (0)