From 489bf30304f430e5c0b93d85c5753e18a0a57aca Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Mon, 11 Jan 2021 11:55:48 +0100 Subject: [PATCH 1/3] Setup github actions --- .github/workflows/test.yml | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 00000000..0802ccf0 --- /dev/null +++ b/.github/workflows/test.yml @@ -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 From 8b0b411a338ef547005faffbfcbc56049e052379 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Mon, 11 Jan 2021 12:10:46 +0100 Subject: [PATCH 2/3] add test case to test fatal errors in root path resolving --- test/roots.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/test/roots.js b/test/roots.js index 884b65a8..5a884c26 100644 --- a/test/roots.js +++ b/test/roots.js @@ -13,7 +13,17 @@ describe("roots", () => { foo: "/fixtures" }, roots: [__dirname, fixtures], - fileSystem + fileSystem, + 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({ @@ -95,4 +105,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(); + } + ); + }); }); From f33b2c9775c783e5dfe3677a7bfd9ea3e3b65cd6 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Mon, 11 Jan 2021 12:12:44 +0100 Subject: [PATCH 3/3] add workaround to ignore fatal errors during root paths resolving to avoid a breaking change --- README.md | 2 ++ lib/ResolverFactory.js | 12 +++++++++++- lib/RootPlugin.js | 19 +++++++++++++++++-- test/roots.js | 1 + 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 91cf7254..c6384eb6 100644 --- a/README.md +++ b/README.md @@ -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 | diff --git a/lib/ResolverFactory.js b/lib/ResolverFactory.js index bb61b9b6..64187f60 100644 --- a/lib/ResolverFactory.js +++ b/lib/ResolverFactory.js @@ -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 @@ -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")); diff --git a/lib/RootPlugin.js b/lib/RootPlugin.js index 5c0e046f..94ed9b6b 100644 --- a/lib/RootPlugin.js +++ b/lib/RootPlugin.js @@ -13,11 +13,13 @@ class RootPlugin { * @param {string | ResolveStepHook} source source hook * @param {Array} 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; } /** @@ -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 ); }); } diff --git a/test/roots.js b/test/roots.js index 5a884c26..e7a48cd3 100644 --- a/test/roots.js +++ b/test/roots.js @@ -14,6 +14,7 @@ describe("roots", () => { }, roots: [__dirname, fixtures], fileSystem, + ignoreRootsErrors: true, plugins: [ { apply(resolver) {