Skip to content

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

Closed
afn wants to merge 2 commits into
get-bb:mainfrom
afn:sparse-plugin-git-checkout
Closed

afn wants to merge 2 commits into
get-bb:mainfrom
afn:sparse-plugin-git-checkout

Conversation

@afn

@afn afn commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Human comments

What was wrong

Stacked on #7911 (shallow-plugin-git-fetch); only the last commit is new here. 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 or large unrelated content, the working-tree write and blob download dominate what remains of the install time. Refs #7910.

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, 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.
  • install-sources.ts promoteImmutableDir: a sparse sibling checkout leaves a symlinked plugin entry dangling in the shared <repo>/<commit> cache dir. Hashing it threw, which skipped the move-aside and made the later rename fail with ENOTDIR. The hash comparison and the move-aside are now independent.
  • 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 the base branch (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.
  • vitest run test/services/plugins test/services/plugin-catalog: 765 passed; the 2 failing files (plugin-authoring-docs, plugin-authoring-doc-examples) fail identically on the base branch because packages/plugin-sdk/bundled-types is not built here. pnpm run typecheck in apps/server: clean. oxlint on the touched files reports only the 3 pre-existing errors present on the base branch.

Fixes #

AGENT GENERATED

afn added 2 commits November 20, 2026 09:22
Install, update, and install-probe for git: sources ran a full git clone of
the repository, checked out the resolved commit, and deleted .git. For
repositories with a long history this dominated install time and could hit
the install command timeout.

cloneGitCommit now runs git init + git fetch --depth 1 <url> <commit> and
checks out FETCH_HEAD. When the server refuses to serve the commit directly
(protocol v0 servers reject unadvertised objects) it falls back to the full
clone and checkout as before.

Fixes get-bb#7910
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 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 sparse sibling checkout leaves such a symlink
dangling in the shared cache directory; promoteImmutableDir now 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.

Stacked on "Fetch only the resolved commit for git plugin installs".
@github-actions github-actions Bot added needs-approval PR author is not on the approved contributor list needs-issue PR does not close an open issue labels Nov 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for the pull request, @afn. It was closed automatically because it does not meet the requirements below yet. This is an automated check, not a review of the change.

Contributor approval. bb reviews pull requests only from approved contributors, and you are not on the list yet. Join the #contributors channel in Discord (https://discord.gg/kvBU6tJhcJ), post a link to your pull request, and explain what it changes and why. A maintainer then adds you to the list and reopens the pull request.

Linked issue. Every pull request must close an open issue in this repository. The issue keeps the problem tracked even if this pull request closes or we take a different approach. Open an issue for the change, or find an existing one, and add Fixes #<number> to the pull request description. Feature requests and UI changes need a maintainer's sign-off on the issue first. Link the issue before you ask for approval.

See CONTRIBUTING.md for details.

@github-actions github-actions Bot closed this Nov 20, 2026
@ymichael

Copy link
Copy Markdown
Collaborator

Leaving this closed for now: #7911 removes the history download, which is most of the cost. Sparse checkout adds four fallback paths and changes shared cache promotion for a smaller remaining saving. Happy to revisit it as a separate issue with measurements.

@afn

afn commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Sounds good. Thanks @ymichael for the quick turnaround on merging the other PR! Hopefully that takes care of the biggest bottleneck, but I’ll reopen this with benchmarks if it seems like it will make a significant difference.

@afn

afn commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #8116 (rebased onto main after #7911 merged; tracking issue #8115).

@afn

afn commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Hi @ymichael - I've reopened this as #8116 after confirming that (at least in our case) it makes a significant improvement in plugin installation time (see benchmarks in #8115). Thanks!

ymichael pushed a commit that referenced this pull request Nov 25, 2026
)

## 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
add`s 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 #8115).

Fixes #8115

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

Labels

needs-approval PR author is not on the approved contributor list needs-issue PR does not close an open issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants