Skip to content

feat: add custom resolvers and fetchers - #10246

Merged
zkochan merged 9 commits into
pnpm:mainfrom
TrevorBurnham:custom-resolvers-fetchers
Nov 30, 2025
Merged

zkochan merged 9 commits into
pnpm:mainfrom
TrevorBurnham:custom-resolvers-fetchers

Conversation

@TrevorBurnham

Copy link
Copy Markdown
Contributor

This is a third attempt at adding a more flexible mechanism for hooking into pnpm's resolution and fetching systems. Previous attempts: #10153, #10201

Design

This branch adds resolvers and fetchers as top-level pnpmfile exports. (The new fetchers is not to be confused with hooks.fetchers, which this PR deprecates.) resolvers is an array of custom resolvers, and fetchers is an array of custom fetchers.

A custom resolver is an object with these methods:

  • canResolve: Tells pnpm whether this resolver can resolve a package. If so, resolve and shouldForceResolve are called.
  • resolve: Provides a resolution for a package.
  • shouldForceResolve (optional): During headless install, can tell pnpm that a package needs to be re-resolved.

A custom fetcher is an object with these methods:

  • canFetch: Tells pnpm whether this fetcher can fetch a package, based on its resolution. If so, fetch is called.
  • fetch: Tells pnpm where to load a package from.

I've written an update to the pnpmfile docs with more details and some examples.

Custom resolution types

A custom resolver can return a standard resolution, or it can return a resolution with a custom type, in which case it needs a corresponding custom fetcher.

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 a custom fetcher for that type.

Relation to hooks

Custom resolvers and fetchers could, in principle, replace hooks.readPackage, hooks.preResolution, hooks.afterAllResolved, and hooks.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. This PR doesn't make any changes to the existing hooks, aside from adding an @deprecated annotation to hooks.fetchers.

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.

Comment thread fetching/pick-fetcher/src/index.ts Resolved
Comment thread hooks/pnpmfile/src/requirePnpmfile.ts Resolved
Comment thread lockfile/types/src/index.ts Outdated
* Example: { type: '@company/cdn', cdnUrl: '...' }
*/
export interface CustomResolution {
type: `@${string}/${string}` // Scoped name (e.g., '@company/cdn', '@acme/artifactory')

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.

This convention looks like a package name but it isn't a package really. Might cause confusion.

I also wonder if the type field should be required at all. After all, authors of custom resolvers/fetchers can add the necessary information into the package id or any other field in the resolution object. Initially I wanted to have a type field to make the fetcher selection seamless but you went with a loop and a separate canFetch function. Hence, the type field's only purpose now is to serve some type of convention.

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.

The problem I was trying to solve here was how to reconcile "users can use custom resolvers to produce whatever resolution they want" with "resolution objects have a well-defined Resolution type". The Resolution type is currently defined as

export type Resolution =
  TarballResolution |
  GitRepositoryResolution |
  DirectoryResolution |
  BinaryResolution

which is a discriminated union based on the type field. If custom resolutions were allowed to have arbitrary strings as their type, then that discriminated union wouldn't work anymore, which would make things messy (both for internal code and for plugin authors; custom fetchers benefit from having cleanly-typed resolution objects).

Declaring the custom resolution type field as following the namespace pattern was the solution I hit on, but I agree that it's awkward for a type to look like a package name. We could go stricter and require custom resolutions to have type: "custom", but OTOH if you have multiple custom resolution types then type feels like the most natural field to use to differentiate them.

How about this:

type?: `custom:${string}` // e.g. "custom:cdn", "custom:artifactory"

That way it's very flexible: You don't have to use type at all for your custom resolution, but if you do, you have to prefix it with custom: to differentiate it from the built-in resolution types.

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.

That format sounds good to me. Also, we can make it required now as making it optional in the future won't be a breaking change.

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.

Ah, I see that type is already optional on the TarballResolution type, so it does need to be required on custom resolutions to avoid ambiguity.

I've updated the custom type format accordingly: e8395dd

Comment thread resolving/resolver-base/src/index.ts Outdated
resolvedVia: string
normalizedBareSpecifier?: string
alias?: string
lockfileResolution?: 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.

This new field is not used anywhere.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a flexible mechanism for custom package resolution and fetching in pnpm through new top-level resolvers and fetchers exports in pnpmfiles. Custom resolvers intercept package resolution before built-in resolvers, while custom fetchers can completely handle fetching for custom resolution types. The implementation includes a caching layer for performance, proper type safety with @-scoped custom resolution types, and the ability for custom fetchers to delegate to pnpm's standard fetchers.

Key changes:

  • Introduces CustomResolver and CustomFetcher interfaces with canResolve/resolve/shouldForceResolve and canFetch/fetch methods respectively
  • Adds caching mechanism for canResolve checks to avoid redundant expensive operations
  • Updates pickFetcher to support async operation and custom fetcher delegation

Reviewed changes

Copilot reviewed 49 out of 50 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
hooks/types/src/index.ts Defines CustomResolver and CustomFetcher interfaces with comprehensive documentation
hooks/types/src/customResolverCache.ts Implements WeakMap-based caching for canResolve results
fetching/pick-fetcher/src/index.ts Updates pickFetcher to be async and check custom fetchers first, with proper error handling for unhandled custom resolution types
resolving/default-resolver/src/index.ts Integrates custom resolvers into resolution flow, checking them before built-in resolvers
hooks/pnpmfile/src/requireHooks.ts Loads top-level resolvers/fetchers exports from pnpmfiles and merges them into hooks
pkg-manager/core/src/install/checkCustomResolverForceResolve.ts Implements shouldForceResolve check to determine if full resolution is needed
releasing/plugin-commands-deploy/src/createDeployFiles.ts Adds pass-through handling for custom resolution types in lockfile conversion
Various package.json/tsconfig.json files Adds dependencies on @pnpm/hooks.types throughout the codebase
Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +47 to +48
"@pnpm/types": "workspace:*",
"@types/normalize-path": "catalog:", "@zkochan/rimraf": "catalog:",

Copilot AI Nov 28, 2025

Copy link

Choose a reason for hiding this comment

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

Incorrect JSON formatting with multiple properties on one line and inconsistent indentation. Lines 47-48 should be properly formatted with each property on its own line and consistent indentation to match the rest of the file.

Copilot uses AI. Check for mistakes.
Comment on lines +12 to +17
packageId: string
}
): Promise<FetchFunction | DirectoryFetcher | GitFetcher | BinaryFetcher> {
// Try custom fetcher hooks first if available
// Custom fetchers act as complete fetcher replacements
if (opts?.customFetchers && opts.customFetchers.length > 0) {

Copilot AI Nov 28, 2025

Copy link

Choose a reason for hiding this comment

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

The packageId field is marked as required in the options type, but the entire opts parameter is optional. This creates an inconsistency: when custom fetchers are provided, packageId becomes effectively required for proper error messages (line 21 uses opts.packageId). Consider making opts required when customFetchers is provided, or make packageId optional and handle the undefined case.

Suggested change
packageId: string
}
): Promise<FetchFunction | DirectoryFetcher | GitFetcher | BinaryFetcher> {
// Try custom fetcher hooks first if available
// Custom fetchers act as complete fetcher replacements
if (opts?.customFetchers && opts.customFetchers.length > 0) {
packageId?: string
}
): Promise<FetchFunction | DirectoryFetcher | GitFetcher | BinaryFetcher> {
// Try custom fetcher hooks first if available
// Custom fetchers act as complete fetcher replacements
if (opts?.customFetchers && opts.customFetchers.length > 0) {
if (!opts.packageId) {
throw new PnpmError(
'MISSING_PACKAGE_ID',
'packageId must be provided in opts when using customFetchers'
)
}

Copilot uses AI. Check for mistakes.
Comment thread pkg-manager/core/src/install/index.ts Resolved
@zkochan

zkochan commented Nov 29, 2025

Copy link
Copy Markdown
Member

Please add changesets

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.

3 participants