Skip to content

Commit

Permalink
Fixes virtuals w/ loose mode (yarnpkg#1077)
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanis authored Mar 17, 2020
1 parent 0aaefc0 commit 6326648
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
24 changes: 24 additions & 0 deletions .yarn/versions/575ebbc8.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
releases:
"@yarnpkg/cli": prerelease
"@yarnpkg/plugin-node-modules": prerelease
"@yarnpkg/pnpify": prerelease

declined:
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnp"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- vscode-zipfs
- "@yarnpkg/builder"
- "@yarnpkg/core"
- "@yarnpkg/doctor"
- "@yarnpkg/pnp"
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,29 @@ describe(`Features`, () => {
),
);

test(
`it should allow resolutions to top-level hoisting candidates (even if they have peer dependencies)`,
makeTemporaryEnv(
{
dependencies: {
[`no-deps`]: `1.0.0`,
[`forward-peer-deps`]: `1.0.0`,
},
},
{
pnpMode: `loose`,
},
async ({path, run, source}) => {
await run(`install`);

await expect(source(`require('peer-deps')`)).resolves.toMatchObject({
name: `peer-deps`,
version: `1.0.0`,
});
},
),
);

test(
`it should log an exception if a dependency tries to require something it doesn't own but that can be accessed through hoisting`,
makeTemporaryEnv(
Expand Down
21 changes: 10 additions & 11 deletions packages/plugin-node-modules/sources/PnpLooseLinker.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {LinkOptions, MinimalLinkOptions, Package} from '@yarnpkg/core';
import {VirtualFS, ZipOpenFS, ppath, PortablePath, npath, Filename} from '@yarnpkg/fslib';
import {getLibzipPromise} from '@yarnpkg/libzip';
import {PnpInstaller, PnpLinker} from '@yarnpkg/plugin-pnp';
import {NodeModulesPackageNode, buildNodeModulesTree} from '@yarnpkg/pnpify';
import {PnpSettings, makeRuntimeApi, PackageInformation, PhysicalPackageLocator, DependencyTarget} from '@yarnpkg/pnp';
import {LinkOptions, structUtils} from '@yarnpkg/core';
import {VirtualFS, ZipOpenFS, ppath, npath, Filename} from '@yarnpkg/fslib';
import {getLibzipPromise} from '@yarnpkg/libzip';
import {PnpInstaller, PnpLinker} from '@yarnpkg/plugin-pnp';
import {NodeModulesPackageNode, buildNodeModulesTree} from '@yarnpkg/pnpify';
import {PnpSettings, makeRuntimeApi, DependencyTarget} from '@yarnpkg/pnp';

export class PnpLooseLinker extends PnpLinker {
protected mode = `loose`;
Expand Down Expand Up @@ -35,14 +35,13 @@ class PnpLooseInstaller extends PnpInstaller {
pnpSettings.fallbackPool = fallbackPool;

const registerFallback = (name: string, entry: NodeModulesPackageNode) => {
const locator = pnp.findPackageLocator(`${npath.fromPortablePath(entry.target)}/`);
if (locator === null)
throw new Error(`Assertion failed: Expected the target to map to a locator`);
const locator = structUtils.parseLocator(entry.locator);
const identStr = structUtils.stringifyIdent(locator);

if (locator.name === name) {
if (identStr === name) {
fallbackPool.set(name, locator.reference);
} else {
fallbackPool.set(name, [locator.name, locator.reference]);
fallbackPool.set(name, [identStr, locator.reference]);
}
};

Expand Down
1 change: 1 addition & 0 deletions packages/yarnpkg-pnpify/sources/buildNodeModulesTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type NodeModulesBaseNode = {
// The entry for a package within a node_modules
export type NodeModulesPackageNode = {
locator: LocatorKey,
// The source path. Note that the virtual paths have been resolved/lost!
target: PortablePath,
// Hard links are copies of the target; soft links are symlinks to it
linkType: LinkType,
Expand Down

0 comments on commit 6326648

Please sign in to comment.