Fix --config
loading: cwd-relative, fallback to require()
#3862
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.
Requirements
Description of the Change
Fixes #3822.
--config
is first interpreted as a cwd-relative path. We check if it exists and is a file. (not directory)If found, we try to parse it according to its file extension. (this behavior is unchanged)
If not found, we attempt to
require.resolve()
it relative to cwd.If
require.resolve()
is able to locate a file, werequire()
it. We do not check file extension nor use a custom loader.Error messages are logged for a) failing to locate a file in the first place and b) failing to parse or load the file that was found. For b) the error message logs the resolved path, so users can see that we found e.g.
../../node_modules/monorepo-shared-config/mocha.js
.Worth noting,
cwd
may not always be the correct directory for resolving. For example, if in the future the--config
value comes from a package.json file --"mocha": "@cspotcode/my-mocha-config"
-- then we should resolve relative to the directory containing package.json, not necessarily cwd.Alternate Designs
Why should this be in core?
Benefits
Possible Drawbacks
Applicable issues
#3822
#3823
#3829