Skip to content

feat: optimize build capsule creation for unmodified exported dependencies - #9820

Merged
davidfirst merged 11 commits into
masterfrom
feat/optimize-build-capsule-creation
Jul 17, 2025
Merged

davidfirst merged 11 commits into
masterfrom
feat/optimize-build-capsule-creation

Conversation

@davidfirst

@davidfirst davidfirst commented Jul 11, 2025 •

Copy link
Copy Markdown
Member

Optimizes bit build performance by using package manager for unmodified exported dependencies instead of creating unnecessary capsules.

When running bit build, the system now:

  • Creates capsules only for modified components and their dependents
  • Uses package manager to install unmodified exported dependencies as packages
  • Reduces build time and resource usage in large workspaces

The optimization is enabled automatically for build operations and maintains backward compatibility.
To disable this optimization, prefix your command with: BIT_FEATURES=disable-capsule-optimization

- Replace usePackagesForUnmodifiedDeps option with disable-capsule-optimization feature toggle
- Optimization is now enabled by default for all capsule operations (build, snap, tag)
- Only when BIT_FEATURES=disable-capsule-optimization is set, the optimization is disabled
- Remove option from IsolateComponentsOptions interface
- Update builder files to remove the explicit option setting
- Add DISABLE_CAPSULE_OPTIMIZATION constant to feature-toggle module
- Add component.isModified() check to filterUnmodifiedExportedDependencies
- Ensure modified dependencies are always built from source, not installed as packages
- Handle cases like 'bit build comp1' where comp1 has modified dependencies
- Add detailed comments explaining the modified component check logic
@davidfirst
davidfirst merged commit 2f9bf17 into master Jul 17, 2025
@davidfirst
davidfirst deleted the feat/optimize-build-capsule-creation branch July 17, 2025 20:05
luvkapur added a commit that referenced this pull request Aug 18, 2026
## Proposed Changes

- Make capsule optimization dependency-aware: a component can use its
published package only when its entire dependency subtree is also safe
to consume as packages.
- Keep package-only subtrees optimized while retaining mixed-generation
dependency paths as capsules.
- Add a registry-backed constructor-identity regression and strengthen
`bit ci pr --keep-lane` coverage for unchanged components.

Closes #10583.

## Problem

Capsule optimization, introduced in #9820, avoids creating capsules for
exported, built, and unmodified dependencies by installing their
published packages instead.

The previous decision was made independently for each component:

> If this component is published and unmodified, use its package.

That establishes package availability, but not whether the resulting
dependency graph is safe.

A dependency still needs a capsule when it is:

- new or not exported;
- modified locally, even when an older version was published;
- a seeder or original seeder that is part of the current build;
- exported without a successful build;
- not configured as an installable package; or
- a snap that built successfully but was never published.

This allowed a graph such as:

```
A (registry package) ──> B (published package copy)
                             +
                         B (current capsule copy)
```

If `B` had to remain a capsule, the published `A` could still resolve
its recorded package copy of `B`, while another path loaded the current
`B` capsule. JavaScript classes and module state are identified by
runtime instance, not package name or semantic version. Two copies can
therefore break `instanceof`, Harmony singletons, shared remotes,
caches, and error recovery based on constructors.

`bit ci pr --keep-lane` exposed this consistently because a reused lane
can preserve artifacts from an earlier run while later runs rebuild only
changed components.

## Fix

The package side of the optimized graph is now required to be
**dependency-closed**:

> If `A` is installed as a package, every transitive dependency of `A`
must also be safe to install as a package.

The isolator now:

1. Applies the existing eligibility rules:
- seeders, modified components, and non-installable components start in
the capsule set;
- published, built, unmodified, installable components start as package
candidates.
2. Walks dependency edges backwards from the capsule set.
3. Promotes any package candidate that depends on a capsule into the
capsule set.
4. Repeats until no package-to-capsule edge remains.

For example:

```
A ──> B ──> C
            ^
       must be a capsule
```

`C` causes `B` to remain a capsule, which causes `A` to remain a
capsule. Conversely, a subtree that is fully resolvable from the
registry remains optimized:

```
A (package) ──> B (package) ──> C (package)
```

The closure is computed in `O(V + E)` time using a reverse dependency
map and a work queue.

## Behavioral Impact

- Mixed graphs may create more capsules than before. This is the
correctness cost of keeping one coherent module generation.
- Registry-only dependency subtrees keep the original optimization.
- Capsule optimization remains enabled globally.
- Lane reuse remains enabled; this does not force every component on a
reused lane to rebuild.
- Capsule selection gains linear graph traversal and `O(V + E)`
temporary bookkeeping.

## Regression Coverage

The registry-backed regression constructs:

```
comp1 -(dev)-> comp2 -(prod)-> comp3 -(prod)-> comp4
  |                                              ^
  `--------------------(dev)---------------------'
```

`comp4` is modified and must remain a capsule. Before the fix,
registry-installed `comp3` loaded its package copy of `comp4`, while
`comp1` loaded the current capsule. Constructor identity failed with:

```
Expected constructor: SharedClass
Received constructor: SharedClass
```

After the fix, the capsule requirement propagates through `comp3` and
`comp2`, and the test passes. The existing optimization test also proves
that a registry-only subtree is still pruned.

The keep-lane regression now verifies that a second PR run reuses the
remote lane while preserving the exact head of an unchanged component.

## Validation

- Identity regression: fails before the fix and passes after it.
- Capsule optimization matrix: 3 passing, including the registry-only
optimization case.
- Keep-lane reuse suite: 4 passing, including exact unchanged-head
preservation.
- Focused Checkout/Lanes build: 18 passing, 2 pending.
- `pnpm exec tsc --noEmit`.
- oxlint: 0 warnings and 0 errors.
- Prettier and `git diff --check`.

Circle validation used two source-identical runs:

- Fresh-lane control: [bit_pr
#440098](https://circleci.com/gh/teambit/bit/440098) passed.
- Reused-lane run: an empty commit triggered [bit_pr
#440134](https://circleci.com/gh/teambit/bit/440134), which explicitly
logged that the existing remote lane was being reused and completed
successfully.
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