Skip to content

Commit d470199

Browse files
authored
Check baseUrl when resolving imported files (#1067)
1 parent 228d772 commit d470199

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

src/transpilation/resolve.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ function resolveDependency(
167167
}
168168

169169
// Check if file is a file in the project
170-
const resolvedPath = path.join(fileDirectory, dependency);
170+
const resolvedPath = path.join(options.baseUrl ?? fileDirectory, dependency);
171171

172172
const possibleProjectFiles = [
173173
resolvedPath, // JSON files need their extension as part of the import path, caught by this branch,

test/transpile/module-resolution.spec.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,43 @@ test("module resolution should not rewrite @NoResolution requires in library mod
350350
expect(transpiledFiles).toHaveLength(1);
351351
expect(transpiledFiles[0].lua).toContain('require("@NoResolution:');
352352
});
353+
354+
test("module resolution uses baseURL to resolve imported files", () => {
355+
util.testModule`
356+
import { foo } from "dep1";
357+
import { bar } from "dep2";
358+
import { baz } from "luadep";
359+
360+
export const fooResult = foo();
361+
export const barResult = bar();
362+
`
363+
.addExtraFile(
364+
"myproject/mydeps/dep1.ts",
365+
`
366+
export function foo() { return "foo"; }
367+
`
368+
)
369+
.addExtraFile(
370+
"myproject/mydeps/dep2.ts",
371+
`
372+
export function bar() { return "bar"; }
373+
`
374+
)
375+
.addExtraFile(
376+
"myproject/mydeps/luadep.d.ts",
377+
`
378+
export function baz(): string;
379+
`
380+
)
381+
.addExtraFile(
382+
"myproject/mydeps/luadep.lua",
383+
`
384+
return { baz = function() return "baz" end }
385+
`
386+
)
387+
.setOptions({ baseUrl: "./myproject/mydeps" })
388+
.expectToEqual({
389+
fooResult: "foo",
390+
barResult: "bar",
391+
});
392+
});

test/util.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,14 @@ export abstract class TestBuilder {
178178
@memoize
179179
public getProgram(): ts.Program {
180180
this.hasProgram = true;
181+
182+
// Exclude lua files from TS program, but keep them in extraFiles so module resolution can find them
183+
const nonLuaExtraFiles = Object.fromEntries(
184+
Object.entries(this.extraFiles).filter(([fileName]) => !fileName.endsWith(".lua"))
185+
);
186+
181187
return tstl.createVirtualProgram(
182-
{ ...this.extraFiles, [normalizeSlashes(this.mainFileName)]: this.getTsCode() },
188+
{ ...nonLuaExtraFiles, [normalizeSlashes(this.mainFileName)]: this.getTsCode() },
183189
this.options
184190
);
185191
}

0 commit comments

Comments
 (0)