Skip to content

Commit 106ef4c

Browse files
feat: imporve error message
1 parent cb0eb58 commit 106ef4c

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

lib/internal/bootstrap/switches/does_own_process_state.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,17 @@ function wrappedUmask(mask) {
138138
}
139139

140140
function wrappedCwd() {
141-
if (cachedCwd === '')
142-
cachedCwd = rawMethods.cwd();
141+
if (cachedCwd === '') {
142+
try {
143+
cachedCwd = rawMethods.cwd();
144+
} catch (err) {
145+
// Enhance the error message to make it clearer what failed and why
146+
if (err.code === 'ENOENT') {
147+
err.message = 'current working directory does not exist: ' +
148+
'process.cwd() failed because the directory was deleted';
149+
}
150+
throw err;
151+
}
152+
}
143153
return cachedCwd;
144154
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
const common = require('../common');
3+
// Fails with EINVAL on SmartOS, EBUSY on Windows, EBUSY on AIX.
4+
if (common.isSunOS || common.isWindows || common.isAIX || common.isIBMi) {
5+
common.skip('cannot rmdir current working directory');
6+
}
7+
8+
const { isMainThread } = require('worker_threads');
9+
10+
if (!isMainThread) {
11+
common.skip('process.chdir is not available in Workers');
12+
}
13+
14+
const assert = require('assert');
15+
const fs = require('fs');
16+
17+
const tmpdir = require('../common/tmpdir');
18+
19+
const dirname = `${tmpdir.path}/cwd-does-not-exist-${process.pid}`;
20+
tmpdir.refresh();
21+
fs.mkdirSync(dirname);
22+
process.chdir(dirname);
23+
fs.rmdirSync(dirname);
24+
25+
// Test that process.cwd() throws with an improved error message
26+
assert.throws(
27+
() => process.cwd(),
28+
{
29+
code: 'ENOENT',
30+
message: /current working directory does not exist.*process\.cwd\(\) failed because the directory was deleted/,
31+
}
32+
);

0 commit comments

Comments
 (0)