Skip to content

Commit b61f8c1

Browse files
committed
Better solution for InstanceSettings user home dir
1 parent c8bf11b commit b61f8c1

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

packages/cli/src/benchmark/benchmark.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ To create a benchmark suite:
2525

2626
- [using "Respond immediately" mode](./suites/workflows/001-1.json)
2727
- [using "When last node finishes" mode](./suites/workflows/001-2.json)
28-
- [using "Respond to Webhook" node mode](./suites/workflows/001-3.json)
28+
- [using "Respond to Webhook node" mode](./suites/workflows/001-3.json)
2929

3030
<!-- /BENCHMARK_SUITES_LIST -->

packages/cli/src/benchmark/lib/hooks/n8nDir.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,32 @@ import path from 'node:path';
33
import { mkdirSync, mkdtempSync, writeFileSync } from 'node:fs';
44
import Container from 'typedi';
55
import { InstanceSettings } from 'n8n-core';
6-
import { Logger } from '@/Logger';
76

7+
/**
8+
* Create a temp .n8n user dir for benchmarking.
9+
*/
810
export function n8nDir() {
9-
const baseDirPath = path.join(tmpdir(), 'n8n-benchmarks/');
11+
const tempBaseDir = path.join(tmpdir(), 'n8n-benchmarks/');
1012

11-
mkdirSync(baseDirPath, { recursive: true });
13+
mkdirSync(tempBaseDir, { recursive: true });
1214

13-
const userDir = mkdtempSync(baseDirPath);
15+
const tempUserHomeDir = mkdtempSync(tempBaseDir);
1416

15-
const n8nDirPath = path.join(userDir, '.n8n');
17+
const tempN8nDir = path.join(tempUserHomeDir, '.n8n');
1618

17-
mkdirSync(n8nDirPath);
19+
mkdirSync(tempN8nDir);
1820

1921
writeFileSync(
20-
path.join(n8nDirPath, 'config'),
22+
path.join(tempN8nDir, 'config'),
2123
JSON.stringify({ encryptionKey: 'temp_encryption_key', instanceId: 'temp-123' }),
2224
'utf-8',
2325
);
2426

25-
// @TODO: Find better approach than overriding like this
26-
// Setting N8N_USER_FOLDER has no effect
27-
const instanceSettings = Container.get(InstanceSettings);
28-
instanceSettings.n8nFolder = n8nDirPath;
29-
Container.set(InstanceSettings, instanceSettings);
27+
process.env.N8N_USER_FOLDER = tempUserHomeDir;
3028

31-
Container.get(Logger).info(
32-
`[Benchmarking] Temp .n8n dir location: ${instanceSettings.n8nFolder}`,
33-
);
29+
/**
30+
* `typedi` has already instantiated `InstanceSettings` using the default user home,
31+
* so re-instantiate it to ensure it picks up the temp user home dir path.
32+
*/
33+
Container.set(InstanceSettings, new InstanceSettings());
3434
}

packages/core/src/InstanceSettings.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class InstanceSettings {
2121
private readonly userHome = this.getUserHome();
2222

2323
/** The path to the n8n folder in which all n8n related data gets saved */
24-
n8nFolder = path.join(this.userHome, '.n8n'); // @TODO: Solution that keeps this readonly
24+
readonly n8nFolder = path.join(this.userHome, '.n8n'); // @TODO: Solution that keeps this readonly
2525

2626
/** The path to the folder where all generated static assets are copied to */
2727
readonly staticCacheDir = path.join(this.userHome, '.cache/n8n/public');

0 commit comments

Comments
 (0)