Skip to content

Commit 8d9c8af

Browse files
authored
Fix getEmitOutDir not using project root if no outDir is provided (#1038)
* Fix getEmitOutDir not using project root if no outDir is provided * Removed redundant tests
1 parent 1e539f0 commit 8d9c8af

File tree

5 files changed

+127
-5
lines changed

5 files changed

+127
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## 0.40.0
44

55
- Added support for using external Lua code in your project. This means you can create and install node_modules packages containing Lua code. It also lets you include Lua source files as part of your source files. Used Lua will automatically be added to your output. For more information, see the [External Lua Code](https://typescripttolua.github.io/docs/external-lua-code) page in the docs.
6-
- **[Breaking]** Removed support for deprecated annotations that have been replaced with language extensions: `/** @luaIterator */`, `/** @vararg */`, `/** @luatable */` and `/** forRange */`. If you were still using these, see [the docs](https://typescripttolua.github.io/docs/advanced/compiler-annotations#vararg) for instructions how to upgrade.
6+
- **[Breaking]** Removed support for deprecated annotations that have been replaced with language extensions: `/** @luaIterator */`, `/** @vararg */`, `/** @luatable */` and `/** forRange */`. If you were still using these, see [the docs](https://typescripttolua.github.io/docs/advanced/compiler-annotations) for instructions how to upgrade.
77
- Added support for `array.entries()`.
88
- Added support for `LuaTable.has(key)` and `LuaTable.delete(key)` to the language extensions. See [docs](https://typescripttolua.github.io/docs/advanced/language-extensions#lua-table-types) for more info.
99
- Made language extension types more strict, disallowing `null` and `undefined` in some places where they would cause problems in Lua.

src/transpilation/bundle.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { SourceNode } from "source-map";
33
import * as ts from "typescript";
44
import { CompilerOptions } from "../CompilerOptions";
55
import { escapeString, tstlHeader } from "../LuaPrinter";
6-
import { cast, formatPathToLuaPath, isNonNull, normalizeSlashes, trimExtension } from "../utils";
6+
import { cast, formatPathToLuaPath, isNonNull, trimExtension } from "../utils";
77
import { couldNotFindBundleEntryPoint } from "./diagnostics";
88
import { getEmitOutDir, getEmitPathRelativeToOutDir, getSourceDir } from "./transpiler";
99
import { EmitFile, ProcessedFile } from "./utils";
@@ -42,7 +42,7 @@ export function getBundleResult(program: ts.Program, files: ProcessedFile[]): [t
4242

4343
// Resolve project settings relative to project file.
4444
const resolvedEntryModule = path.resolve(getSourceDir(program), entryModule);
45-
const outputPath = normalizeSlashes(path.resolve(getEmitOutDir(program), bundleFile));
45+
const outputPath = path.resolve(getEmitOutDir(program), bundleFile);
4646

4747
if (program.getSourceFile(resolvedEntryModule) === undefined && program.getSourceFile(entryModule) === undefined) {
4848
diagnostics.push(couldNotFindBundleEntryPoint(entryModule));

src/transpilation/transpiler.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ export function getEmitOutDir(program: ts.Program): string {
122122
if (outDir && outDir.length > 0) {
123123
return path.isAbsolute(outDir) ? outDir : path.resolve(getProjectRoot(program), outDir);
124124
}
125-
return program.getCommonSourceDirectory();
125+
126+
// If no outDir is provided, emit in project root
127+
return getProjectRoot(program);
126128
}
127129

128130
export function getProjectRoot(program: ts.Program): string {

test/transpile/bundle.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ test("should transpile into one file", () => {
1212

1313
const { outPath, lua } = transpiledFiles[0];
1414
// Verify the name is as specified in tsconfig
15-
expect(outPath.endsWith("bundle/bundle.lua")).toBe(true);
15+
expect(outPath.endsWith(path.join("bundle", "bundle.lua"))).toBe(true);
1616
// Verify exported module by executing
1717
// Use an empty TS string because we already transpiled the TS project
1818
util.testModule("").setLuaHeader(lua!).expectToEqual({ myNumber: 3 });

test/transpile/paths.spec.ts

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import * as path from "path";
2+
import * as ts from "typescript";
3+
import { getSourceDir } from "../../src";
4+
import * as util from "../util";
5+
6+
const cwd = process.cwd();
7+
8+
// Path for project tsconfig.json to resolve for
9+
const configFilePath = path.join(cwd, "tsconfig.json");
10+
11+
describe("getSourceDir", () => {
12+
test("with rootDir", () => {
13+
const program = ts.createProgram(["main.ts", "src/otherfile.ts"], { configFilePath, rootDir: "src" });
14+
15+
// getCommonSourceDirectory does not work right so mock it
16+
jest.spyOn(program, "getCommonSourceDirectory").mockReturnValue(cwd);
17+
18+
expect(getSourceDir(program)).toBe(path.join(cwd, "src"));
19+
});
20+
21+
test("without rootDir", () => {
22+
const program = ts.createProgram(["main.ts", "src/otherfile.ts"], { configFilePath });
23+
24+
// getCommonSourceDirectory does not work right so mock it
25+
jest.spyOn(program, "getCommonSourceDirectory").mockReturnValue(cwd);
26+
27+
// Common sources directory is project root
28+
expect(normalize(getSourceDir(program))).toBe(cwd);
29+
});
30+
31+
test("without rootDir in src dir", () => {
32+
const program = ts.createProgram([path.join(cwd, "src", "main.ts"), path.join(cwd, "src", "otherfile.ts")], {
33+
configFilePath,
34+
});
35+
36+
// getCommonSourceDirectory does not work right so mock it
37+
jest.spyOn(program, "getCommonSourceDirectory").mockReturnValue(path.join(cwd, "src"));
38+
39+
// Common sources directory is src
40+
expect(normalize(getSourceDir(program))).toBe(path.join(cwd, "src"));
41+
});
42+
});
43+
44+
describe("getEmitPath", () => {
45+
test("puts files next to input without options", () => {
46+
const { transpiledFiles } = util.testModule``
47+
.setMainFileName("main.ts")
48+
.addExtraFile("dir/extra.ts", "")
49+
.expectToHaveNoDiagnostics()
50+
.getLuaResult();
51+
52+
const fileNames = transpiledFiles.map(f => f.outPath);
53+
expect(fileNames).toContain("main.lua");
54+
expect(fileNames).toContain(path.join("dir", "extra.lua"));
55+
});
56+
57+
test("puts files in outdir", () => {
58+
const outDir = path.join(cwd, "tstl-out");
59+
const { transpiledFiles } = util.testModule``
60+
.setMainFileName("main.ts")
61+
.addExtraFile("dir/extra.ts", "")
62+
.setOptions({ outDir })
63+
.expectToHaveNoDiagnostics()
64+
.getLuaResult();
65+
66+
const fileNames = transpiledFiles.map(f => f.outPath);
67+
expect(fileNames).toContain(path.join(outDir, "main.lua"));
68+
expect(fileNames).toContain(path.join(outDir, "dir", "extra.lua"));
69+
});
70+
71+
test("puts files from rootDir in outdir", () => {
72+
const outDir = path.join(cwd, "tstl-out");
73+
const { transpiledFiles } = util.testModule``
74+
.setMainFileName("src/main.ts")
75+
.addExtraFile("src/extra.ts", "")
76+
.setOptions({ rootDir: "src", outDir })
77+
.expectToHaveNoDiagnostics()
78+
.getLuaResult();
79+
80+
const fileNames = transpiledFiles.map(f => f.outPath);
81+
expect(fileNames).toContain(path.join(outDir, "main.lua"));
82+
expect(fileNames).toContain(path.join(outDir, "extra.lua"));
83+
});
84+
85+
test("puts bundle relative to project root", () => {
86+
const { transpiledFiles } = util.testModule``
87+
.setMainFileName("src/main.ts")
88+
.addExtraFile("src/extra.ts", "")
89+
.setOptions({ configFilePath, rootDir: "src", luaBundle: "out/bundle.lua", luaBundleEntry: "src/main.ts" })
90+
.expectToHaveNoDiagnostics()
91+
.getLuaResult();
92+
93+
const fileNames = transpiledFiles.map(f => f.outPath);
94+
expect(fileNames).toHaveLength(1);
95+
expect(fileNames).toContain(path.join(cwd, "out", "bundle.lua"));
96+
});
97+
98+
test("puts bundle relative to outdir", () => {
99+
const { transpiledFiles } = util.testModule``
100+
.setMainFileName("src/main.ts")
101+
.addExtraFile("src/extra.ts", "")
102+
.setOptions({
103+
configFilePath,
104+
rootDir: "src",
105+
outDir: "out1",
106+
luaBundle: "out2/bundle.lua",
107+
luaBundleEntry: "src/main.ts",
108+
})
109+
.expectToHaveNoDiagnostics()
110+
.getLuaResult();
111+
112+
const fileNames = transpiledFiles.map(f => f.outPath);
113+
expect(fileNames).toHaveLength(1);
114+
expect(fileNames).toContain(path.join(cwd, "out1", "out2", "bundle.lua"));
115+
});
116+
});
117+
118+
function normalize(path: string) {
119+
return path.endsWith("/") ? path.slice(0, path.length - 1) : path;
120+
}

0 commit comments

Comments
 (0)