Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[node-modules] Adds support for creating link: to a missing folder #1170

Merged
merged 1 commit into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .yarn/versions/8dc23b49.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
releases:
"@yarnpkg/cli": prerelease
"@yarnpkg/plugin-node-modules": 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"
- "@yarnpkg/core"
- "@yarnpkg/doctor"
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,27 @@ describe('Node_Modules', () => {
),
);

test(`should support dependency via link: protocol to a missing folder`,
makeTemporaryEnv(
{
dependencies: {
abc: `link:../abc`,
},
},
async ({path, run, source}) => {
await writeFile(npath.toPortablePath(`${path}/../one-fixed-dep.local/abc.js`), '');

await writeFile(npath.toPortablePath(`${path}/.yarnrc.yml`), `
nodeLinker: "node-modules"
`);

await expect(run(`install`)).resolves.toBeTruthy();

await expect(xfs.lstatPromise(npath.toPortablePath(`${path}/node_modules/abc`))).resolves.toBeDefined();
},
),
);

test(`should support replacement of regular dependency with portal: protocol dependency`,
makeTemporaryEnv(
{
Expand Down
56 changes: 35 additions & 21 deletions packages/plugin-node-modules/sources/NodeModulesLinker.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import {BuildDirective, MessageName, Project} from '@yarnpkg/core';
import {Linker, LinkOptions, MinimalLinkOptions, LinkType} from '@yarnpkg/core';
import {Locator, Package, BuildType} from '@yarnpkg/core';
import {structUtils, Report, Manifest, miscUtils, FinalizeInstallStatus, FetchResult, DependencyMeta} from '@yarnpkg/core';
import {VirtualFS, ZipOpenFS} from '@yarnpkg/fslib';
import {PortablePath, npath, ppath, toFilename, Filename, xfs, FakeFS} from '@yarnpkg/fslib';
import {getLibzipPromise} from '@yarnpkg/libzip';
import {parseSyml} from '@yarnpkg/parsers';
import {AbstractPnpInstaller} from '@yarnpkg/plugin-pnp';
import {NodeModulesLocatorMap, buildLocatorMap, buildNodeModulesTree} from '@yarnpkg/pnpify';
import {PnpSettings, makeRuntimeApi} from '@yarnpkg/pnp';
import cmdShim from '@zkochan/cmd-shim';
import {UsageError} from 'clipanion';
import fs from 'fs';
import {BuildDirective, MessageName, Project, FetchResult} from '@yarnpkg/core';
import {Linker, LinkOptions, MinimalLinkOptions, LinkType} from '@yarnpkg/core';
import {Locator, Package, BuildType, FinalizeInstallStatus} from '@yarnpkg/core';
import {structUtils, Report, Manifest, miscUtils, DependencyMeta} from '@yarnpkg/core';
import {VirtualFS, ZipOpenFS, xfs, FakeFS} from '@yarnpkg/fslib';
import {PortablePath, npath, ppath, toFilename, Filename} from '@yarnpkg/fslib';
import {getLibzipPromise} from '@yarnpkg/libzip';
import {parseSyml} from '@yarnpkg/parsers';
import {AbstractPnpInstaller} from '@yarnpkg/plugin-pnp';
import {NodeModulesLocatorMap, buildLocatorMap} from '@yarnpkg/pnpify';
import {buildNodeModulesTree} from '@yarnpkg/pnpify';
import {PnpSettings, makeRuntimeApi} from '@yarnpkg/pnp';
import cmdShim from '@zkochan/cmd-shim';
import {UsageError} from 'clipanion';
import fs from 'fs';

const STATE_FILE_VERSION = 1;
const NODE_MODULES = `node_modules` as Filename;
Expand Down Expand Up @@ -109,8 +110,11 @@ class NodeModulesInstaller extends AbstractPnpInstaller {

const installStatuses: Array<FinalizeInstallStatus> = [];

for (const [locatorStr, installRecord] of locatorMap.entries()) {
const locator = structUtils.parseLocator(locatorStr);
for (const [locatorKey, installRecord] of locatorMap.entries()) {
if (isLinkLocator(locatorKey))
continue;

const locator = structUtils.parseLocator(locatorKey);
const pnpLocator = {name: structUtils.stringifyIdent(locator), reference: locator.reference};

const pnpEntry = pnp.getPackageInformation(pnpLocator);
Expand Down Expand Up @@ -510,16 +514,26 @@ function refineNodeModulesRoots(locationTree: LocationTree, binSymlinks: BinSyml
return {locationTree: refinedLocationTree, binSymlinks: refinedBinSymlinks};
};

function isLinkLocator(locatorKey: LocatorKey): boolean {
let descriptor = structUtils.parseDescriptor(locatorKey);
if (structUtils.isVirtualDescriptor(descriptor))
descriptor = structUtils.devirtualizeDescriptor(descriptor);

return descriptor.range.startsWith('link:');
};

async function createBinSymlinkMap(installState: NodeModulesLocatorMap, locationTree: LocationTree, projectRoot: PortablePath, {loadManifest}: {loadManifest: (sourceLocation: PortablePath) => Promise<Manifest>}) {
const locatorScriptMap = new Map<LocatorKey, Map<string, string>>();
for (const [locatorKey, {locations}] of installState) {
const manifest = await loadManifest(locations[0]);
let manifest = isLinkLocator(locatorKey) ? null : await loadManifest(locations[0]);

const bin = new Map();
for (const [name, value] of manifest.bin) {
const target = ppath.join(locations[0], value);
if (value !== '' && xfs.existsSync(target)) {
bin.set(name, value);
if (manifest) {
for (const [name, value] of manifest.bin) {
const target = ppath.join(locations[0], value);
if (value !== '' && xfs.existsSync(target)) {
bin.set(name, value);
}
}
}

Expand Down