Skip to content

Commit 799fd24

Browse files
cjihrigevanlucas
authored andcommitted
cluster: add cwd to cluster.settings
This commit allows cluster workers to be created with configurable working directories. Fixes: #16388 PR-URL: #18399 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 84f8e62 commit 799fd24

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

doc/api/cluster.md

+2
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,8 @@ changes:
711711
* `exec` {string} File path to worker file. **Default:** `process.argv[1]`
712712
* `args` {Array} String arguments passed to worker.
713713
**Default:** `process.argv.slice(2)`
714+
* `cwd` {string} Current working directory of the worker process. **Default:**
715+
`undefined` (inherits from parent process)
714716
* `silent` {boolean} Whether or not to send output to parent's stdio.
715717
**Default:** `false`
716718
* `stdio` {Array} Configures the stdio of forked processes. Because the

lib/internal/cluster/master.js

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ function createWorkerProcess(id, env) {
126126
}
127127

128128
return fork(cluster.settings.exec, cluster.settings.args, {
129+
cwd: cluster.settings.cwd,
129130
env: workerEnv,
130131
silent: cluster.settings.silent,
131132
windowsHide: cluster.settings.windowsHide,

test/parallel/test-cluster-cwd.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const cluster = require('cluster');
5+
6+
if (cluster.isMaster) {
7+
common.refreshTmpDir();
8+
9+
assert.strictEqual(cluster.settings.cwd, undefined);
10+
cluster.fork().on('message', common.mustCall((msg) => {
11+
assert.strictEqual(msg, process.cwd());
12+
}));
13+
14+
cluster.setupMaster({ cwd: common.tmpDir });
15+
assert.strictEqual(cluster.settings.cwd, common.tmpDir);
16+
cluster.fork().on('message', common.mustCall((msg) => {
17+
assert.strictEqual(msg, common.tmpDir);
18+
}));
19+
} else {
20+
process.send(process.cwd());
21+
process.disconnect();
22+
}

0 commit comments

Comments
 (0)