Skip to content

process: improve error message when cwd no longer exists#61736

Open
scott-memco wants to merge 1 commit intonodejs:mainfrom
scott-memco:fix/process-cwd-error-message
Open

process: improve error message when cwd no longer exists#61736
scott-memco wants to merge 1 commit intonodejs:mainfrom
scott-memco:fix/process-cwd-error-message

Conversation

@scott-memco
Copy link

Summary

When the current working directory is deleted or unmounted while a Node.js process is running, process.cwd() throws:

Error: ENOENT: no such file or directory, uv_cwd
    at process.wrappedCwd [as cwd] (node:internal/bootstrap/switches/does_own_process_state:142:28)

The error references uv_cwd (a libuv internal) but never mentions process.cwd() or explains that the directory was deleted. This is confusing to debug, especially when triggered deep in dependency code.

Changes

lib/internal/bootstrap/switches/does_own_process_state.js

Wraps the rawMethods.cwd() call in wrappedCwd() to catch ENOENT and throw a descriptive error:

  • Sets message to: "The current working directory does not exist. It may have been deleted or unmounted. Change to an existing directory and try again."
  • Sets syscall to process.cwd instead of uv_cwd
  • Preserves err.code (ENOENT) so existing programmatic checks still work
  • Re-throws non-ENOENT errors unchanged

Before:

Error: ENOENT: no such file or directory, uv_cwd

After:

Error: The current working directory does not exist. It may have been deleted or unmounted. Change to an existing directory and try again.

test/parallel/test-process-cwd-enoent.js (new)

Regression test that:

  1. Creates a temp directory
  2. Spawns a child process that chdirs into it, deletes it, and calls process.cwd()
  3. Asserts the error has code: 'ENOENT', syscall: 'process.cwd', and a message mentioning "current working directory"
  4. Skipped on Windows (Windows does not allow deleting cwd of a running process)

Fixes: #57045

Made with Cursor

When the current working directory is deleted or unmounted while a
Node.js process is running, process.cwd() throws an error that
references the internal libuv syscall (uv_cwd) rather than
process.cwd(). This makes the error confusing to debug, especially
when triggered deep in dependency code.

Wrap the raw cwd() call in wrappedCwd() to catch ENOENT and throw a
descriptive error that:
- Explicitly mentions "current working directory does not exist"
- Sets syscall to "process.cwd" instead of "uv_cwd"
- Preserves the ENOENT error code for programmatic checks
- Re-throws non-ENOENT errors unchanged

Fixes: nodejs#57045
Co-authored-by: Cursor <cursoragent@cursor.com>
@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/startup

@nodejs-github-bot nodejs-github-bot added lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Feb 8, 2026
Copy link
Member

@addaleax addaleax left a comment

Choose a reason for hiding this comment

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

This seems like it duplicates 3518af0

@codecov
Copy link

codecov bot commented Feb 9, 2026

Codecov Report

❌ Patch coverage is 88.23529% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.72%. Comparing base (a879b39) to head (cdeae2e).
⚠️ Report is 8 commits behind head on main.

Files with missing lines Patch % Lines
...ernal/bootstrap/switches/does_own_process_state.js 88.23% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #61736   +/-   ##
=======================================
  Coverage   89.72%   89.72%           
=======================================
  Files         675      675           
  Lines      204525   204540   +15     
  Branches    39303    39305    +2     
=======================================
+ Hits       183508   183525   +17     
+ Misses      13296    13287    -9     
- Partials     7721     7728    +7     
Files with missing lines Coverage Δ
...ernal/bootstrap/switches/does_own_process_state.js 94.33% <88.23%> (-0.80%) ⬇️

... and 27 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

process.cwd() fails in a not usefully descriptive way

4 participants