Skip to content

fix: preserve inherited OLDPWD when importing ShellJS - #1282

Open
maxitoon wants to merge 2 commits into
shelljs:mainfrom
maxitoon:fix/preserve-inherited-oldpwd
Open

maxitoon wants to merge 2 commits into
shelljs:mainfrom
maxitoon:fix/preserve-inherited-oldpwd

Conversation

@maxitoon

Copy link
Copy Markdown

Fixes #1030.

Importing ShellJS currently deletes process.env.OLDPWD, even when it was supplied by the caller. This changes the importing application's environment and can break consumers such as webpack's EnvironmentPlugin.

Remove that import-time deletion. Existing cd() behavior remains responsible for updating OLDPWD after a successful directory change. Consequently, cd('-') can also use a previous directory inherited from the calling environment rather than treating it as absent.

Regression tests run imports in fresh child processes, covering absent, empty, and nonempty values, plus cd('-') with an inherited directory. The existing missing-previous-directory test now establishes and restores its own environment explicitly.

Validation on Linux / Node 20.19.2:

  • Clean base: npm test — 640 passing, 6 skipped; lint passes.
  • New regressions before the source change: three failures.
  • node node_modules/ava/entrypoints/cli.mjs test/env.js test/cd.js — 15 passing.
  • After the change: npm test — 644 passing, 6 skipped; lint passes.
  • npm run check-node-support and git diff --check — pass.

Limitations: other Node versions and native Windows/macOS have not been run locally. The node-support check is static, not a runtime matrix. npm 12.0.2 warned that this Node version is unsupported by its CLI, although the listed commands completed successfully. These are local results, not a claim of hosted CI success.

Implementation, tests, and independent local reviews used AI assistance.

Signed-off-by: Maxime Tolos <104535850+maxitoon@users.noreply.github.com>
Comment thread test/env.js Outdated
t.is(shell.env.SHELLJS_TEST, process.env.SHELLJS_TEST);
});

[undefined, '', 'previous directory'].forEach(oldpwd => {

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.

Please unroll this loop into separate test cases. If you want to reuse code, then you can declare a helper function.

Comment thread test/cd.js Outdated
const result = shell.cd('-'); // Haven't changed yet, so there is no previous directory
const oldpwd = process.env.OLDPWD;
delete process.env.OLDPWD;
t.teardown(() => {

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'm not sure this teardown step is really necessary

@maxitoon

maxitoon commented Oct 14, 2026 •

Copy link
Copy Markdown
Author

Updated in commit 126b239. test/env.js now has separate explicit absent, empty, and nonempty inherited OLDPWD cases. test/cd.js runs the missing-OLDPWD case in an isolated child, removing the need for teardown. Local Linux/Node 20 verification: focused tests 15 passed; full suite 644 passed, 6 skipped; lint passed. Hosted CI for this new commit is pending verification. Correction: my earlier comment was posted before the commit was pushed; the update is now present on this PR.

Signed-off-by: Maxime Tolos <104535850+maxitoon@users.noreply.github.com>
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.

Deletion of OLDPWD env variable creates error with webpack

2 participants