Repository navigation
feat: add adapter hooks for resolution and fetching - #10201
TrevorBurnham wants to merge 4 commits into
Conversation
| fetchers?: CustomFetchers | ||
| // eslint-disable-next-line | ||
| updateConfig?: (config: any) => any | ||
| adapters?: Adapter[] |
There was a problem hiding this comment.
an adapters field nested in a hooks field seems weird to me. Both words basically mean the same.
I'd use fetchers and resolver. They can be objects with the related functions.
hooks: {
resolvers: [{ doResolve, resolve }]
}There was a problem hiding this comment.
One problem with that is that hooks.fetchers is already taken. Also, I do think it's convenient from a code organization standpoint to put related resolvers and fetchers in the same object, even if it isn't technically necessary.
I could move adapters up a level if you think it'd make more sense for adapters and hooks to be siblings. They're different APIs with overlapping functionality, so the words having similar meaning seems fine to me.
There was a problem hiding this comment.
But they are already grouped. An organization can create a pnpmfile that will be loaded as a plugin. They will be grouped in a single pnpmfile.
There was a problem hiding this comment.
It's a matter of writing
// Custom resolver and fetcher for use case A
const resolverA = { canResolve, resolve };
const fetcherA = { canFetch, fetch };vs.
// Custom adapter for use case A
const adapterA = { canResolve, resolve, canFetch, fetch };To me the latter is a bit clearer and more convenient, but it's a minor point. If you want to avoid introducing new terminology, I can figure out an alternative.
There was a problem hiding this comment.
We can do this:
module.exports = {
hooks: { canResolve, resolve, canFetch, fetch }
}I don't know if someone would ever need multiple "groups" but pnpm can load multiple pnpmfiles, so that would be possible as well. But even multiple in a file could work with an array:
module.exports = {
hooks: [
{ canResolve, resolve, canFetch, fetch },
{ canResolve, resolve, canFetch, fetch },
]
}There was a problem hiding this comment.
I tried making that change, which I've pushed as a separate branch. But on reflection, the developer ergonomics don't feel right to me. Having functions like updateConfig and importPackage live at the same level as canResolve and resolve makes the relationships between those functions less clear:
module.exports = {
hooks: [
{ updateConfig, importPackage }, // hooks that act globally
{ canResolve, resolve, canFetch, fetch }, // hooks that act on individual packages
]
}If the hooks.adapters nesting is the issue, let's de-nest the adapters. If the word "adapters" is the issue, let's call them "providers," objects that provide alternate resolution and fetching mechanisms. Putting that together, my proposal is:
module.exports = {
hooks: { updateConfig, importPackage },
providers: [
{ canResolve, resolve, canFetch, fetch },
]
}There was a problem hiding this comment.
Unnested providers sounds good to me.
module.exports = {
hooks: { updateConfig, importPackage },
providers: [
{ canResolve, resolve, canFetch, fetch },
]
}but might be better to name it "packageProviders" instead.
although by mixing resolvers with fetchers we make it look like a resolver should have a dedicated fetcher, which is not the case. The tarball fetcher for instance is used by packages resolved by both the npm-resolver and sometimes the git-resolver. Separating to fetchers and resolvers would make that point move obvious
module.exports = {
hooks: { updateConfig, importPackage },
resolvers: [
{ canResolve, resolve },
],
fetchers: [
{ canFetch, fetch },
]
}There was a problem hiding this comment.
Hmm… packageProviders makes it sound like we're providing packages, rather than providing package resolution/fetching.
Having resolvers and fetchers as siblings of hooks does seem clear, except that hooks.fetchers already means something else.
We could combine the two ideas and have packageResolvers and packageFetchers. OTOH the word "package" in those names is a bit redundant… What about the word "custom," to differentiate these resolvers and fetchers from the internal ones?
module.exports = {
hooks: { updateConfig, importPackage },
customResolvers: [
{ canResolve, resolve },
],
customFetchers: [
{ canFetch, fetch },
]
}There was a problem hiding this comment.
I believe we should deprecate the hooks.fetchers field in v11. That's why I have added @d3lm to the review. I think only StackBlitz currently uses hooks.fetchers.
Hence, just fetchers and resolvers is fine.
| }) | ||
| return { | ||
| ...result, | ||
| resolvedVia: 'adapter', |
There was a problem hiding this comment.
why doesn't the custom resolver populate this field?
There was a problem hiding this comment.
I was trying to minimize API complexity. Is there a benefit to having different adapters set different resolvedVia values?
| return { | ||
| resolve: async (wantedDependency, opts) => { | ||
| const resolution = await resolveFromNpm(wantedDependency, opts as ResolveFromNpmOptions) ?? | ||
| const resolution = (_resolveFromAdapters && await _resolveFromAdapters(wantedDependency, opts)) ?? |
There was a problem hiding this comment.
| const resolution = (_resolveFromAdapters && await _resolveFromAdapters(wantedDependency, opts)) ?? | |
| const resolution = await _resolveFromAdapters?.(wantedDependency, opts) ?? |
| for (const adapter of opts.adapters) { | ||
| if (adapter.canFetch && adapter.fetch) { | ||
| // eslint-disable-next-line no-await-in-loop | ||
| const canFetch = await adapter.canFetch(opts.packageId, resolution) |
There was a problem hiding this comment.
I don't know if it is a problem that this function gets packageId passed, while fetch doesn't get it.
There was a problem hiding this comment.
I was torn on this one. It'd be nice for fetch to receive the packageId, but I also wanted it to receive the same arguments as pnpm's built-in fetchers, which don't.
|
Closing in favor of #10246. The functionality in that PR is the same, but the API design there is reworked per discussion above. |
This PR is an effort to add more flexibility to pnpm's hook system, inspired by Yarn's plugin functionality.
This is a fresh iteration following a previous attempt at the same goal: #10153
Design
The new hooks are all methods on an object called an "adapter." You can have as many adapters as you like, with each one handling a different type of package.
An adapter can have the following methods:
canResolve: Tells pnpm whether this adapter can resolve a package. If so,resolveandshouldForceResolveare called.resolve: Provides a resolution for a package.shouldForceResolve: During headless install, can tell pnpm that a package needs to be re-resolved.canFetch: Tells pnpm whether this adapter can fetch a package, based on its resolution. If so,fetchis called.fetch: Tells pnpm where to load a package from.This set of methods gives plugin authors complete control over how a package is loaded and how it's represented in the lockfile. Depending on the use case, a plugin might use just the resolve hooks, just the fetch hooks, or both.
I've written an update to the pnpmfile docs with more details and some examples.
Custom resolution types
Having a
resolvehook raises a question: Shouldresolvebe allowed to return a resolution with atypethat pnpm doesn't know how to handle? The answer here is "yes"; you just need to implement a correspondingfetch.To avoid ambiguity, this PR introduces the convention that custom resolution types should be
@-scoped. If a resolution has"type": "@my-company/internal-package", that means that you need anadapter.fetchmethod for that type. It makes for a lockfile entry that clearly indicates, "This package needs special handling."Relation to other hooks
The new adapter hooks could, in principle, replace
readPackage,preResolution,afterAllResolved, andfetchers. They're a complete and cohesive solution to the problems that those hooks solve.Of course, they could also peacefully coexist with the existing hooks, which are unaffected by this PR.
Testing
I've added unit tests for all of the new hooks. However, the integration tests I've tried to add emit the error "Jest environment has been torn down" followed by a timeout. This seems to be due to an issue with Jest's dependency resolution (a cycle somewhere, maybe?), but I haven't been able to track it down. This branch includes those integration tests with
test.skipin case others want to try to get them running.I've also validated this branch by using all adapter hooks in a real project.