Fix the subprocess error being hidden when the current directory was deleted
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sindre Sorhus · committed by Sindre Sorhus · parent b67f199 · Browse files · 2 files·+47 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+47 added−9 removed
- tests:+38 added−8 removed
- other:+9 added−1 removed
Provenance
- Commit
91d9ded- Tree
ae91d2b- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
b67f199
2 files changed+47 added−9 removed
modified:lib/arguments/cwd.js+9 added-1 removed
16 unmodified lines171819202122232425262728293023313233343512 unmodified lines16 unmodified lines throw error; }};
// `process.cwd()` throws if the current directory has been deleted.// This is only used to improve an error message, so it must not throw itself, which would hide that error.const getCurrentCwd = () => { try { return process.cwd(); } catch {}};
// When `cwd` option has an invalid value, provide with a better error messageexport const fixCwdError = (originalMessage, cwd) => { if (cwd === getDefaultCwd()) { if (cwd === getCurrentCwd()) { return originalMessage; }
let cwdStat;12 unmodified linesmodified:test/arguments/cwd.js+38 added-8 removed
48 unmodified lines495051525353545556575859606162636465606162636465666667686970717273747576777879808182838485868788899091929394959697989910010110210310431 unmodified lines48 unmodified linestest('The "cwd" option defaults to process.cwd() - sync', testErrorCwdDefault, execaSync);
// Windows does not allow removing a directory used as `cwd` of a running subprocessif (!isWindows) { const testCwdPreSpawn = async (t, execaMethod) => { // Deletes the current directory, which makes `process.cwd()` throw const chdirToMissingDirectory = async t => { const currentCwd = process.cwd(); t.teardown(() => { process.chdir(currentCwd); });
const filePath = tempfile(); await mkdir(filePath); process.chdir(filePath); await rmdir(filePath); };
try { t.throws(() => { execaMethod('empty.js'); }, {message: /The current directory does not exist/}); } finally { process.chdir(currentCwd); } const testCwdPreSpawn = async (t, execaMethod) => { await chdirToMissingDirectory(t);
t.throws(() => { execaMethod('empty.js'); }, {message: /The current directory does not exist/}); };
test.serial('The "cwd" option default fails if current cwd is missing', testCwdPreSpawn, execa); test.serial('The "cwd" option default fails if current cwd is missing - sync', testCwdPreSpawn, execaSync);
// The error message mentions the current directory, but computing it must not fail when it has been deleted. // Otherwise, this would hide the subprocess' own error, and would throw even with the `reject: false` option. const testCwdMissingError = async (t, execaMethod) => { await chdirToMissingDirectory(t);
const {failed, code, message} = await execaMethod('does_not_exist', {cwd: FIXTURES_DIRECTORY, reject: false}); t.true(failed); t.is(code, 'ENOENT'); t.true(message.includes('does_not_exist')); };
test.serial('The subprocess error is kept if current cwd is missing', testCwdMissingError, execa); test.serial('The subprocess error is kept if current cwd is missing - sync', testCwdMissingError, execaSync);
// The `cwd` option is valid, so the error message must not mention it at all const testCwdMissingValidOption = async (t, execaMethod) => { await chdirToMissingDirectory(t);
const {message} = await execaMethod('does_not_exist', {cwd: FIXTURES_DIRECTORY, reject: false}); t.false(message.includes('The "cwd" option')); };
test.serial('The "cwd" option is not reported as invalid if current cwd is missing', testCwdMissingValidOption, execa); test.serial('The "cwd" option is not reported as invalid if current cwd is missing - sync', testCwdMissingValidOption, execaSync);}
const cwdNotExisting = {cwd: 'does_not_exist', expectedCode: 'ENOENT', expectedMessage: 'The "cwd" option is invalid'};const cwdTooLong = {cwd: '.'.repeat(1e5), expectedCode: isWindows && majorNodeVersion >= 20 ? 'ENOENT' : 'ENAMETOOLONG', expectedMessage: 'The "cwd" option is invalid'};31 unmodified lines