From dc000a55d3752e95244c1e015d6e849d301d8c07 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Fri, 26 Jan 2018 14:16:20 -0500 Subject: [PATCH] cluster: add cwd to cluster.settings This commit allows cluster workers to be created with configurable working directories. Fixes: https://github.com/nodejs/node/issues/16388 PR-URL: https://github.com/nodejs/node/pull/18399 Reviewed-By: James M Snell Reviewed-By: Luigi Pinca --- doc/api/cluster.md | 2 ++ lib/internal/cluster/master.js | 1 + test/parallel/test-cluster-cwd.js | 23 +++++++++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 test/parallel/test-cluster-cwd.js diff --git a/doc/api/cluster.md b/doc/api/cluster.md index 0a3676cb26f71b..8a5f816d747474 100644 --- a/doc/api/cluster.md +++ b/doc/api/cluster.md @@ -745,6 +745,8 @@ changes: * `exec` {string} File path to worker file. **Default:** `process.argv[1]` * `args` {Array} String arguments passed to worker. **Default:** `process.argv.slice(2)` + * `cwd` {string} Current working directory of the worker process. **Default:** + `undefined` (inherits from parent process) * `silent` {boolean} Whether or not to send output to parent's stdio. **Default:** `false` * `stdio` {Array} Configures the stdio of forked processes. Because the diff --git a/lib/internal/cluster/master.js b/lib/internal/cluster/master.js index aa349f8e1afbd7..3c6a398f117433 100644 --- a/lib/internal/cluster/master.js +++ b/lib/internal/cluster/master.js @@ -129,6 +129,7 @@ function createWorkerProcess(id, env) { } return fork(cluster.settings.exec, cluster.settings.args, { + cwd: cluster.settings.cwd, env: workerEnv, silent: cluster.settings.silent, windowsHide: cluster.settings.windowsHide, diff --git a/test/parallel/test-cluster-cwd.js b/test/parallel/test-cluster-cwd.js new file mode 100644 index 00000000000000..e3facfedfe856e --- /dev/null +++ b/test/parallel/test-cluster-cwd.js @@ -0,0 +1,23 @@ +'use strict'; +const common = require('../common'); +const tmpdir = require('../common/tmpdir'); +const assert = require('assert'); +const cluster = require('cluster'); + +if (cluster.isMaster) { + tmpdir.refresh(); + + assert.strictEqual(cluster.settings.cwd, undefined); + cluster.fork().on('message', common.mustCall((msg) => { + assert.strictEqual(msg, process.cwd()); + })); + + cluster.setupMaster({ cwd: tmpdir.path }); + assert.strictEqual(cluster.settings.cwd, tmpdir.path); + cluster.fork().on('message', common.mustCall((msg) => { + assert.strictEqual(msg, tmpdir.path); + })); +} else { + process.send(process.cwd()); + process.disconnect(); +}