From d6d6eab7bb4af87b873dbd1e520b27f4904b2596 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Sun, 17 Sep 2023 13:11:29 +0200 Subject: [PATCH 01/11] lib: reset the cwd cache before execution --- .../bootstrap/switches/does_not_own_process_state.js | 1 + lib/internal/bootstrap/switches/does_own_process_state.js | 5 +++++ lib/internal/process/pre_execution.js | 1 + 3 files changed, 7 insertions(+) diff --git a/lib/internal/bootstrap/switches/does_not_own_process_state.js b/lib/internal/bootstrap/switches/does_not_own_process_state.js index 21be25384ff673..083140480ad423 100644 --- a/lib/internal/bootstrap/switches/does_not_own_process_state.js +++ b/lib/internal/bootstrap/switches/does_not_own_process_state.js @@ -9,6 +9,7 @@ process.abort = unavailable('process.abort()'); process.chdir = unavailable('process.chdir()'); process.umask = wrappedUmask; process.cwd = rawMethods.cwd; +process.cwd.resetCwdCache = () => {}; if (credentials.implementsPosixCredentials) { process.initgroups = unavailable('process.initgroups()'); diff --git a/lib/internal/bootstrap/switches/does_own_process_state.js b/lib/internal/bootstrap/switches/does_own_process_state.js index 85b5c3dfcb09ed..94d144afb0c96a 100644 --- a/lib/internal/bootstrap/switches/does_own_process_state.js +++ b/lib/internal/bootstrap/switches/does_own_process_state.js @@ -7,6 +7,7 @@ process.abort = rawMethods.abort; process.umask = wrappedUmask; process.chdir = wrappedChdir; process.cwd = wrappedCwd; +process.cwd.resetCwdCache = resetCwdCache; if (credentials.implementsPosixCredentials) { const wrapped = wrapPosixCredentialSetters(credentials); @@ -107,6 +108,10 @@ function wrapPosixCredentialSetters(credentials) { // directory is changed by `chdir`, it'll be updated. let cachedCwd = ''; +function resetCwdCache() { + cachedCwd = ''; +} + function wrappedChdir(directory) { validateString(directory, 'directory'); rawMethods.chdir(directory); diff --git a/lib/internal/process/pre_execution.js b/lib/internal/process/pre_execution.js index 1f4a08515b5ae9..9c9da2b516aa48 100644 --- a/lib/internal/process/pre_execution.js +++ b/lib/internal/process/pre_execution.js @@ -193,6 +193,7 @@ function patchProcessObject(expandArgv1) { process.exitCode = undefined; process._exiting = false; process.argv[0] = process.execPath; + process.cwd.resetCwdCache(); if (expandArgv1 && process.argv[1] && !StringPrototypeStartsWith(process.argv[1], '-')) { From 7b2cfac4fc18a36e108c58256dfc188c86d94fd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Sun, 17 Sep 2023 13:26:39 +0200 Subject: [PATCH 02/11] test: add a cwd cache bust test --- test/fixtures/snapshot/cwd.js | 10 ++++++ test/parallel/test-snapshot-cwd.js | 49 ++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 test/fixtures/snapshot/cwd.js create mode 100644 test/parallel/test-snapshot-cwd.js diff --git a/test/fixtures/snapshot/cwd.js b/test/fixtures/snapshot/cwd.js new file mode 100644 index 00000000000000..4860cc6662af8a --- /dev/null +++ b/test/fixtures/snapshot/cwd.js @@ -0,0 +1,10 @@ +const { + setDeserializeMainFunction, +} = require('v8').startupSnapshot; + +// To make sure the cwd is present in the cache +process.cwd(); + +setDeserializeMainFunction(() => { + console.log(process.cwd()); +}); diff --git a/test/parallel/test-snapshot-cwd.js b/test/parallel/test-snapshot-cwd.js new file mode 100644 index 00000000000000..8288e06bac601b --- /dev/null +++ b/test/parallel/test-snapshot-cwd.js @@ -0,0 +1,49 @@ +'use strict'; + +// This tests that user land snapshots works when the instance restored from +// the snapshot is launched with -p and -e + +require('../common'); +const assert = require('assert'); +const { spawnSync } = require('child_process'); +const tmpdir = require('../common/tmpdir'); +const fixtures = require('../common/fixtures'); +const fs = require('fs'); +const path = require('path'); + +tmpdir.refresh(); +const blobPath = tmpdir.resolve('snapshot.blob'); +const file = fixtures.path('snapshot', 'cwd.js'); + +const subdir = path.join(tmpdir.path, 'foo'); +fs.mkdirSync(subdir); + +{ + // Create the snapshot. + const child = spawnSync(process.execPath, [ + '--snapshot-blob', + blobPath, + '--build-snapshot', + file, + ], { + cwd: tmpdir.path, + encoding: `utf8` + }); + + assert.strictEqual(child.status, 0); +} + +{ + // Check a custom works. + const child = spawnSync(process.execPath, [ + '--snapshot-blob', + blobPath, + file, + ], { + cwd: subdir, + encoding: `utf8` + }); + + assert.strictEqual(child.status, 0); + assert.strictEqual(child.stdout, `${subdir}\n`); +} From f02a6ab6b7209d98ad9512dd300ac1a7992774f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Sun, 17 Sep 2023 13:51:14 +0200 Subject: [PATCH 03/11] fix lint --- test/parallel/test-snapshot-cwd.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-snapshot-cwd.js b/test/parallel/test-snapshot-cwd.js index 8288e06bac601b..24d7d26f66247a 100644 --- a/test/parallel/test-snapshot-cwd.js +++ b/test/parallel/test-snapshot-cwd.js @@ -27,7 +27,7 @@ fs.mkdirSync(subdir); file, ], { cwd: tmpdir.path, - encoding: `utf8` + encoding: 'utf8' }); assert.strictEqual(child.status, 0); @@ -41,7 +41,7 @@ fs.mkdirSync(subdir); file, ], { cwd: subdir, - encoding: `utf8` + encoding: 'utf8' }); assert.strictEqual(child.status, 0); From 851b29a2549fdd22af98a35496ee2cbe449fd49b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Mon, 18 Sep 2023 09:03:08 +0200 Subject: [PATCH 04/11] leverage addSerializeCallback --- .../switches/does_not_own_process_state.js | 1 - .../bootstrap/switches/does_own_process_state.js | 13 ++++++++++--- lib/internal/process/pre_execution.js | 1 - 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/internal/bootstrap/switches/does_not_own_process_state.js b/lib/internal/bootstrap/switches/does_not_own_process_state.js index 083140480ad423..21be25384ff673 100644 --- a/lib/internal/bootstrap/switches/does_not_own_process_state.js +++ b/lib/internal/bootstrap/switches/does_not_own_process_state.js @@ -9,7 +9,6 @@ process.abort = unavailable('process.abort()'); process.chdir = unavailable('process.chdir()'); process.umask = wrappedUmask; process.cwd = rawMethods.cwd; -process.cwd.resetCwdCache = () => {}; if (credentials.implementsPosixCredentials) { process.initgroups = unavailable('process.initgroups()'); diff --git a/lib/internal/bootstrap/switches/does_own_process_state.js b/lib/internal/bootstrap/switches/does_own_process_state.js index 94d144afb0c96a..8f457de3e1183e 100644 --- a/lib/internal/bootstrap/switches/does_own_process_state.js +++ b/lib/internal/bootstrap/switches/does_own_process_state.js @@ -2,12 +2,17 @@ const credentials = internalBinding('credentials'); const rawMethods = internalBinding('process_methods'); +const { + namespace: { + addSerializeCallback, + isBuildingSnapshot, + }, +} = require('internal/v8/startup_snapshot'); process.abort = rawMethods.abort; process.umask = wrappedUmask; process.chdir = wrappedChdir; process.cwd = wrappedCwd; -process.cwd.resetCwdCache = resetCwdCache; if (credentials.implementsPosixCredentials) { const wrapped = wrapPosixCredentialSetters(credentials); @@ -108,8 +113,10 @@ function wrapPosixCredentialSetters(credentials) { // directory is changed by `chdir`, it'll be updated. let cachedCwd = ''; -function resetCwdCache() { - cachedCwd = ''; +if (isBuildingSnapshot()) { + addSerializeCallback(() => { + cachedCwd = ''; + }); } function wrappedChdir(directory) { diff --git a/lib/internal/process/pre_execution.js b/lib/internal/process/pre_execution.js index 9c9da2b516aa48..1f4a08515b5ae9 100644 --- a/lib/internal/process/pre_execution.js +++ b/lib/internal/process/pre_execution.js @@ -193,7 +193,6 @@ function patchProcessObject(expandArgv1) { process.exitCode = undefined; process._exiting = false; process.argv[0] = process.execPath; - process.cwd.resetCwdCache(); if (expandArgv1 && process.argv[1] && !StringPrototypeStartsWith(process.argv[1], '-')) { From a74d68d587e5566bece438d03141f79bdb6dda1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Mon, 18 Sep 2023 10:01:52 +0200 Subject: [PATCH 05/11] leverage spawnSyncAndExitWithoutError --- test/parallel/test-snapshot-cwd.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/test/parallel/test-snapshot-cwd.js b/test/parallel/test-snapshot-cwd.js index 24d7d26f66247a..4313ed064bc727 100644 --- a/test/parallel/test-snapshot-cwd.js +++ b/test/parallel/test-snapshot-cwd.js @@ -4,8 +4,7 @@ // the snapshot is launched with -p and -e require('../common'); -const assert = require('assert'); -const { spawnSync } = require('child_process'); +const { spawnSyncAndExitWithoutError } = require('../common/child_process'); const tmpdir = require('../common/tmpdir'); const fixtures = require('../common/fixtures'); const fs = require('fs'); @@ -20,7 +19,7 @@ fs.mkdirSync(subdir); { // Create the snapshot. - const child = spawnSync(process.execPath, [ + spawnSyncAndExitWithoutError(process.execPath, [ '--snapshot-blob', blobPath, '--build-snapshot', @@ -28,22 +27,22 @@ fs.mkdirSync(subdir); ], { cwd: tmpdir.path, encoding: 'utf8' + }, { + status: 0, }); - - assert.strictEqual(child.status, 0); } { // Check a custom works. - const child = spawnSync(process.execPath, [ + const child = spawnSyncAndExitWithoutError(process.execPath, [ '--snapshot-blob', blobPath, file, ], { cwd: subdir, encoding: 'utf8' + }, { + status: 0, + stdout: `${subdir}\n` }); - - assert.strictEqual(child.status, 0); - assert.strictEqual(child.stdout, `${subdir}\n`); } From 3736efe9d6a12d2f7e9caec8d7c8178b693c2cff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Mon, 18 Sep 2023 10:31:58 +0200 Subject: [PATCH 06/11] use trim --- test/parallel/test-snapshot-cwd.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-snapshot-cwd.js b/test/parallel/test-snapshot-cwd.js index 4313ed064bc727..70134174d84efb 100644 --- a/test/parallel/test-snapshot-cwd.js +++ b/test/parallel/test-snapshot-cwd.js @@ -34,7 +34,7 @@ fs.mkdirSync(subdir); { // Check a custom works. - const child = spawnSyncAndExitWithoutError(process.execPath, [ + spawnSyncAndExitWithoutError(process.execPath, [ '--snapshot-blob', blobPath, file, @@ -43,6 +43,7 @@ fs.mkdirSync(subdir); encoding: 'utf8' }, { status: 0, - stdout: `${subdir}\n` + trim: true, + stdout: subdir }); } From 9b9b5201d4e7ac1a60d6614d24aa70dc61210d63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Mon, 18 Sep 2023 10:54:08 +0200 Subject: [PATCH 07/11] Update test/parallel/test-snapshot-cwd.js Co-authored-by: Antoine du Hamel --- test/parallel/test-snapshot-cwd.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-snapshot-cwd.js b/test/parallel/test-snapshot-cwd.js index 70134174d84efb..964b78bc50b6d7 100644 --- a/test/parallel/test-snapshot-cwd.js +++ b/test/parallel/test-snapshot-cwd.js @@ -44,6 +44,6 @@ fs.mkdirSync(subdir); }, { status: 0, trim: true, - stdout: subdir + stdout: subdir, }); } From d4b132bdc088036a394c5f311b61d3cfb49d6bb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Mon, 18 Sep 2023 12:48:05 +0200 Subject: [PATCH 08/11] Update test/parallel/test-snapshot-cwd.js --- test/parallel/test-snapshot-cwd.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/parallel/test-snapshot-cwd.js b/test/parallel/test-snapshot-cwd.js index 964b78bc50b6d7..f4cfaaf67935cf 100644 --- a/test/parallel/test-snapshot-cwd.js +++ b/test/parallel/test-snapshot-cwd.js @@ -33,7 +33,6 @@ fs.mkdirSync(subdir); } { - // Check a custom works. spawnSyncAndExitWithoutError(process.execPath, [ '--snapshot-blob', blobPath, From 12eab3d3ee0cf7567ea902f1accc7c2083b3dd82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Mon, 18 Sep 2023 12:49:24 +0200 Subject: [PATCH 09/11] Update test/parallel/test-snapshot-cwd.js --- test/parallel/test-snapshot-cwd.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-snapshot-cwd.js b/test/parallel/test-snapshot-cwd.js index f4cfaaf67935cf..0849e074117b9a 100644 --- a/test/parallel/test-snapshot-cwd.js +++ b/test/parallel/test-snapshot-cwd.js @@ -1,7 +1,7 @@ 'use strict'; -// This tests that user land snapshots works when the instance restored from -// the snapshot is launched with -p and -e +// This tests that process.cwd() is accurate when +// restoring state from a snapshot require('../common'); const { spawnSyncAndExitWithoutError } = require('../common/child_process'); From e2109f78e775b033f28ddef5a07471af7d51cf79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Mon, 18 Sep 2023 14:22:49 +0200 Subject: [PATCH 10/11] Update test/parallel/test-snapshot-cwd.js Co-authored-by: Livia Medeiros --- test/parallel/test-snapshot-cwd.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-snapshot-cwd.js b/test/parallel/test-snapshot-cwd.js index 0849e074117b9a..4759b1d570b5b9 100644 --- a/test/parallel/test-snapshot-cwd.js +++ b/test/parallel/test-snapshot-cwd.js @@ -14,7 +14,7 @@ tmpdir.refresh(); const blobPath = tmpdir.resolve('snapshot.blob'); const file = fixtures.path('snapshot', 'cwd.js'); -const subdir = path.join(tmpdir.path, 'foo'); +const subdir = tmpdir.resolve('foo'); fs.mkdirSync(subdir); { From f02b286f5c7c9fa6e2b93a507ed5afc90eaf0032 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Mon, 18 Sep 2023 16:08:08 +0200 Subject: [PATCH 11/11] Update test-snapshot-cwd.js --- test/parallel/test-snapshot-cwd.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/parallel/test-snapshot-cwd.js b/test/parallel/test-snapshot-cwd.js index 4759b1d570b5b9..c41ded27b45a3e 100644 --- a/test/parallel/test-snapshot-cwd.js +++ b/test/parallel/test-snapshot-cwd.js @@ -8,7 +8,6 @@ const { spawnSyncAndExitWithoutError } = require('../common/child_process'); const tmpdir = require('../common/tmpdir'); const fixtures = require('../common/fixtures'); const fs = require('fs'); -const path = require('path'); tmpdir.refresh(); const blobPath = tmpdir.resolve('snapshot.blob');