Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/internal/bootstrap/switches/does_own_process_state.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,17 @@ function wrappedUmask(mask) {
}

function wrappedCwd() {
if (cachedCwd === '')
cachedCwd = rawMethods.cwd();
if (cachedCwd === '') {
try {
cachedCwd = rawMethods.cwd();
} catch (err) {
// Enhance the error message to make it clearer what failed and why
if (err.code === 'ENOENT') {
err.message = 'current working directory does not exist: ' +
'process.cwd() failed because the directory was deleted';
}
throw err;
}
}
return cachedCwd;
}
32 changes: 32 additions & 0 deletions test/parallel/test-cwd-enoent-improved-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';
const common = require('../common');
// Fails with EINVAL on SmartOS, EBUSY on Windows, EBUSY on AIX.
if (common.isSunOS || common.isWindows || common.isAIX || common.isIBMi) {
common.skip('cannot rmdir current working directory');
}

const { isMainThread } = require('worker_threads');

if (!isMainThread) {
common.skip('process.chdir is not available in Workers');
}

const assert = require('assert');
const fs = require('fs');

const tmpdir = require('../common/tmpdir');

const dirname = `${tmpdir.path}/cwd-does-not-exist-${process.pid}`;
tmpdir.refresh();
fs.mkdirSync(dirname);
process.chdir(dirname);
fs.rmdirSync(dirname);

// Test that process.cwd() throws with an improved error message
assert.throws(
() => process.cwd(),
{
code: 'ENOENT',
message: /current working directory does not exist.*process\.cwd\(\) failed because the directory was deleted/,
}
);