Skip to content

Commit d1390a6

Browse files
committed
fix: DenoRuntimeSubprocessController runtime config and path resolutions
1 parent 9eb52d8 commit d1390a6

2 files changed

Lines changed: 18 additions & 17 deletions

File tree

packages/apps/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
.deno-cache/
22
/tests/test-data/dbs
3+
4+
# File generated at runtime
5+
deno.runtime.jsonc

packages/apps/src/server/runtime/deno/AppsEngineDenoRuntime.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as child_process from 'child_process';
22
import * as fs from 'fs';
3-
import os from 'os';
43
import * as path from 'path';
54
import { type Readable, EventEmitter } from 'stream';
65
import { inspect as utilInspect } from 'util';
@@ -82,19 +81,13 @@ function getDenoConfigPath(): string {
8281
return require.resolve('../../../../deno-runtime/deno.jsonc');
8382
}
8483

85-
function getDenoConfig(): DenoConfigurationFileSchema {
86-
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-unsafe-return, import/no-dynamic-require
87-
return require(getDenoConfigPath());
88-
}
89-
9084
/**
9185
* Resolves the absolute path to @rocket.chat/apps-engine's src/ directory.
9286
* Uses require.resolve so it works regardless of the runtime environment
9387
* (monorepo dev, Meteor bundle, standalone node_modules).
9488
*/
9589
function getAppsEngineDir(): string {
96-
const pkgJsonPath = require.resolve('@rocket.chat/apps-engine/package.json');
97-
return path.dirname(pkgJsonPath);
90+
return path.dirname(require.resolve('@rocket.chat/apps-engine/package.json'));
9891
}
9992

10093
/**
@@ -105,16 +98,20 @@ function getAppsEngineDir(): string {
10598
*
10699
* Returns the path to the generated config file.
107100
*/
108-
function generateEphemeralDenoConfig(targetPath: string, appsEnginePath: string, staticConfig: DenoConfigurationFileSchema): void {
109-
if (!targetPath.startsWith(os.tmpdir())) {
110-
throw new Error(`Temp directory "${targetPath}" is not inside the system temp directory`);
101+
function generateEphemeralDenoConfig(targetPath: string, denoConfigPath: string, appsEnginePath: string): void {
102+
let staticConfig: DenoConfigurationFileSchema;
103+
104+
try {
105+
staticConfig = JSON.parse(fs.readFileSync(denoConfigPath, 'utf8'));
106+
} catch {
107+
throw new Error(`Failed to read the static Deno config at "${denoConfigPath}"`);
111108
}
112109

113110
const runtimeConfig = {
114111
...staticConfig,
115112
imports: {
116113
...staticConfig.imports,
117-
'@rocket.chat/apps-engine/': appsEnginePath,
114+
'@rocket.chat/apps-engine/': `${appsEnginePath}/`,
118115
},
119116
};
120117

@@ -178,11 +175,12 @@ export class DenoRuntimeSubprocessController extends EventEmitter implements IRu
178175
this.tempFilePath = manager.getTempFilePath();
179176
this.denoConfigPath = getDenoConfigPath();
180177
this.appsEnginePath = getAppsEngineDir();
181-
this.denoEphemeralConfigPath = path.join(this.tempFilePath, 'deno_ephemeral.jsonc');
178+
179+
this.packagePath = path.join(path.dirname(this.denoConfigPath), '..');
180+
this.denoDir = process.env.DENO_DIR ?? path.join(this.packagePath, '.deno-cache');
182181

183182
this.denoRuntimePath = path.join(this.tempFilePath, 'deno-runtime', 'main.ts');
184-
this.denoDir = process.env.DENO_DIR ?? path.join(this.denoConfigPath, '..', '.deno-cache');
185-
this.packagePath = path.dirname(this.denoConfigPath);
183+
this.denoEphemeralConfigPath = this.denoConfigPath.replace('.jsonc', '.runtime.jsonc');
186184

187185
/**
188186
* Deno 2.x refuses to run scripts inside the node_modules, so we create a symlink to the deno runtime files in the temp directory
@@ -197,7 +195,7 @@ export class DenoRuntimeSubprocessController extends EventEmitter implements IRu
197195
}
198196

199197
// Generate a runtime config with the resolved absolute path for @rocket.chat/apps-engine/
200-
generateEphemeralDenoConfig(this.denoEphemeralConfigPath, this.appsEnginePath, getDenoConfig());
198+
generateEphemeralDenoConfig(this.denoEphemeralConfigPath, this.denoConfigPath, this.appsEnginePath);
201199

202200
this.debug = baseDebug.extend(appPackage.info.id);
203201
this.messenger = new ProcessMessenger();
@@ -217,7 +215,7 @@ export class DenoRuntimeSubprocessController extends EventEmitter implements IRu
217215

218216
public spawnProcess(): void {
219217
try {
220-
const allowedDirs = [this.appsEnginePath, this.packagePath, this.tempFilePath];
218+
const allowedDirs = [this.tempFilePath, this.denoDir, this.packagePath, this.appsEnginePath];
221219

222220
const options = [
223221
'run',

0 commit comments

Comments
 (0)