Repository navigation
fix: resolve relative paths in unloadFile against cwd - #6472
gaurav0107 wants to merge 3 commits into
Conversation
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
agammann
left a comment
There was a problem hiding this comment.
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>
|
Thanks, good catch. Addressed in 5e479b3.
|
|
Thanks for the update. I rechecked |
|
Thanks! I missed one collision case in my recheck, sorry: if Could |
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>
|
@agammann Good catch, confirmed. |
|
Thanks for addressing the collision. I rechecked |
PR Checklist
status: accepting prsOverview
Mocha#loadFiles()loads files withrequire(path.resolve(file)), butunloadFile()passed the path straight torequire.resolve(), which resolves relative tolib/nodejs/. A file added with a relative path (e.g.mocha.addFile("test/a.spec.js")) therefore never left therequirecache; since #6355 the resultingMODULE_NOT_FOUNDis swallowed, sounloadFiles()silently did nothing and the file was not re-run on the next load.unloadFile()now resolves the path against the cwd first, matchingloadFiles(). 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
lintjob failure is unrelated:lint:knip-docsflags the unusedsatteridependency added todocs/package.jsonin #6452.🤖 Opened with Superhuman, an open-source contribution agent.