Skip to content

Commit

Permalink
Fixes the resolution when a package has an invalid "main" but a valid… (
Browse files Browse the repository at this point in the history
#6682)

* Fixes the resolution when a package has an invalid "main" but a valid "index.js"

* Update CHANGELOG.md

* Fixes the resolution when a package has an invalid "main" but a valid "index.js"
  • Loading branch information
arcanis authored Nov 14, 2018
1 parent 8317ddf commit e7ffb52
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "invalid-main",
"version": "1.0.0",
"main": "DoesntExists"
}
20 changes: 20 additions & 0 deletions packages/pkg-tests/pkg-tests-specs/sources/pnp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}) => {
Expand Down
7 changes: 5 additions & 2 deletions src/util/generate-pnp-map-api.tpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down

0 comments on commit e7ffb52

Please sign in to comment.