Skip to content

Commit bbc7105

Browse files
arcanisruyadorno
authored andcommitted
lib: reset the cwd cache before execution
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>
1 parent 774c1cf commit bbc7105

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

lib/internal/bootstrap/switches/does_own_process_state.js

+12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
const credentials = internalBinding('credentials');
44
const rawMethods = internalBinding('process_methods');
5+
const {
6+
namespace: {
7+
addSerializeCallback,
8+
isBuildingSnapshot,
9+
},
10+
} = require('internal/v8/startup_snapshot');
511

612
process.abort = rawMethods.abort;
713
process.umask = wrappedUmask;
@@ -107,6 +113,12 @@ function wrapPosixCredentialSetters(credentials) {
107113
// directory is changed by `chdir`, it'll be updated.
108114
let cachedCwd = '';
109115

116+
if (isBuildingSnapshot()) {
117+
addSerializeCallback(() => {
118+
cachedCwd = '';
119+
});
120+
}
121+
110122
function wrappedChdir(directory) {
111123
validateString(directory, 'directory');
112124
rawMethods.chdir(directory);

test/fixtures/snapshot/cwd.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const {
2+
setDeserializeMainFunction,
3+
} = require('v8').startupSnapshot;
4+
5+
// To make sure the cwd is present in the cache
6+
process.cwd();
7+
8+
setDeserializeMainFunction(() => {
9+
console.log(process.cwd());
10+
});

test/parallel/test-snapshot-cwd.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
// This tests that process.cwd() is accurate when
4+
// restoring state from a snapshot
5+
6+
require('../common');
7+
const { spawnSyncAndExitWithoutError } = require('../common/child_process');
8+
const tmpdir = require('../common/tmpdir');
9+
const fixtures = require('../common/fixtures');
10+
const fs = require('fs');
11+
12+
tmpdir.refresh();
13+
const blobPath = tmpdir.resolve('snapshot.blob');
14+
const file = fixtures.path('snapshot', 'cwd.js');
15+
16+
const subdir = tmpdir.resolve('foo');
17+
fs.mkdirSync(subdir);
18+
19+
{
20+
// Create the snapshot.
21+
spawnSyncAndExitWithoutError(process.execPath, [
22+
'--snapshot-blob',
23+
blobPath,
24+
'--build-snapshot',
25+
file,
26+
], {
27+
cwd: tmpdir.path,
28+
encoding: 'utf8'
29+
}, {
30+
status: 0,
31+
});
32+
}
33+
34+
{
35+
spawnSyncAndExitWithoutError(process.execPath, [
36+
'--snapshot-blob',
37+
blobPath,
38+
file,
39+
], {
40+
cwd: subdir,
41+
encoding: 'utf8'
42+
}, {
43+
status: 0,
44+
trim: true,
45+
stdout: subdir,
46+
});
47+
}

0 commit comments

Comments
 (0)