Skip to content

feat: add adapter hooks for resolution and fetching - #10201

Closed
TrevorBurnham wants to merge 4 commits into
pnpm:mainfrom
TrevorBurnham:adapter-hooks
Closed

TrevorBurnham wants to merge 4 commits into
pnpm:mainfrom
TrevorBurnham:adapter-hooks

Conversation

@TrevorBurnham

Copy link
Copy Markdown
Contributor

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, resolve and shouldForceResolve are 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, fetch is 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 resolve hook raises a question: Should resolve be allowed to return a resolution with a type that pnpm doesn't know how to handle? The answer here is "yes"; you just need to implement a corresponding fetch.

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 an adapter.fetch method 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, and fetchers. 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.skip in case others want to try to get them running.

I've also validated this branch by using all adapter hooks in a real project.

Comment thread fetching/pick-fetcher/src/index.ts Resolved
Comment thread resolving/default-resolver/src/index.ts Resolved
fetchers?: CustomFetchers
// eslint-disable-next-line
updateConfig?: (config: any) => any
adapters?: Adapter[]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 }]
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 },
  ]
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 },
  ]
}

@zkochan zkochan Nov 24, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 },
  ]
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 },
  ]
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread resolving/default-resolver/src/index.ts Resolved
})
return {
...result,
resolvedVia: 'adapter',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why doesn't the custom resolver populate this field?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to minimize API complexity. Is there a benefit to having different adapters set different resolvedVia values?

Comment thread resolving/default-resolver/src/index.ts Outdated
return {
resolve: async (wantedDependency, opts) => {
const resolution = await resolveFromNpm(wantedDependency, opts as ResolveFromNpmOptions) ??
const resolution = (_resolveFromAdapters && await _resolveFromAdapters(wantedDependency, opts)) ??

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const resolution = (_resolveFromAdapters && await _resolveFromAdapters(wantedDependency, opts)) ??
const resolution = await _resolveFromAdapters?.(wantedDependency, opts) ??

Comment thread resolving/default-resolver/package.json Resolved
Comment thread fetching/pick-fetcher/src/index.ts Resolved
Comment thread fetching/pick-fetcher/src/index.ts Resolved
Comment thread fetching/pick-fetcher/src/index.ts Resolved
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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if it is a problem that this function gets packageId passed, while fetch doesn't get it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@zkochan
zkochan requested a review from d3lm November 19, 2025 00:19
@TrevorBurnham

Copy link
Copy Markdown
Contributor Author

Closing in favor of #10246. The functionality in that PR is the same, but the API design there is reworked per discussion above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants