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 <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: LiviaMedeiros <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
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‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
constcredentials=internalBinding('credentials');
44
constrawMethods=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
letcachedCwd='';
109115

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

‎test/fixtures/snapshot/cwd.js‎

Lines changed: 10 additions & 0 deletions
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‎

Lines changed: 47 additions & 0 deletions
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+
consttmpdir=require('../common/tmpdir');
9+
constfixtures=require('../common/fixtures');
10+
constfs=require('fs');
11+
12+
tmpdir.refresh();
13+
constblobPath=tmpdir.resolve('snapshot.blob');
14+
constfile=fixtures.path('snapshot','cwd.js');
15+
16+
constsubdir=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)