Skip to content

Commit

Permalink
Merge pull request #274 from webpack/bugfix/error-in-root-path
Browse files Browse the repository at this point in the history
add workaround to ignore fatal errors during root paths resolving to avoid a breaking change
  • Loading branch information
sokra authored Jan 11, 2021
2 parents e08f936 + f33b2c9 commit c2343ea
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Test

on:
push:
branches: [enhanced-resolve-4]
pull_request:
branches: [enhanced-resolve-4]

jobs:
build:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
node-version: [6.x, 8.x, 10.x, 12.x, 14.x]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: yarn --frozen-lockfile
- run: yarn test
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ For more examples creating different types resolvers (sync/async, context, etc)
| mainFields | ["main"] | A list of main fields in description files |
| mainFiles | ["index"] | A list of main files in directories |
| modules | ["node_modules"] | A list of directories to resolve modules from, can be absolute path or folder name |
| roots | [] | A list of directories to resolve request starting with `/` from |
| ignoreRootsErrors | false | Ignore fatal errors happening during handling of `roots` (allows to add `roots` without a breaking change) |
| unsafeCache | false | Use this cache object to unsafely cache the successful requests |
| plugins | [] | A list of additional resolve plugins which should be applied |
| symlinks | true | Whether to resolve symlinks to their symlinked location |
Expand Down
12 changes: 11 additions & 1 deletion lib/ResolverFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ exports.createResolver = function(options) {
// A list of root paths
const roots = options.roots || [];

// Ignore errors happening when resolving roots
const ignoreRootsErrors = options.ignoreRootsErrors || false;

const restrictions = options.restrictions || [];

// Use this cache object to unsafely cache the successful requests
Expand Down Expand Up @@ -222,7 +225,14 @@ exports.createResolver = function(options) {
});
plugins.push(new ModuleKindPlugin("after-described-resolve", "raw-module"));
roots.forEach(root => {
plugins.push(new RootPlugin("after-described-resolve", root, "relative"));
plugins.push(
new RootPlugin(
"after-described-resolve",
root,
"relative",
ignoreRootsErrors
)
);
});
plugins.push(new JoinRequestPlugin("after-described-resolve", "relative"));

Expand Down
19 changes: 17 additions & 2 deletions lib/RootPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ class RootPlugin {
* @param {string | ResolveStepHook} source source hook
* @param {Array<string>} root roots
* @param {string | ResolveStepHook} target target hook
* @param {boolean=} ignoreErrors ignore error during resolving of root paths
*/
constructor(source, root, target) {
constructor(source, root, target, ignoreErrors) {
this.root = root;
this.source = source;
this.target = target;
this._ignoreErrors = ignoreErrors;
}

/**
Expand All @@ -44,7 +46,20 @@ class RootPlugin {
obj,
`root path ${this.root}`,
resolveContext,
callback
this._ignoreErrors
? (err, result) => {
if (err) {
if (resolveContext.log) {
resolveContext.log(
`Ignored fatal error while resolving root path:\n${err}`
);
}
return callback();
}
if (result) return callback(null, result);
callback();
}
: callback
);
});
}
Expand Down
28 changes: 27 additions & 1 deletion test/roots.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,18 @@ describe("roots", () => {
foo: "/fixtures"
},
roots: [__dirname, fixtures],
fileSystem
fileSystem,
ignoreRootsErrors: true,
plugins: [
{
apply(resolver) {
resolver.hooks.file.tap("Test", request => {
if (/test.fixtures.*test.fixtures/.test(request.path))
throw new Error("Simulate a fatal error in root path");
});
}
}
]
});

const contextResolver = ResolverFactory.createResolver({
Expand Down Expand Up @@ -95,4 +106,19 @@ describe("roots", () => {
done();
});
});

it("should resolve an absolute path", done => {
resolver.resolve(
{},
fixtures,
path.join(fixtures, "b.js"),
{},
(err, result) => {
if (err) return done(err);
if (!result) throw new Error("No result");
result.should.equal(path.resolve(fixtures, "b.js"));
done();
}
);
});
});

0 comments on commit c2343ea

Please sign in to comment.