Skip to content

Check out only the selected plugin's directories for git installs - #8116

Merged
ymichael merged 2 commits into
get-bb:mainfrom
afn:sparse-plugin-git-checkout
Nov 25, 2026
Merged

ymichael merged 2 commits into
get-bb:mainfrom
afn:sparse-plugin-git-checkout

Conversation

@afn

@afn afn commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Human comments

What was wrong

After #7911, cloneGitCommit in apps/server/src/services/plugins/managed-plugin-artifacts.ts fetches a single commit but still checks out — and, where the server supports it, downloads — the commit's whole tree, even when the install selects one plugin out of a monorepo (--subdirectory <path> or a .bb/plugins.json collection entry). The selected directory is only picked after the full tree is on disk (resolveSelectedSubdirectory → pluginRootDir). For a marketplace monorepo with many plugins and sizeable unrelated content, the working-tree write (and blob download) dominates install time: #8115 measures 1:26 → 10 s and 23 s → 7 s for two marketplace plugins. Fixes #8115.

Supersedes #7917, which was opened stacked on #7911 before it merged and cannot be reopened after the rebase.

What changed

  • managed-plugin-artifacts.ts: cloneGitCommit takes a GitCheckoutTarget (a selection to resolve, or an installed plugin's recorded subdirectory) and returns the resolved subdirectory, replacing the callers' resolveSelectedSubdirectory step. For a subdirectory or entry selection the shallow path now: adds the URL as the origin remote, fetches with --filter=blob:none so a partial clone can lazily fetch the blobs the checkout needs, sets a cone-mode sparse checkout of the selected directory (or of .bb for an entry, then sparse-checkout adds the directory the manifest names after reading it), and checks out FETCH_HEAD. Cone mode keeps repository-root files, so a root package.json/README is still present. The root selection and the full-clone fallback are unchanged. Servers that do not advertise partial clone ignore --filter with a warning; the sparse working tree still applies.
  • Fallbacks to the full tree at checkout time, so the caller sees exactly what a full clone would: cone mode refuses to add a path that is not a tree (a symlinked plugin directory), the resolved plugin root (or .bb/plugins.json) does not realpath inside the sparse tree, or the path contains cone-mode pattern characters. Each runs git sparse-checkout disable.
  • Fallback to the full tree at validation time. A plugin can depend on paths outside its directory that a full checkout supplied: a bb.server entry that is a symlink into shared/, or a server source importing ../../shared/module.js (managed installs bundle out-of-root relative source). cloneGitCommit therefore returns a GitCheckout handle and keeps .git; the install, update (stageGitCandidate) and probe paths read the manifest and run validateInstallDir through checkout.withFullTreeFallback(fn), which on failure under a sparse tree runs git sparse-checkout disable (blobs fetched lazily from the origin promisor remote on a partial clone) and retries once. Cancellation is never retried; if expanding fails, the step's own error is reported. checkout.finish() removes .git before the content hash and promotion. The assertions between the manifest read and validation (plugin id, registration, builtin shadowing) stay outside the fallback.
  • Links that leave the sparse tree. A plugin's skills directory can be a tracked symlink such as skills -> ../../shared/skills; skill discovery treats an unreadable skills root as no skills rather than failing, so neither the manifest read nor validation would throw and the fallback above would never run: the install would succeed with its skills silently gone. Before accepting a sparse tree, cloneGitCommit now walks the selected plugin root (keepSparseCheckoutIfLinksStayInside): a symlink that does not resolve, or resolves to a directory outside the plugin root (cone mode writes only the files of directories outside the selection, so such a directory is incomplete), disables the sparse checkout before anything reads the tree. Links to files that resolve stay.
  • install-sources.ts promoteGitPluginArtifact: a shared <repo>/<commit> checkout created by a sparse install lacks whatever that plugin did not need, and promotion into an existing checkout moved only the plugin root. That left no shared directories for a later plugin's rebuild from source (after an SDK change), and a repository-root symlink such as alias -> plugins, which cone mode writes without its target, stayed dangling, so promoting alias/alpha failed in mkdir with ENOENT on every retry. Promotion now first adopts every entry of the staging tree the checkout lacks (adoptMissingEntries), merging directories present in both and leaving the roots of plugins already installed from the checkout untouched (installedRoots, from listPluginArtifactsAtOrUnderPath; installs delete .npmrc/.yarnrc* from a plugin root, so merging into one would change its content hash), then promotes the plugin root at its real path so a symlinked plugin directory resolves to it. The protected roots are matched by their real checkout-relative paths as well as the recorded ones (protectedCheckoutRoots): a plugin installed through a directory symlink is recorded as alias/alpha while adoption walks plugins/alpha, and a sibling's full-tree fallback would otherwise put back the files its install removed and leave its content hash stale. promoteImmutableDir moves aside whatever occupies the target even when hashing it fails, instead of failing the later rename with ENOTDIR.
  • The update path passes row.sourceGitSubdirectory directly and keeps not consulting the collection manifest.
  • No wire, CLI, or guide changes.

How you verified

  • New test checks out only the directories the selected plugin needs (plugin-install.test.ts): installs entry alpha, asserts plugins/alpha, .bb/plugins.json and README.md are present in the shared checkout while unrelated/ and plugins/beta are absent; then installs plugins/beta by subdirectory and asserts both plugins coexist and unrelated/ is still absent. Fails on main (the whole tree is present).
  • New test installs a collection entry from a server that serves partial clones: sets uploadpack.allowFilter=true on the fixture repo so --filter=blob:none is honored and the lazy blob fetch through the origin promisor remote is exercised; asserts the built plugin and the absence of unselected directories.
  • The existing promotes an in-repository symlinked plugin after a sibling test caught both the cone-mode add refusal and the dangling-symlink promote failure during development; it passes with the fallbacks and the promoteImmutableDir change.
  • New tests for the validation-time fallback and the shared-checkout promotion, each failing without them with the quoted error: materializes the whole commit when the selected plugin imports from outside its directory (a sparse sibling with a committed .npmrc installs first; a plugin importing ../../shared/message.js then installs, the bundle contains the shared string, shared/message.ts is in the checkout, and the sibling's .npmrc stays removed; failed with server bundle build ... failed), materializes the whole commit when the selected plugin's entry is a symlink out of its directory (failed with manifest bb.server points at a missing file), installs a plugin through a symlinked directory a sibling left dangling in the shared checkout (root alias -> plugins; failed with ENOENT ... mkdir '.../alias'), and in plugin-update.test.ts updates a subdirectory plugin that imports from outside its directory (failed with ok: false).
  • Second-round tests, each failing without the change: materializes the whole commit when the selected plugin's skills are a symlink out of its directory (after a sparse sibling; the SKILL.md resolves through the link and readPluginManifest(...).skillNames is ["review"]), leaves a plugin installed through a symlinked path untouched when a sibling expands the shared checkout (other/beta, then alias/alpha with a committed .npmrc, then plugins/gamma whose shared import forces the full tree; plugins/alpha/.npmrc stays removed and alpha's content hash still matches its artifact), and in plugin-update.test.ts updates a subdirectory plugin whose skills are a symlink out of its directory.
  • pnpm exec turbo run test --filter=@bb/server -- test/services/plugins/plugin-install.test.ts test/services/plugins/plugin-update.test.ts: 113 passed; the whole test/services/plugins/ directory: all passed (the 10 update-resolver.test.ts failures are the same on main on this machine: a global git fsmonitor--daemon.ipc socket copied with a fixture repo). pnpm run typecheck in apps/server: clean. oxlint on the touched files reports only the errors already present on main.
  • Real-world timings on a marketplace monorepo, before/after this change: plugin1 1:26.62 → 10.409 s, plugin2 23.005 s → 7.434 s (table in Plugin git installs check out the whole repository tree when only one subdirectory is selected #8115).

Fixes #8115

AGENT GENERATED

Installing a plugin from a subdirectory or a collection entry of a git
repository materialized the whole tree of the resolved commit even though
only one plugin directory was used. For monorepos with many plugins or
large unrelated content this dominated the working-tree write and, with a
partial-clone capable server, the download.

cloneGitCommit now takes the checkout target. For a subdirectory or entry
selection it fetches with --filter=blob:none from a named origin remote
(so the partial clone can lazily fetch the blobs the checkout needs),
configures a cone-mode sparse checkout of the selected directory (or of
.bb for an entry, then adds the directory the collection manifest names
after reading it), and returns a checkout handle with the resolved
subdirectory, replacing the callers' resolveSelectedSubdirectory step.
The root selection and the full clone fallback are unchanged. Servers
that do not advertise filters ignore --filter with a warning; the sparse
working tree still applies.

Cone mode refuses to add a path that is not a tree (a symlinked plugin
directory) and a symlinked plugin cannot resolve inside a sparse tree, so
in both cases the sparse checkout is disabled and the whole commit is
materialized as before.

A plugin can also depend on paths outside its directory that a full
checkout supplied: a bb.server entry that is a symlink into a shared
directory, or a server source that imports ../../shared/module.js, which
managed installs bundle. The checkout handle therefore keeps .git, and
the install, update and probe paths read the manifest and validate the
install through withFullTreeFallback: when a step fails on a sparse tree,
the handle runs git sparse-checkout disable, which writes the rest of the
commit (fetching blobs lazily from the promisor remote on a partial
clone), and runs the step again. Cancellation is never retried, and a
failure to expand the tree reports the step's own error. finish() removes
.git once the tree is final, before the content hash and the promotion.

A shared <repo>/<commit> checkout that a sparse install created lacks
whatever that plugin did not need. Promotion into an existing checkout
moved only the plugin root, so a later plugin that needed the whole tree
would have no shared directories for a rebuild from source after an SDK
change, and a repository-root symlink such as alias -> plugins, which
cone mode writes without its target, stayed dangling, so promoting
alias/alpha failed in mkdir with ENOENT on every retry for that commit.
promoteGitPluginArtifact now first adopts every entry of the staging tree
the checkout lacks, merging directories present in both and leaving the
roots of plugins already installed from the checkout untouched (an
install removes .npmrc and .yarnrc files from a plugin root, so merging
would change their content hashes), then promotes the plugin root at its
real path, so a symlinked plugin directory resolves to it in the
checkout. promoteImmutableDir moves whatever occupies the target aside
even when hashing it fails, instead of failing the later rename with
ENOTDIR.

The update path passes the installed plugin's recorded subdirectory
directly and keeps not consulting the collection manifest.
@afn
afn force-pushed the sparse-plugin-git-checkout branch from 8ee92ab to 30850aa Compare November 24, 2026 21:45
…talled roots by real path

A plugin's skills directory can be a tracked symlink out of its own
directory, such as skills -> ../../shared/skills. The sparse checkout
left the link dangling, and skill discovery treats an unreadable skills
root as no skills rather than failing, so neither the manifest read nor
validation threw and the fallback to the whole commit never ran: the
install succeeded with its skills silently gone, and so did an update.

cloneGitCommit now walks the selected plugin root before accepting a
sparse tree. A symlink that does not resolve, or that resolves to a
directory outside the plugin root (cone mode writes only the files of
directories outside the selection, so such a directory is incomplete),
disables the sparse checkout before anything reads the tree. Links to
files outside the root stay, since a file that resolves is whole.

Promotion into an existing shared checkout left alone the roots of the
plugins installed from it, comparing the recorded paths. A plugin
installed through a directory symlink is recorded by that path
(alias/alpha) while adoption walks the real tree (plugins/alpha), so a
later sibling's full-tree fallback could put back the .npmrc and .yarnrc
files its install had removed and leave its content hash stale.
promoteGitPluginArtifact now protects each installed root by its real
checkout-relative path as well as the recorded one.

Tests: a plugin whose skills are a symlink out of its directory installs
with the skill present after a sparse sibling, and such a plugin updates;
a plugin installed through alias/alpha keeps its contents and content
hash when a sibling's install expands the shared checkout. Each failed
before this change.
ymichael added a commit that referenced this pull request Nov 25, 2026
…#8128)

## Human comments

Related to #7911 and #8116, this PR speeds up adding/updating git-hosted
marketplaces, which can be painfully slow.

## What was wrong

`materializeGit` in
`apps/server/src/services/plugin-catalog/marketplace-source.ts` ran `git
clone --no-checkout <url>` with no depth or filter, resolved the ref
with `rev-parse`, checked out the whole tree, and deleted `.git` — on
every `bb marketplace add`, every manual refresh, and every periodic
refresh, without ever comparing the ref against the stored
`sourceGitCommit`. For a marketplace monorepo with a long history that
is ~87 s of download per refresh to read a 346 KB `marketplace.json` and
152 KB of icons. #7911 fixed the same clone for plugin installs; the
marketplace path had its own. Fixes #8127.

## What changed

- `marketplace-source.ts`: `materializeGit` first resolves the ref with
`resolveGitRef` (one `ls-remote`, the same resolver plugin installs
use). If it resolves to the stored commit, the stored catalog is
returned as `unchanged: true` with no checkout, like the https path does
on a 304. If the ref moved (or there is no stored commit), the commit is
fetched with `git init` + `fetch --depth 1 --no-tags --filter=blob:none
origin <sha>` into a cone-mode sparse checkout: for a root marketplace,
`sparse-checkout set --cone` (repository-root files only), then after
reading `marketplace.json` `sparse-checkout add` of the directories its
local icons live in; for a `subdirectory` marketplace, the subdirectory
itself. The manifest and icons are then read exactly as before. If
`ls-remote` cannot resolve the ref (for example `origin/<branch>`, which
the existing `rev-parse` fallback accepts), the server refuses the
fetch, the manifest, the subdirectory, or a local icon path crosses a
symlink (whose target may lie outside the sparse tree), `sparse-checkout
add` fails, or an icon directory or the subdirectory contains
sparse-pattern characters, the code falls back to the previous full
clone + checkout (`sparse-checkout disable` where a sparse tree already
exists), so the caller sees the same tree a full clone would. Servers
that do not advertise partial clone ignore `--filter` with a warning and
the sparse working tree still applies.
- `marketplace-icons.ts`: `onlyMissing` now also reuses cached *local*
icons whose source path is unchanged, and skips local icons that are not
cached, so an unchanged git refresh never reads from a checkout that was
not made. Local icons only occur for `git:` and `path:` sources; `path:`
is never `unchanged`, and at the same commit the files are identical.
Remote icons behave as before.
- `plugin-catalog-service.ts`: the refresh passes `row.sourceGitCommit`
into `cached`. The add path passes `cached: null` and takes the
sparse-fetch path. A stored catalog that fails to parse is re-read from
the repository with a warning rather than failing the refresh.
- No wire, CLI, or guide changes; which ref and subdirectory a
marketplace reads is unchanged.

## How you verified

- New test `refreshes an unchanged ref from the stored catalog without
fetching the repository` (third-party-marketplaces.test.ts): after
`addMarketplace`, deletes the fixture repository's loose objects so
`ls-remote` still answers but any clone or fetch fails, then asserts
`refreshMarketplaces` returns `ok: true` with the same commit, the
stored icon's content hash is unchanged, no warnings were emitted, and
the staging directory is empty. Fails on `main` (`fatal: … nonexistent
object`).
- New tests `fetches only the catalog and its icons when the ref moved`
(root) and `fetches only the subdirectory when the ref moved`
(subdirectory): serve the fixture over `file://` through an `insteadOf`
rewrite with `uploadpack.allowFilter=true`, commit a new catalog (root
case: with a second icon in a nested directory) plus
`plugins/alpha/dist/index.js`, delete that blob's object, assert that
`git clone` of the repository fails with `possible repository
corruption`, and assert the refresh succeeds with the new commit, both
entries and both icons. Fail on `main` (the full clone needs the missing
blob).
- New `reads the catalog when %s` cases (POSIX): `marketplace.json`, the
subdirectory, an icon file, and the `icons` directory each a symlink to
another path in the repository. The first three failed with the sparse
checkout before the symlink check (`ENOENT … marketplace.json`, `not a
directory in the repository`, icon dropped with a warning); all four
match `main`.
- Existing `reads the selected catalog commit for ref %s` cases still
pass, including `origin/catalog-next`, which `ls-remote` cannot resolve
and which now exercises the full-clone fallback, and `catalog-release`,
an annotated tag that `resolveGitRef` peels.
- `pnpm exec turbo run test --filter=@bb/server --
test/services/plugin-catalog/`: 171 passed (9 files).
`test/services/plugins/plugin-install.test.ts` and
`plugin-update.test.ts`: 104 passed. `turbo run typecheck
--filter=@bb/server`: clean. `oxlint` on the touched files reports only
the `node:fs/promises` import restriction already present on `main`.
- Real-world, through `createPluginCatalogService` against the
marketplace monorepo in #8127 (89 entries, 39 icons stored, identical
warnings before and after): `addMarketplace` 87.05 s → 3.54 s;
`refreshMarketplaces` with the ref unchanged 87.72 s → 0.75 s.

Fixes #8127

> AGENT GENERATED

---------

Co-authored-by: Michael Yong <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit ce81bd5 into get-bb:main Nov 25, 2026
30 checks passed
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.

Plugin git installs check out the whole repository tree when only one subdirectory is selected

2 participants