Skip to content

fix: resolve relative paths in unloadFile against cwd - #6472

Open
gaurav0107 wants to merge 3 commits into
mochajs:mainfrom
gaurav0107:fix/4548-unloadfiles-throws-module-not-found-in-e
Open

gaurav0107 wants to merge 3 commits into
mochajs:mainfrom
gaurav0107:fix/4548-unloadfiles-throws-module-not-found-in-e

Conversation

@gaurav0107

@gaurav0107 gaurav0107 commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

PR Checklist

Overview

Mocha#loadFiles() loads files with require(path.resolve(file)), but unloadFile() passed the path straight to require.resolve(), which resolves relative to lib/nodejs/. A file added with a relative path (e.g. mocha.addFile("test/a.spec.js")) therefore never left the require cache; since #6355 the resulting MODULE_NOT_FOUND is swallowed, so unloadFiles() silently did nothing and the file was not re-run on the next load.

unloadFile() now resolves the path against the cwd first, matching loadFiles(). Absolute paths (what the CLI passes) are unaffected. Added a node-unit test that unloads via a cwd-relative path; it fails without the change.

The lint job failure is unrelated: lint:knip-docs flags the unused satteri dependency added to docs/package.json in #6452.


🤖 Opened with Superhuman, an open-source contribution agent.

Mocha#loadFiles() requires files via path.resolve(file), but unloadFile()
resolved the given path relative to lib/nodejs/, so files added with a
relative path were never evicted from the require cache.

Fixes mochajs#4548
@gaurav0107
gaurav0107 marked this pull request as ready for review November 6, 2026 18:26

@agammann agammann left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

path.resolve(file) turns a bare module specifier into a path under cwd. Previously unloadFile("some-package") resolved through Node’s module lookup; after this change it returns on MODULE_NOT_FOUND and leaves that package in require.cache. Could cwd resolution apply only to relative path inputs, with a regression for a package specifier?

Only resolve `./`, `../` and absolute inputs strictly against the cwd.
Bare inputs try Node's module lookup first (previous behavior), then fall
back to the cwd so paths like `test/a.spec.js` still unload. Adds
regression tests for a package specifier and a ./-prefixed path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@gaurav0107

Copy link
Copy Markdown
Contributor Author

Thanks, good catch. Addressed in 5e479b3.

./, ../ and absolute inputs now resolve strictly against the cwd. Bare inputs try Node's module lookup first, which is the old behavior, and fall back to the cwd only if that lookup fails. That fallback is still needed because loadFiles passes cwd-relative paths without a dot prefix, like test/a.spec.js. I added regression tests for a package specifier (ms) and for a ./-prefixed path. The package test fails on the previous commit.

@agammann

Copy link
Copy Markdown

Thanks for the update. I rechecked 5e479b3: bare specifiers try Node module lookup first, while relative paths resolve against cwd; the tests include both a package and a dot-prefixed path. This addresses my concern. I reviewed the diff but did not run tests locally. AI-assisted review.

@gaurav0107

Copy link
Copy Markdown
Contributor Author

Thanks for re-checking 5e479b3, @agammann. Glad the bare-specifier and dot-prefixed path cases address your concern.

@agammann

Copy link
Copy Markdown

Thanks! I missed one collision case in my recheck, sorry: if test/a.spec.js exists both under the cwd and as node_modules/test/a.spec.js, loadFiles() loads the cwd file via path.resolve, but unloadFiles() passes the original string to unloadFile(), whose first candidate resolves the package file. The cwd file remains cached, so the tests may not rerun. I reproduced that resolution/cache behavior with an isolated Node 24 fixture using the current candidate logic; I did not run the full Mocha suite.

Could unloadFiles() resolve its known file paths before passing them to the static unloadFile()? That would retain bare package lookup for direct callers. A regression test with both files present would cover the collision. This follow-up was AI-assisted.

loadFiles() and loadFilesAsync() load each entry via path.resolve(), but
unloadFiles() passed the raw string to Mocha.unloadFile(), which tries
Node's module lookup first for bare inputs. A cwd-relative path like
`ms/index.js` that also exists as a package subpath therefore unloaded the
package file and left the loaded test file cached. Resolve against the cwd
in unloadFiles() so it targets the same cache key; direct callers of
Mocha.unloadFile() keep bare package lookup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@gaurav0107

Copy link
Copy Markdown
Contributor Author

@agammann Good catch, confirmed. loadFiles() and loadFilesAsync() both use path.resolve(file), but unloadFiles() passed the raw string through. Addressed in 4922b31: unloadFiles() now resolves each entry against the cwd before calling Mocha.unloadFile(), so direct callers still get bare package lookup. Added a regression test that creates ms/index.js in a temp cwd (colliding with node_modules/ms/index.js), loads it, and checks that unloadFiles() evicts the cwd copy. It failed before the change and passes now, along with the rest of the unit/node-unit suite.

@agammann

Copy link
Copy Markdown

Thanks for addressing the collision. I rechecked 4922b31: resolving the instance’s known file paths before calling Mocha.unloadFile() matches the loading path while preserving bare-package lookup for direct callers. The new regression covers the case I raised, and the hosted Node unit job passed. I reviewed the patch and CI results but did not run the suite locally. AI-assisted review.

@gaurav0107

Copy link
Copy Markdown
Contributor Author

Thanks for re-checking 4922b31, @agammann, and for the careful reviews on this one.

This branch has not been deployed

No deployments
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.

unloadFiles throws MODULE_NOT_FOUND in ESM context for files added with relative path

2 participants