Repository navigation
fix(cd): recover from a deleted working directory - #1277
Draft
vhinayindia wants to merge 1 commit into
Draft
vhinayindia wants to merge 1 commit into
vhinayindia wants to merge 1 commit into
Conversation
nfischer
reviewed
Oct 9, 2026
| var curDir = process.cwd(); | ||
| var curDir; | ||
| try { | ||
| curDir = process.cwd(); |
Member
There was a problem hiding this comment.
I wasn't able to repro this exception when I tried on my system. What I tried was:
shell.cd('~/foobar');
shell.rm('-r', '~/foobar');
shell.cd('~');
shell.echo(shell.pwd()); // success| } | ||
| process.chdir(dir); | ||
| process.env.OLDPWD = curDir; | ||
| if (curDir) { |
Member
There was a problem hiding this comment.
I see where you're coming from with this behavior, but this isn't actually how cd - behaves or how bash sets $OLDPWD. On Linux, this sets $OLDPWD regardless of whether that folder still exists. There isn't an error at all until you try cd - to go back to a deleted folder, at which point it says:
$ cd -
cd: no such file or directory: ~/this_folder_was_deleted
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1050.
When the current working directory has been removed,
process.cwd()throws beforecd()can attempt to enter a valid destination. Allow that specificENOENTcase to continue toprocess.chdir(). Other errors keep the existing handling.After successful recovery, clear
OLDPWDbecause the departed directory's pathname is unavailable. Failed changes preserve it, and normal successful changes still update it as before. Bash can retain the deleted path through its cached logical working directory; ShellJS does not maintain that state, so retaining the preexistingOLDPWDwould point to an older directory instead.The regression tests use isolated child processes and remove only uniquely created empty fixture directories. They cover absolute destinations,
cd('-'), fatal and nonfatal behavior, invalid destinations,OLDPWD, and errors other thanENOENT.Validation on Linux:
test/cd.js: 18 passed.npm test, including posttest lint, on Node 18.20.8, 24.19.0, and 26.10.0: 640 passed, 6 existing skips each.npm run test-with-coverageon Node 24.19.0: passed;src/cd.jshas 100% statement and line coverage.npm run lint,npm run check-node-support, andgit diff --check: passed.Not tested on macOS, Windows, or Node 20/22. The deleted-cwd fixture tests are skipped on Windows, where removing an active working directory is not supported.