Skip to content

Commit

Permalink
lib: reset the cwd cache before execution
Browse files Browse the repository at this point in the history
PR-URL: #49684
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
arcanis authored and ruyadorno committed Sep 28, 2023
1 parent 774c1cf commit bbc7105
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/internal/bootstrap/switches/does_own_process_state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/snapshot/cwd.js
Original file line number Diff line number Diff line change
@@ -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());
});
47 changes: 47 additions & 0 deletions test/parallel/test-snapshot-cwd.js
Original file line number Diff line number Diff line change
@@ -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,
});
}

0 comments on commit bbc7105

Please sign in to comment.