Repository navigation
feat: add custom resolvers and fetchers - #10246
Conversation
| * Example: { type: '@company/cdn', cdnUrl: '...' } | ||
| */ | ||
| export interface CustomResolution { | ||
| type: `@${string}/${string}` // Scoped name (e.g., '@company/cdn', '@acme/artifactory') |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
| resolvedVia: string | ||
| normalizedBareSpecifier?: string | ||
| alias?: string | ||
| lockfileResolution?: Resolution |
There was a problem hiding this comment.
This new field is not used anywhere.
There was a problem hiding this comment.
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
CustomResolverandCustomFetcherinterfaces 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.
| "@pnpm/types": "workspace:*", | ||
| "@types/normalize-path": "catalog:", "@zkochan/rimraf": "catalog:", |
There was a problem hiding this comment.
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.
| 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) { |
There was a problem hiding this comment.
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.
| 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' | |
| ) | |
| } |
|
Please add changesets |
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
resolversandfetchersas top-level pnpmfile exports. (The newfetchersis not to be confused withhooks.fetchers, which this PR deprecates.)resolversis an array of custom resolvers, andfetchersis 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,resolveandshouldForceResolveare 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,fetchis 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, andhooks.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
@deprecatedannotation tohooks.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.skipin case others want to try to get them running.