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

refactor(plugin-link)!: rename resolvers and fetchers #4865

Merged
merged 1 commit into from
Sep 18, 2022
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
6 changes: 6 additions & 0 deletions .yarn/versions/4df7cbc6.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
releases:
"@yarnpkg/plugin-link": major

declined:
- "@yarnpkg/cli"
- "@yarnpkg/core"
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ The following changes only affect people writing Yarn plugins:

- `versionUtils.{fetchBase,fetchRoot,fetchChangedFiles}` have been moved from `@yarnpkg/plugin-version` to `@yarnpkg/plugin-git`. Use `gitUtils.{fetchBase,fetchRoot,fetchChangedFiles}` instead.

- For consistency reasons:
- `Link{Resolver,Fetcher}` have been renamed to `Portal{Resolver,Fetcher}`
- `RawLink{Resolver,Fetcher}` have been renamed to `Link{Resolver,Fetcher}`

### Installs

- The `pnpm` linker avoids creating symlinks that lead to loops on the file system, by moving them higher up in the directory structure.
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-link/sources/LinkFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ export class LinkFetcher implements Fetcher {
);

if (parentFetch.localPath) {
return {packageFs: new CwdFS(sourcePath, {baseFs: sourceFs}), releaseFs: effectiveParentFetch.releaseFs, prefixPath: PortablePath.dot, localPath: sourcePath};
return {packageFs: new CwdFS(sourcePath, {baseFs: sourceFs}), releaseFs: effectiveParentFetch.releaseFs, prefixPath: PortablePath.dot, discardFromLookup: true, localPath: sourcePath};
} else {
return {packageFs: new JailFS(sourcePath, {baseFs: sourceFs}), releaseFs: effectiveParentFetch.releaseFs, prefixPath: PortablePath.dot};
return {packageFs: new JailFS(sourcePath, {baseFs: sourceFs}), releaseFs: effectiveParentFetch.releaseFs, prefixPath: PortablePath.dot, discardFromLookup: true};
}
}
}
39 changes: 15 additions & 24 deletions packages/plugin-link/sources/LinkResolver.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {Resolver, ResolveOptions, MinimalResolveOptions} from '@yarnpkg/core';
import {Descriptor, Locator, Manifest, Package} from '@yarnpkg/core';
import {LinkType} from '@yarnpkg/core';
import {miscUtils, structUtils} from '@yarnpkg/core';
import {npath} from '@yarnpkg/fslib';
import {Resolver, ResolveOptions, MinimalResolveOptions, Package} from '@yarnpkg/core';
import {Descriptor, Locator} from '@yarnpkg/core';
import {LinkType} from '@yarnpkg/core';
import {structUtils} from '@yarnpkg/core';
import {npath} from '@yarnpkg/fslib';

import {LINK_PROTOCOL} from './constants';
import {LINK_PROTOCOL} from './constants';

export class LinkResolver implements Resolver {
supportsDescriptor(descriptor: Descriptor, opts: MinimalResolveOptions) {
Expand Down Expand Up @@ -50,33 +50,24 @@ export class LinkResolver implements Resolver {
};
}

async resolve(locator: Locator, opts: ResolveOptions): Promise<Package> {
if (!opts.fetchOptions)
throw new Error(`Assertion failed: This resolver cannot be used unless a fetcher is configured`);

const packageFetch = await opts.fetchOptions.fetcher.fetch(locator, opts.fetchOptions);

const manifest = await miscUtils.releaseAfterUseAsync(async () => {
return await Manifest.find(packageFetch.prefixPath, {baseFs: packageFetch.packageFs});
}, packageFetch.releaseFs);

async resolve(locator: Locator, opts: ResolveOptions) {
return {
...locator,

version: manifest.version || `0.0.0`,
version: `0.0.0`,

languageName: manifest.languageName || opts.project.configuration.get(`defaultLanguageName`),
languageName: opts.project.configuration.get(`defaultLanguageName`),
linkType: LinkType.SOFT,

conditions: manifest.getConditions(),
conditions: null,

dependencies: opts.project.configuration.normalizeDependencyMap(manifest.dependencies),
peerDependencies: manifest.peerDependencies,
dependencies: new Map(),
peerDependencies: new Map(),

dependenciesMeta: manifest.dependenciesMeta,
peerDependenciesMeta: manifest.peerDependenciesMeta,
dependenciesMeta: new Map(),
peerDependenciesMeta: new Map(),

bin: manifest.bin,
bin: new Map(),
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import {Locator} from '@yarnpkg/
import {structUtils} from '@yarnpkg/core';
import {CwdFS, JailFS, ppath, PortablePath} from '@yarnpkg/fslib';

import {RAW_LINK_PROTOCOL} from './constants';
import {PORTAL_PROTOCOL} from './constants';

export class RawLinkFetcher implements Fetcher {
export class PortalFetcher implements Fetcher {
supports(locator: Locator, opts: MinimalFetchOptions) {
if (!locator.reference.startsWith(RAW_LINK_PROTOCOL))
if (!locator.reference.startsWith(PORTAL_PROTOCOL))
return false;

return true;
}

getLocalPath(locator: Locator, opts: FetchOptions) {
const {parentLocator, path} = structUtils.parseFileStyleRange(locator.reference, {protocol: RAW_LINK_PROTOCOL});
const {parentLocator, path} = structUtils.parseFileStyleRange(locator.reference, {protocol: PORTAL_PROTOCOL});
if (ppath.isAbsolute(path))
return path;

Expand All @@ -26,9 +26,9 @@ export class RawLinkFetcher implements Fetcher {
}

async fetch(locator: Locator, opts: FetchOptions) {
const {parentLocator, path} = structUtils.parseFileStyleRange(locator.reference, {protocol: RAW_LINK_PROTOCOL});
const {parentLocator, path} = structUtils.parseFileStyleRange(locator.reference, {protocol: PORTAL_PROTOCOL});

// If the link target is an absolute path we can directly access it via its
// If the portal target is an absolute path we can directly access it via its
// location on the disk. Otherwise we must go through the package fs.
const parentFetch: FetchResult = ppath.isAbsolute(path)
? {packageFs: new CwdFS(PortablePath.root), prefixPath: PortablePath.dot, localPath: PortablePath.root}
Expand All @@ -52,9 +52,9 @@ export class RawLinkFetcher implements Fetcher {
);

if (parentFetch.localPath) {
return {packageFs: new CwdFS(sourcePath, {baseFs: sourceFs}), releaseFs: effectiveParentFetch.releaseFs, prefixPath: PortablePath.dot, discardFromLookup: true, localPath: sourcePath};
return {packageFs: new CwdFS(sourcePath, {baseFs: sourceFs}), releaseFs: effectiveParentFetch.releaseFs, prefixPath: PortablePath.dot, localPath: sourcePath};
} else {
return {packageFs: new JailFS(sourcePath, {baseFs: sourceFs}), releaseFs: effectiveParentFetch.releaseFs, prefixPath: PortablePath.dot, discardFromLookup: true};
return {packageFs: new JailFS(sourcePath, {baseFs: sourceFs}), releaseFs: effectiveParentFetch.releaseFs, prefixPath: PortablePath.dot};
}
}
}
82 changes: 82 additions & 0 deletions packages/plugin-link/sources/PortalResolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import {Resolver, ResolveOptions, MinimalResolveOptions} from '@yarnpkg/core';
import {Descriptor, Locator, Manifest, Package} from '@yarnpkg/core';
import {LinkType} from '@yarnpkg/core';
import {miscUtils, structUtils} from '@yarnpkg/core';
import {npath} from '@yarnpkg/fslib';

import {PORTAL_PROTOCOL} from './constants';

export class PortalResolver implements Resolver {
supportsDescriptor(descriptor: Descriptor, opts: MinimalResolveOptions) {
if (!descriptor.range.startsWith(PORTAL_PROTOCOL))
return false;

return true;
}

supportsLocator(locator: Locator, opts: MinimalResolveOptions) {
if (!locator.reference.startsWith(PORTAL_PROTOCOL))
return false;

return true;
}

shouldPersistResolution(locator: Locator, opts: MinimalResolveOptions) {
return false;
}

bindDescriptor(descriptor: Descriptor, fromLocator: Locator, opts: MinimalResolveOptions) {
return structUtils.bindDescriptor(descriptor, {
locator: structUtils.stringifyLocator(fromLocator),
});
}

getResolutionDependencies(descriptor: Descriptor, opts: MinimalResolveOptions) {
return {};
}

async getCandidates(descriptor: Descriptor, dependencies: unknown, opts: ResolveOptions) {
const path = descriptor.range.slice(PORTAL_PROTOCOL.length);

return [structUtils.makeLocator(descriptor, `${PORTAL_PROTOCOL}${npath.toPortablePath(path)}`)];
}

async getSatisfying(descriptor: Descriptor, dependencies: Record<string, Package>, locators: Array<Locator>, opts: ResolveOptions) {
const [locator] = await this.getCandidates(descriptor, dependencies, opts);

return {
locators: locators.filter(candidate => candidate.locatorHash === locator.locatorHash),
sorted: false,
};
}

async resolve(locator: Locator, opts: ResolveOptions): Promise<Package> {
if (!opts.fetchOptions)
throw new Error(`Assertion failed: This resolver cannot be used unless a fetcher is configured`);

const packageFetch = await opts.fetchOptions.fetcher.fetch(locator, opts.fetchOptions);

const manifest = await miscUtils.releaseAfterUseAsync(async () => {
return await Manifest.find(packageFetch.prefixPath, {baseFs: packageFetch.packageFs});
}, packageFetch.releaseFs);

return {
...locator,

version: manifest.version || `0.0.0`,

languageName: manifest.languageName || opts.project.configuration.get(`defaultLanguageName`),
linkType: LinkType.SOFT,

conditions: manifest.getConditions(),

dependencies: opts.project.configuration.normalizeDependencyMap(manifest.dependencies),
peerDependencies: manifest.peerDependencies,

dependenciesMeta: manifest.dependenciesMeta,
peerDependenciesMeta: manifest.peerDependenciesMeta,

bin: manifest.bin,
};
}
}
73 changes: 0 additions & 73 deletions packages/plugin-link/sources/RawLinkResolver.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/plugin-link/sources/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const LINK_PROTOCOL = `portal:`;
export const PORTAL_PROTOCOL = `portal:`;

export const RAW_LINK_PROTOCOL = `link:`;
export const LINK_PROTOCOL = `link:`;
18 changes: 9 additions & 9 deletions packages/plugin-link/sources/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import {Plugin} from '@yarnpkg/core';
import {Plugin} from '@yarnpkg/core';

import {LinkFetcher} from './LinkFetcher';
import {LinkResolver} from './LinkResolver';
import {RawLinkFetcher} from './RawLinkFetcher';
import {RawLinkResolver} from './RawLinkResolver';
import {LinkFetcher} from './LinkFetcher';
import {LinkResolver} from './LinkResolver';
import {PortalFetcher} from './PortalFetcher';
import {PortalResolver} from './PortalResolver';

export {PortalFetcher};
export {PortalResolver};
export {LinkFetcher};
export {LinkResolver};
export {RawLinkFetcher};
export {RawLinkResolver};

const plugin: Plugin = {
fetchers: [
RawLinkFetcher,
LinkFetcher,
PortalFetcher,
],
resolvers: [
RawLinkResolver,
LinkResolver,
PortalResolver,
],
};

Expand Down