Visitar URL original
fix(cd): recover from a deleted working directory by vhinayindia · Pull Request #1277 · shelljs/shelljs · GitHub
Skip to content

fix(cd): recover from a deleted working directory - #1277

Draft
vhinayindia wants to merge 1 commit into
shelljs:mainfrom
vhinayindia:fix/cd-deleted-cwd
Draft

vhinayindia wants to merge 1 commit into
shelljs:mainfrom
vhinayindia:fix/cd-deleted-cwd

Conversation

@vhinayindia

Copy link
Copy Markdown

Fixes #1050.

When the current working directory has been removed, process.cwd() throws before cd() can attempt to enter a valid destination. Allow that specific ENOENT case to continue to process.chdir(). Other errors keep the existing handling.

After successful recovery, clear OLDPWD because 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 preexisting OLDPWD would 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 than ENOENT.

Validation on Linux:

  • Four recovery regressions fail against unchanged upstream.
  • Focused 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-coverage on Node 24.19.0: passed; src/cd.js has 100% statement and line coverage.
  • npm run lint, npm run check-node-support, and git 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.

Comment thread src/cd.js
var curDir = process.cwd();
var curDir;
try {
curDir = process.cwd();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

Comment thread src/cd.js
}
process.chdir(dir);
process.env.OLDPWD = curDir;
if (curDir) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

I can't cd after I removed a sub-folder!?

2 participants