From c82cdc366f2ac66ab47bf845b2b8fdbfc20e0d00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Wed, 14 Nov 2018 20:10:54 +0000 Subject: [PATCH 1/3] Fixes the resolution when a package has an invalid "main" but a valid "index.js" --- .../packages/invalid-main-1.0.0/package.json | 5 +++++ .../pkg-tests/pkg-tests-specs/sources/pnp.js | 20 +++++++++++++++++++ src/util/generate-pnp-map-api.tpl.js | 7 +++++-- 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 packages/pkg-tests/pkg-tests-fixtures/packages/invalid-main-1.0.0/package.json diff --git a/packages/pkg-tests/pkg-tests-fixtures/packages/invalid-main-1.0.0/package.json b/packages/pkg-tests/pkg-tests-fixtures/packages/invalid-main-1.0.0/package.json new file mode 100644 index 0000000000..90bc5010e2 --- /dev/null +++ b/packages/pkg-tests/pkg-tests-fixtures/packages/invalid-main-1.0.0/package.json @@ -0,0 +1,5 @@ +{ + "name": "invalid-main", + "version": "1.0.0", + "main": "DoesntExists" +} diff --git a/packages/pkg-tests/pkg-tests-specs/sources/pnp.js b/packages/pkg-tests/pkg-tests-specs/sources/pnp.js index 487669f959..cbb75c53d5 100644 --- a/packages/pkg-tests/pkg-tests-specs/sources/pnp.js +++ b/packages/pkg-tests/pkg-tests-specs/sources/pnp.js @@ -591,6 +591,26 @@ module.exports = makeTemporaryEnv => { }), ); + test( + `it should ignore the "main" entry if it doesn't resolve`, + makeTemporaryEnv( + { + dependencies: { + [`invalid-main`]: `1.0.0`, + }, + }, + {plugNPlay: true}, + async ({path, run, source}) => { + await run(`install`); + + await expect(source(`require("invalid-main")`)).resolves.toMatchObject({ + name: `invalid-main`, + version: `1.0.0`, + }); + }, + ), + ); + test( `it should use the regular Node resolution when requiring files outside of the pnp install tree`, makeTemporaryEnv({}, {plugNPlay: true}, async ({path, run, source}) => { diff --git a/src/util/generate-pnp-map-api.tpl.js b/src/util/generate-pnp-map-api.tpl.js index 306db98d79..9075adc2ef 100644 --- a/src/util/generate-pnp-map-api.tpl.js +++ b/src/util/generate-pnp-map-api.tpl.js @@ -176,8 +176,11 @@ function applyNodeExtensionResolution(unqualifiedPath, {extensions}) { // If the "main" field changed the path, we start again from this new location if (nextUnqualifiedPath && nextUnqualifiedPath !== unqualifiedPath) { - unqualifiedPath = nextUnqualifiedPath; - continue; + const resolution = applyNodeExtensionResolution(nextUnqualifiedPath, {extensions}); + + if (resolution !== null) { + return resolution; + } } } From 0a3a5f73146890cb6b55c79e312005f4ae56b433 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Wed, 14 Nov 2018 20:14:41 +0000 Subject: [PATCH 2/3] Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bef6d3b90..88442452d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ Please add one entry in this file for each change in Yarn's behavior. Use the sa ## Master +- Fixes a resolution issue when a package had an invalid `main` entry + + [#6682](https://github.com/yarnpkg/yarn/pull/6682) - [**Maƫl Nison**](https://twitter.com/arcanis) + ## 1.12.3 **Important:** This release contains a cache bump. It will cause the very first install following the upgrade to take slightly more time, especially if you don't use the [Offline Mirror](https://yarnpkg.com/blog/2016/11/24/offline-mirror/) feature. After that everything will be back to normal. From 11a26c6d255f0131313e5152e72d520e4b7ba105 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Wed, 14 Nov 2018 20:10:54 +0000 Subject: [PATCH 3/3] Fixes the resolution when a package has an invalid "main" but a valid "index.js" --- .../packages/invalid-main-1.0.0/index.js | 10 ++++++++++ .../packages/invalid-main-1.0.0/package.json | 5 +++++ .../pkg-tests/pkg-tests-specs/sources/pnp.js | 20 +++++++++++++++++++ src/util/generate-pnp-map-api.tpl.js | 7 +++++-- 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 packages/pkg-tests/pkg-tests-fixtures/packages/invalid-main-1.0.0/index.js create mode 100644 packages/pkg-tests/pkg-tests-fixtures/packages/invalid-main-1.0.0/package.json diff --git a/packages/pkg-tests/pkg-tests-fixtures/packages/invalid-main-1.0.0/index.js b/packages/pkg-tests/pkg-tests-fixtures/packages/invalid-main-1.0.0/index.js new file mode 100644 index 0000000000..a6bf8f5865 --- /dev/null +++ b/packages/pkg-tests/pkg-tests-fixtures/packages/invalid-main-1.0.0/index.js @@ -0,0 +1,10 @@ +/* @flow */ + +module.exports = require(`./package.json`); + +for (const key of [`dependencies`, `devDependencies`, `peerDependencies`]) { + for (const dep of Object.keys(module.exports[key] || {})) { + // $FlowFixMe The whole point of this file is to be dynamic + module.exports[key][dep] = require(dep); + } +} diff --git a/packages/pkg-tests/pkg-tests-fixtures/packages/invalid-main-1.0.0/package.json b/packages/pkg-tests/pkg-tests-fixtures/packages/invalid-main-1.0.0/package.json new file mode 100644 index 0000000000..90bc5010e2 --- /dev/null +++ b/packages/pkg-tests/pkg-tests-fixtures/packages/invalid-main-1.0.0/package.json @@ -0,0 +1,5 @@ +{ + "name": "invalid-main", + "version": "1.0.0", + "main": "DoesntExists" +} diff --git a/packages/pkg-tests/pkg-tests-specs/sources/pnp.js b/packages/pkg-tests/pkg-tests-specs/sources/pnp.js index 487669f959..cbb75c53d5 100644 --- a/packages/pkg-tests/pkg-tests-specs/sources/pnp.js +++ b/packages/pkg-tests/pkg-tests-specs/sources/pnp.js @@ -591,6 +591,26 @@ module.exports = makeTemporaryEnv => { }), ); + test( + `it should ignore the "main" entry if it doesn't resolve`, + makeTemporaryEnv( + { + dependencies: { + [`invalid-main`]: `1.0.0`, + }, + }, + {plugNPlay: true}, + async ({path, run, source}) => { + await run(`install`); + + await expect(source(`require("invalid-main")`)).resolves.toMatchObject({ + name: `invalid-main`, + version: `1.0.0`, + }); + }, + ), + ); + test( `it should use the regular Node resolution when requiring files outside of the pnp install tree`, makeTemporaryEnv({}, {plugNPlay: true}, async ({path, run, source}) => { diff --git a/src/util/generate-pnp-map-api.tpl.js b/src/util/generate-pnp-map-api.tpl.js index 306db98d79..9075adc2ef 100644 --- a/src/util/generate-pnp-map-api.tpl.js +++ b/src/util/generate-pnp-map-api.tpl.js @@ -176,8 +176,11 @@ function applyNodeExtensionResolution(unqualifiedPath, {extensions}) { // If the "main" field changed the path, we start again from this new location if (nextUnqualifiedPath && nextUnqualifiedPath !== unqualifiedPath) { - unqualifiedPath = nextUnqualifiedPath; - continue; + const resolution = applyNodeExtensionResolution(nextUnqualifiedPath, {extensions}); + + if (resolution !== null) { + return resolution; + } } }