Skip to content

Commit 0695475

Browse files
committed
Make absolute/relative paths handling more consistent
1 parent fa25e24 commit 0695475

5 files changed

Lines changed: 36 additions & 14 deletions

File tree

src/transpilation/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,23 @@ export function createVirtualProgram(input: Record<string, string>, options: Com
4747
throw new Error("Not implemented");
4848
}
4949

50+
const getFileFromInput = (fileName: string) =>
51+
input[fileName] ?? (fileName.startsWith("/") ? input[fileName.slice(1)] : undefined);
52+
5053
const compilerHost: ts.CompilerHost = {
5154
useCaseSensitiveFileNames: () => false,
5255
getCanonicalFileName: fileName => fileName,
53-
getCurrentDirectory: () => "",
54-
fileExists: fileName => fileName.startsWith("lib.") || fileName in input,
56+
getCurrentDirectory: () => "/",
57+
fileExists: fileName => fileName.startsWith("lib.") || getFileFromInput(fileName) !== undefined,
5558
readFile: notImplemented,
5659
writeFile: notImplemented,
5760
getDefaultLibFileName: ts.getDefaultLibFileName,
5861
getNewLine: () => "\n",
5962

6063
getSourceFile(fileName) {
61-
if (fileName in input) {
62-
return ts.createSourceFile(fileName, input[fileName], ts.ScriptTarget.Latest, false);
64+
const fileFromInput = getFileFromInput(fileName);
65+
if (fileFromInput !== undefined) {
66+
return ts.createSourceFile(fileName, fileFromInput, ts.ScriptTarget.Latest, false);
6367
}
6468

6569
if (fileName.startsWith("lib.")) {

src/transpilation/transpile/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as ts from "typescript";
33
import { CompilerOptions, validateOptions } from "../../CompilerOptions";
44
import { createPrinter } from "../../LuaPrinter";
55
import { createVisitorMap, transformSourceFile } from "../../transformation";
6-
import { isNonNull } from "../../utils";
6+
import { assert, isNonNull } from "../../utils";
77
import { getPlugins, Plugin } from "./plugins";
88
import { getTransformers } from "./transformers";
99
import { EmitHost, ProcessedFile } from "../utils";
@@ -71,8 +71,17 @@ export function getProgramTranspileResult(
7171
diagnostics.push(...transformDiagnostics);
7272
if (!options.noEmit && !options.emitDeclarationOnly) {
7373
const printResult = printer(program, emitHost, sourceFile.fileName, luaAst, luaLibFeatures);
74-
const sourceRootDir = program.getCommonSourceDirectory();
75-
const fileName = path.join(sourceRootDir, sourceFile.fileName);
74+
75+
let fileName: string;
76+
if (path.isAbsolute(sourceFile.fileName)) {
77+
fileName = sourceFile.fileName;
78+
} else {
79+
const currentDirectory = emitHost.getCurrentDirectory();
80+
// Having no absolute path in path.resolve would make it fallback to real cwd
81+
assert(path.isAbsolute(currentDirectory), `Invalid path: ${currentDirectory}`);
82+
fileName = path.resolve(currentDirectory, sourceFile.fileName);
83+
}
84+
7685
transpiledFiles.push({ sourceFiles: [sourceFile], fileName, luaAst, ...printResult });
7786
}
7887
};

src/transpilation/transpiler.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,20 @@ class Transpilation {
6060
private seenFiles = new Set<string>();
6161
private files: ProcessedFile[] = [];
6262

63-
constructor(private emitHost: EmitHost, private program: ts.Program) {}
64-
6563
private options = this.program.getCompilerOptions() as CompilerOptions;
66-
private rootDir = this.program.getCommonSourceDirectory();
67-
private outDir = this.options.outDir ?? this.rootDir;
64+
private rootDir: string;
65+
private outDir: string;
66+
67+
constructor(private emitHost: EmitHost, private program: ts.Program) {
68+
const { rootDir } = program.getCompilerOptions();
69+
this.rootDir =
70+
// getCommonSourceDirectory ignores provided rootDir when TS6059 is emitted
71+
rootDir == null
72+
? program.getCommonSourceDirectory()
73+
: ts.getNormalizedAbsolutePath(rootDir, emitHost.getCurrentDirectory());
74+
75+
this.outDir = this.options.outDir ?? this.rootDir;
76+
}
6877

6978
public emit(transpiledFiles: ProcessedFile[]): EmitFile[] {
7079
transpiledFiles.forEach(file => this.seenFiles.add(file.fileName));
@@ -93,11 +102,9 @@ class Transpilation {
93102

94103
private handleProcessedFile(file: ProcessedFile) {
95104
const replacer: ResolveMacroReplacer = (request: string) => {
96-
let basedir = path.dirname(file.fileName);
97-
if (basedir === ".") basedir = this.emitHost.getCurrentDirectory();
98105
let resolvedPath: string;
99106
try {
100-
const result = this.resolver.resolveSync({}, basedir, request);
107+
const result = this.resolver.resolveSync({}, path.dirname(file.fileName), request);
101108
assert(typeof result === "string");
102109
resolvedPath = result;
103110
} catch (error) {

src/transpilation/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ interface BaseFile {
1919
}
2020

2121
export interface ProcessedFile extends BaseFile {
22+
/** Absolute source file path. */
2223
fileName: string;
2324
luaAst?: lua.Block;
2425
/** @internal */

src/typescript-internal.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export {};
33
declare module "typescript" {
44
function createDiagnosticReporter(system: System, pretty?: boolean): DiagnosticReporter;
55
function createWatchStatusReporter(system: System, pretty?: boolean): WatchStatusReporter;
6+
function getNormalizedAbsolutePath(fileName: string, currentDirectory: string): string;
67

78
interface System {
89
setBlocking?(): void;

0 commit comments

Comments
 (0)