Skip to content

Conversation

robobun
Copy link
Contributor

@robobun robobun commented Aug 4, 2025

Description

Updates Turborepo's bun.lock parser to better align with Bun's current lockfile format implementation. This addresses version detection issues, adds support for new lockfile fields, and implements v1 optimizations.

Testing Instructions

# Unit tests
cargo test -p turborepo-lockfiles

# Specific Bun tests
cargo test -p turborepo-lockfiles bun

# Run with coverage (requires llvm-tools-preview)
cargo coverage

Test Results

  • ✅ All 238 lockfile tests pass
  • ✅ 72 Bun-specific tests (43 newly added)
  • ✅ No regressions in existing functionality

Background

While investigating Turborepo's bun.lock support against Bun's implementation (src/install/lockfile/bun.lock.zig), I identified several gaps:

  1. Critical: global_change() only detected package manager changes, not lockfile version changes
  2. Missing features: Several fields were not parsed (catalogs, overrides, trusted dependencies)
  3. Performance: V1 workspace optimizations were not implemented
  4. Compatibility: Platform-specific constraints (os/cpu) were ignored

Changes

Core Fixes

  1. Version Detection

    • Added LockfileVersion enum for v0/v1 handling
    • Fixed global_change() to detect version changes (prevents incorrect cache hits)
    • Added bun_global_change() for API consistency with other package managers
  2. New Field Support

    pub struct BunLockfileData {
        lockfile_version: i32,
        workspaces: Map<String, WorkspaceEntry>,
        packages: Map<String, PackageEntry>,
        patched_dependencies: Map<String, String>,
        trusted_dependencies: Vec<String>,      // NEW
        overrides: Map<String, String>,         // NEW
        catalog: Map<String, String>,           // NEW
        catalogs: Map<String, Map<String, String>>, // NEW
    }
  3. Platform Constraints

    pub enum Negatable {
        None,                    // No constraint
        Single(String),          // "darwin"
        Multiple(Vec<String>),   // ["x64", "arm64"]
        Not(Vec<String>),        // ["\!win32", "\!freebsd"]
    }

Feature Implementations

Feature Implementation Tests Added
Catalog Resolution resolve_catalog_version() method 11
Overrides apply_overrides() method 3
V1 Workspaces Direct resolution from workspaces section 10
Platform Constraints Negatable type with serde support 6
Subgraph Filtering Smart metadata filtering 2
Integration Tests Combined feature scenarios 11

Resolution Order

The implementation follows this precedence: catalog → override → patch

Areas of Uncertainty

I want to be transparent about areas where I'm less confident or made assumptions:

  1. V1 Behavior Beyond Workspaces: While I implemented the workspace optimization mentioned in Bun's source, there may be other v1-specific behaviors I'm not aware of.

  2. Trusted Dependencies: I parse and filter these, but I'm unsure if Turborepo needs to actually use this information for any security/installation decisions.

  3. Platform Constraint Application: The Negatable::allows() method is implemented but unused. I'm not sure where in Turborepo's architecture platform filtering should actually occur.

  4. Catalog Precedence: I assumed catalogs should be resolved before overrides based on the code structure, but this precedence isn't explicitly documented.

  5. Error Handling: Some edge cases (like circular workspace dependencies or conflicting overrides) may not be handled optimally.

Potential Issues

  1. Performance: The catalog resolution adds an extra lookup step during package resolution. For large monorepos with many catalog references, this could impact performance.

  2. Backward Compatibility: While I've maintained compatibility with existing lockfiles, the changes to global_change() will cause cache invalidation for projects upgrading Turborepo.

  3. Incomplete Platform Support: The platform constraints are parsed but not actively used for filtering during dependency resolution.

Questions for Reviewers

  1. Should platform constraints actually filter dependencies during resolution, or is parsing/preserving them sufficient?
  2. Is the catalog resolution precedence (catalog → override → patch) correct?
  3. Should trusted dependencies affect any Turborepo behavior?
  4. Are there other v1-specific optimizations beyond workspace handling that should be implemented?
  5. Should the UnsupportedVersion error be recoverable or fatal?

Known Limitations

  1. Trusted dependencies are parsed but don't affect installation behavior
  2. Platform constraints are parsed but not used for dependency filtering (would need higher-level changes)
  3. libc field is not implemented (marked TODO in Bun source)
  4. The Negatable::allows() method for platform matching is unused (added for completeness)

Additional Context

This is a draft PR as I would appreciate feedback on the implementation approach, particularly in areas where I made assumptions about intended behavior.

🤖 Generated with Claude Code

Co-Authored-By: Claude noreply@anthropic.com

Copy link
Contributor

vercel bot commented Aug 4, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
examples-basic-web Ready Ready Preview Comment Sep 30, 2025 9:31pm
examples-designsystem-docs Ready Ready Preview Comment Sep 30, 2025 9:31pm
examples-gatsby-web Ready Ready Preview Comment Sep 30, 2025 9:31pm
examples-kitchensink-blog Ready Ready Preview Comment Sep 30, 2025 9:31pm
examples-nonmonorepo Ready Ready Preview Comment Sep 30, 2025 9:31pm
examples-svelte-web Ready Ready Preview Comment Sep 30, 2025 9:31pm
examples-tailwind-web Ready Ready Preview Comment Sep 30, 2025 9:31pm
examples-vite-web Ready Ready Preview Comment Sep 30, 2025 9:31pm
turbo-site Ready Ready Preview Comment Sep 30, 2025 9:31pm
Copy link
Contributor

vercel bot commented Aug 4, 2025

Someone is attempting to deploy a commit to the Vercel Team on Vercel.

A member of the Team first needs to authorize it.

…rent implementation

Updates Turborepo's bun.lock parser to support features from Bun's latest lockfile
format, addressing compatibility gaps and adding missing functionality.

## What?

- Add LockfileVersion enum for v0/v1 format handling
- Fix global_change() to detect version changes (was only checking package manager)
- Add support for new lockfile fields: trustedDependencies, overrides, catalog/catalogs
- Implement platform-specific constraints (os/cpu fields)
- Add v1 workspace optimizations (reduces redundant package entries)
- Add bun_global_change() standalone function for API consistency

## Why?

The existing implementation had several gaps:
- Cache invalidation could fail when lockfile versions changed
- New Bun lockfile fields were silently ignored
- V1 optimizations were not implemented
- Platform constraints were not parsed

## How?

- Catalog resolution via resolve_catalog_version() method
- Override support via apply_overrides() method
- Negatable enum for platform constraints
- Version-aware workspace resolution
- Comprehensive subgraph filtering

## Testing

Ran tests with:
```bash
cargo test -p turborepo-lockfiles
```

- Added 43 new tests (72 total Bun tests, up from 29)
- All 238 package manager tests pass
- Test coverage includes: version detection, catalogs, overrides, v1 workspaces,
  platform constraints, integration scenarios

## Notes

- Based on Bun's src/install/lockfile/bun.lock.zig
- Follows existing patterns from npm/pnpm/yarn implementations
- Some features (trusted deps, platform filtering) are parsed but not actively used

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@Firgrep
Copy link
Contributor

Firgrep commented Aug 25, 2025

@Jarred-Sumner Any updates or timelines on this? Seems like turborepo with bun catalogs just triggers fan-out tasks whenever there is a change to the lock file (e.g. adding any package, local workspace or external).

@anthonyshew anthonyshew changed the title feat(turborepo-lockfiles): Update bun.lock support to match Bun's current implementation Sep 30, 2025
let workspace_key = format!("{workspace_name}/{name}");
if let Some((_key, entry)) = self.package_entry(&workspace_key) {
// Check if the entry matches the override version (if different from resolved)
Copy link
Contributor

Choose a reason for hiding this comment

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

When an override is specified but the overridden package doesn't exist in the lockfile, the code silently ignores the override and returns the original package instead of failing or returning None.

View Details
📝 Patch Details
diff --git a/crates/turborepo-lockfiles/src/bun/mod.rs b/crates/turborepo-lockfiles/src/bun/mod.rs
index d78140d22..96a69e7b8 100644
--- a/crates/turborepo-lockfiles/src/bun/mod.rs
+++ b/crates/turborepo-lockfiles/src/bun/mod.rs
@@ -416,6 +416,10 @@ impl Lockfile for BunLockfile {
                         key: override_entry.ident.to_string(),
                         version: pkg_version,
                     }));
+                } else {
+                    // Override specified but target package not found in lockfile
+                    // Return None to be consistent with catalog resolution behavior
+                    return Ok(None);
                 }
             }
 

Analysis

Inconsistent error handling for unsatisfiable overrides in BunLockfile.resolve_package()

What fails: In crates/turborepo-lockfiles/src/bun/mod.rs, the resolve_package() method silently ignores overrides when the overridden package version doesn't exist in the lockfile, returning the original package instead of None.

How to reproduce:

 ,
    "packages": {"foo": ["foo@1.0.0", {}, "sha512"]},
    "overrides": {"foo": "2.0.0"}  // 2.0.0 doesn't exist!
}));

let result = lockfile.resolve_package("", "foo", "^1.0.0").unwrap();
// Before fix: returns Some(Package{key: "foo@1.0.0", ...})
// After fix: returns None

Result: The method returns Some(Package{key: "foo@1.0.0"}) instead of None, silently ignoring the override.

Expected: Should return Ok(None) to be consistent with catalog resolution behavior (lines 367-373), which returns Ok(None) when a catalog reference can't be resolved.

Fix: Added else clause after line 419 to return Ok(None) when override package not found, matching the behavior of catalog resolution and preventing silent fallback to the original package.

@anthonyshew anthonyshew marked this pull request as ready for review September 30, 2025 21:40
@anthonyshew anthonyshew requested a review from a team as a code owner September 30, 2025 21:40
Copy link
Contributor

@anthonyshew anthonyshew left a comment

Choose a reason for hiding this comment

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

I think we're far enough along on this to merge. We can put it in people's hands and see how it does on a canary.

Tremendous thanks to @Jarred-Sumner for kindling the flame to get us to do this, finally. Let's roll.

@anthonyshew
Copy link
Contributor

anthonyshew commented Sep 30, 2025

Not sure why the check for a site build is hanging. The site is done building successfully. I'll merge past it.

@anthonyshew anthonyshew merged commit 16a64c5 into vercel:main Sep 30, 2025
39 of 40 checks passed
@Firgrep
Copy link
Contributor

Firgrep commented Oct 1, 2025

@anthonyshew Thanks so much for this! Really appreciate your work!

@anthonyshew
Copy link
Contributor

@Firgrep Appreciate the kind word. Please try it out and let us know how it goes, per this comment.

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

Labels

4 participants