Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ process.abort = unavailable('process.abort()');
process.chdir = unavailable('process.chdir()');
process.umask = wrappedUmask;
process.cwd = rawMethods.cwd;
process.cwd.resetCwdCache = () => {};
Comment thread
joyeecheung marked this conversation as resolved.
Outdated

if (credentials.implementsPosixCredentials) {
process.initgroups = unavailable('process.initgroups()');
Expand Down
5 changes: 5 additions & 0 deletions lib/internal/bootstrap/switches/does_own_process_state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -107,6 +108,10 @@ function wrapPosixCredentialSetters(credentials) {
// directory is changed by `chdir`, it'll be updated.
let cachedCwd = '';

function resetCwdCache() {
cachedCwd = '';
Comment thread
joyeecheung marked this conversation as resolved.
Outdated
}

function wrappedChdir(directory) {
validateString(directory, 'directory');
rawMethods.chdir(directory);
Expand Down
1 change: 1 addition & 0 deletions lib/internal/process/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -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], '-')) {
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());
});
49 changes: 49 additions & 0 deletions test/parallel/test-snapshot-cwd.js
Original file line number Diff line number Diff line change
@@ -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

Comment thread
arcanis marked this conversation as resolved.
Outdated
require('../common');
const assert = require('assert');
const { spawnSync } = require('child_process');
Comment thread
aduh95 marked this conversation as resolved.
Outdated
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');
Comment thread
arcanis marked this conversation as resolved.
Outdated
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.
Comment thread
arcanis marked this conversation as resolved.
Outdated
Comment thread
arcanis marked this conversation as resolved.
Outdated
const child = spawnSync(process.execPath, [
'--snapshot-blob',
blobPath,
file,
], {
cwd: subdir,
encoding: 'utf8'
});

assert.strictEqual(child.status, 0);
assert.strictEqual(child.stdout, `${subdir}\n`);
}