Skip to content

fix(process): clarify error when process.cwd() fails due to missing d… #58361

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

Closed
Closed
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
17 changes: 14 additions & 3 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ a string representing the location in the code at which
```js
const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack; // Similar to `new Error().stack`
myObject.stack; // Similar to `new Error().stack`
```

The first line of the trace will be prefixed with
Expand Down Expand Up @@ -600,7 +600,7 @@ Indicates that a provided argument is not an allowable type. For example,
passing a function to a parameter which expects a string would be a `TypeError`.

```js
require('node:url').parse(() => { });
require('node:url').parse(() => {});
// Throws TypeError, since it expected a string.
```

Expand Down Expand Up @@ -1121,6 +1121,17 @@ added:

An attempt to invoke an unsupported crypto operation was made.

<a id="ERR_CWD_DELETED"></a>

### `ERR_CWD_DELETED`

<!-- YAML
added:
- v25.0.0
-->

`process.cwd()` was called on a directory that no longer exists.

<a id="ERR_DEBUGGER_ERROR"></a>

### `ERR_DEBUGGER_ERROR`
Expand Down Expand Up @@ -2935,7 +2946,7 @@ The context must be a `SecureContext`.

### `ERR_TLS_INVALID_PROTOCOL_METHOD`

The specified `secureProtocol` method is invalid. It is either unknown, or
The specified `secureProtocol` method is invalid. It is either unknown, or
disabled because it is insecure.

<a id="ERR_TLS_INVALID_PROTOCOL_VERSION"></a>
Expand Down
2 changes: 2 additions & 0 deletions src/node_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ void OOMErrorHandler(const char* location, const v8::OOMDetails& details);
V(ERR_CRYPTO_UNKNOWN_DH_GROUP, Error) \
V(ERR_CRYPTO_UNSUPPORTED_OPERATION, Error) \
V(ERR_CRYPTO_JOB_INIT_FAILED, Error) \
V(ERR_CWD_DELETED, Error) \
V(ERR_DLOPEN_DISABLED, Error) \
V(ERR_DLOPEN_FAILED, Error) \
V(ERR_ENCODING_INVALID_ENCODED_DATA, TypeError) \
Expand Down Expand Up @@ -203,6 +204,7 @@ ERRORS_WITH_CODE(V)
V(ERR_CRYPTO_UNKNOWN_DH_GROUP, "Unknown DH group") \
V(ERR_CRYPTO_UNSUPPORTED_OPERATION, "Unsupported crypto operation") \
V(ERR_CRYPTO_JOB_INIT_FAILED, "Failed to initialize crypto job config") \
V(ERR_CWD_DELETED, "Current working directory has been deleted") \
V(ERR_DLOPEN_FAILED, "DLOpen failed") \
V(ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE, \
"Context not associated with Node.js environment") \
Expand Down
4 changes: 4 additions & 0 deletions src/node_process_methods.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ static void Cwd(const FunctionCallbackInfo<Value>& args) {
size_t cwd_len = sizeof(buf);
int err = uv_cwd(buf, &cwd_len);
if (err) {
if (err == UV_ENOENT) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error that's thrown already has an "ENOENT" error code. This shouldn't be necessary.

return THROW_ERR_CWD_DELETED(env);
}

return env->ThrowUVException(err, "uv_cwd");
}

Expand Down
45 changes: 45 additions & 0 deletions test/parallel/test-cwd-deleted.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

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

// Removing the current working directory is not supported on all platforms.
if (common.isSunOS || common.isWindows || common.isAIX || common.isIBMi) {
common.skip('cannot rmdir current working directory on this platform');
}

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 path = require('path');
const tmpdir = require('../common/tmpdir');

tmpdir.refresh();
const dirname = path.join(tmpdir.path, `cwd-deleted-test-${process.pid}`);
const originalCwd = process.cwd();

fs.mkdirSync(dirname);
process.chdir(dirname);

try {
fs.rmdirSync(dirname);
} catch (err) {
process.chdir(originalCwd);
common.skip(`rmdir failed: ${err.message}. Skipping test.`);
}

assert.throws(
() => {
process.cwd();
},
{
name: 'Error',
message: 'Current working directory has been deleted',
}
);

process.chdir(originalCwd);
2 changes: 1 addition & 1 deletion test/parallel/test-cwd-enoent-preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ proc.stdout.pipe(process.stdout);
proc.stderr.pipe(process.stderr);

proc.once('exit', common.mustCall(function(exitCode, signalCode) {
assert.strictEqual(exitCode, 0);
assert.strictEqual(exitCode, 1);
assert.strictEqual(signalCode, null);
}));