From b7d836e2c7f5725884d75c8a8564fada3d0dd69b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Thu, 21 Sep 2023 18:56:33 +0200 Subject: [PATCH] lib: reset the cwd cache before execution PR-URL: https://github.com/nodejs/node/pull/49684 Reviewed-By: Antoine du Hamel Reviewed-By: Ben Noordhuis Reviewed-By: LiviaMedeiros Reviewed-By: Joyee Cheung Reviewed-By: Ruben Bridgewater --- .../switches/does_own_process_state.js | 12 +++++ test/fixtures/snapshot/cwd.js | 10 ++++ test/parallel/test-snapshot-cwd.js | 47 +++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 test/fixtures/snapshot/cwd.js create mode 100644 test/parallel/test-snapshot-cwd.js diff --git a/lib/internal/bootstrap/switches/does_own_process_state.js b/lib/internal/bootstrap/switches/does_own_process_state.js index 85b5c3dfcb..8f457de3e1 100644 --- a/lib/internal/bootstrap/switches/does_own_process_state.js +++ b/lib/internal/bootstrap/switches/does_own_process_state.js @@ -2,6 +2,12 @@ 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; @@ -107,6 +113,12 @@ function wrapPosixCredentialSetters(credentials) { // directory is changed by `chdir`, it'll be updated. let cachedCwd = ''; +if (isBuildingSnapshot()) { + addSerializeCallback(() => { + cachedCwd = ''; + }); +} + function wrappedChdir(directory) { validateString(directory, 'directory'); rawMethods.chdir(directory); diff --git a/test/fixtures/snapshot/cwd.js b/test/fixtures/snapshot/cwd.js new file mode 100644 index 0000000000..4860cc6662 --- /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 0000000000..c41ded27b4 --- /dev/null +++ b/test/parallel/test-snapshot-cwd.js @@ -0,0 +1,47 @@ +'use strict'; + +// This tests that process.cwd() is accurate when +// restoring state from a snapshot + +require('../common'); +const { spawnSyncAndExitWithoutError } = require('../common/child_process'); +const tmpdir = require('../common/tmpdir'); +const fixtures = require('../common/fixtures'); +const fs = require('fs'); + +tmpdir.refresh(); +const blobPath = tmpdir.resolve('snapshot.blob'); +const file = fixtures.path('snapshot', 'cwd.js'); + +const subdir = tmpdir.resolve('foo'); +fs.mkdirSync(subdir); + +{ + // Create the snapshot. + spawnSyncAndExitWithoutError(process.execPath, [ + '--snapshot-blob', + blobPath, + '--build-snapshot', + file, + ], { + cwd: tmpdir.path, + encoding: 'utf8' + }, { + status: 0, + }); +} + +{ + spawnSyncAndExitWithoutError(process.execPath, [ + '--snapshot-blob', + blobPath, + file, + ], { + cwd: subdir, + encoding: 'utf8' + }, { + status: 0, + trim: true, + stdout: subdir, + }); +}