Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: reset the cwd cache before execution #49684

Merged
merged 11 commits into from
Sep 21, 2023
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());
});
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

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

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