Skip to content

Commit 07f4763

Browse files
committed
fix: file key normalization
1 parent 1be16ec commit 07f4763

3 files changed

Lines changed: 31 additions & 22 deletions

File tree

src/compiler/AppsEngineValidator.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ export class AppsEngineValidator {
114114
filename: string,
115115
compilationResult: ICompilerResult,
116116
): any {
117+
filename = filename.replace(/\\/g, "/");
117118
const exports = {};
118119
const context = vm.createContext({
119120
require: (filepath: string) => {

src/compiler/TscBasedCompiler.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ export class TscBasedCompiler {
3939
}: IAppSource): Promise<ICompilerResult> {
4040
const startTime = Date.now();
4141

42+
const posixClassFile = appInfo.classFile.replace(/\\/g, "/");
43+
4244
// Entry‐file must exist
43-
if (!appInfo.classFile || !sourceFiles[appInfo.classFile]) {
45+
if (!posixClassFile || !sourceFiles[posixClassFile]) {
4446
throw new Error(
4547
`Invalid App package. Could not find the classFile (${appInfo.classFile}).`,
4648
);
@@ -228,21 +230,21 @@ export class TscBasedCompiler {
228230
await collect(path.join(this.sourcePath, BUILD_DIR));
229231

230232
// Point at the main JS file
231-
const mainJs = appInfo.classFile.replace(/\.ts$/, ".js");
233+
const mainJs = posixClassFile.replace(/\.ts$/, ".js");
232234
result.mainFile = result.files[mainJs];
233235

234236
// Extract implemented interfaces from the TS AST
235237
const srcNode = TS.createSourceFile(
236-
appInfo.classFile,
237-
sourceFiles[appInfo.classFile].content,
238+
posixClassFile,
239+
sourceFiles[posixClassFile].content,
238240
TS.ScriptTarget.Latest,
239241
true,
240242
);
241243
result.implemented = this.extractInterfaces(srcNode);
242244

243245
// Run inheritance checks
244246
this.appValidator.checkInheritance(
245-
appInfo.classFile.replace(/\.ts$/, ""),
247+
posixClassFile.replace(/\.ts$/, ""),
246248
result,
247249
);
248250

src/compiler/TypescriptCompiler.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { EventEmitter } from "events";
22
import fs from "fs";
33
import path from "path";
44

5-
import type { IAppInfo } from "@rocket.chat/apps-engine/definition/metadata";
65
import type {
76
CompilerOptions,
87
EmitOutput,
@@ -59,10 +58,24 @@ export class TypescriptCompiler {
5958
appInfo,
6059
sourceFiles: files,
6160
}: IAppSource): ICompilerResult {
61+
// Normalize all file keys, file names, and classFile to POSIX separators
62+
// once up front so getScriptFileNames(), getScriptVersion(), getScriptSnapshot(),
63+
// and all subsequent lookups into result.files operate on consistent keys.
64+
const posixClassFile = appInfo.classFile.replace(/\\/g, "/");
65+
const normalizedFiles: IMapCompilerFile = {};
66+
for (const [key, file] of Object.entries(files)) {
67+
const posixKey = key.replace(/\\/g, "/");
68+
normalizedFiles[posixKey] = {
69+
...file,
70+
name: file.name.replace(/\\/g, "/"),
71+
};
72+
}
73+
files = normalizedFiles;
74+
6275
if (
63-
!appInfo.classFile ||
64-
!files[appInfo.classFile] ||
65-
!this.isValidFile(files[appInfo.classFile])
76+
!posixClassFile ||
77+
!files[posixClassFile] ||
78+
!this.isValidFile(files[posixClassFile])
6679
) {
6780
throw new Error(
6881
`Invalid App package. Could not find the classFile (${appInfo.classFile}) file.`,
@@ -219,7 +232,7 @@ export class TypescriptCompiler {
219232

220233
result.implemented = this.getImplementedInterfaces(
221234
languageService,
222-
appInfo,
235+
posixClassFile,
223236
);
224237

225238
Object.defineProperty(result, "diagnostics", {
@@ -242,11 +255,10 @@ export class TypescriptCompiler {
242255
file.compiled = output.outputFiles[0].text;
243256
});
244257

245-
result.mainFile =
246-
result.files[appInfo.classFile.replace(/\.ts$/, ".js")];
258+
result.mainFile = result.files[posixClassFile.replace(/\.ts$/, ".js")];
247259

248260
this.appValidator.checkInheritance(
249-
appInfo.classFile.replace(/\.ts$/, ""),
261+
posixClassFile.replace(/\.ts$/, ""),
250262
result,
251263
);
252264

@@ -344,13 +356,11 @@ export class TypescriptCompiler {
344356

345357
private getImplementedInterfaces(
346358
languageService: LanguageService,
347-
appInfo: IAppInfo,
359+
classFile: string,
348360
): ICompilerResult["implemented"] {
349361
const result: ICompilerResult["implemented"] = [];
350362

351-
const src = languageService
352-
.getProgram()
353-
.getSourceFile(appInfo.classFile);
363+
const src = languageService.getProgram().getSourceFile(classFile);
354364

355365
this.ts.forEachChild(src, (n) => {
356366
if (!this.ts.isClassDeclaration(n)) {
@@ -406,10 +416,6 @@ export class TypescriptCompiler {
406416
return false;
407417
}
408418

409-
return (
410-
file.name.trim() !== "" &&
411-
path.posix.normalize(file.name) &&
412-
file.content.trim() !== ""
413-
);
419+
return file.name.trim() !== "" && file.content.trim() !== "";
414420
}
415421
}

0 commit comments

Comments
 (0)