Skip to content

Can't create new dependency patch using linker = "isolated" #21212

Closed

Description

@i-love-neko-girls

What version of Bun is running?

1.2.19+aad3abead

What platform is your computer?

Darwin 24.0.0 arm64 arm

What steps can reproduce the bug?

  1. install any dependency with --linker=isolated
  2. try to patch it

What is the expected behavior?

works as intended

What do you see instead?

➜ bun patch example
bun patch v1.2.19 (aad3abea)

4 packages installed [50.00ms]

To patch example, edit the following folder:

  node_modules/example

Once you're done with your changes, run:

  bun patch --commit 'node_modules/example'

➜ bun patch --commit 'node_modules/example'
bun patch v1.2.19 (aad3abea)
error: failed to parse patchfile: bad_file_mode
error: failed to apply patchfile (patches/example@0.0.0.patch)

Checked 177 installs across 189 packages (no changes) [31.00ms]

➜ bun patch example
bun patch v1.2.19 (aad3abea)
error: failed to parse patchfile: bad_file_mode
error: failed to apply patchfile (patches/example@0.0.0.patch)

Checked 177 installs across 189 packages (no changes) [14.00ms]
error: error overwriting folder in node_modules: FileNotFound

Additional information

existing patches seems to work

Activity

  1. added
    bun install
    and removed on Jul 20, 2025
  2. added a commit that references this issue on Jul 20, 2025
    94e3050
  3. robobun commented on Aug 13, 2025

    @robobun
    Collaborator

    Analysis

    I've reproduced this issue and identified the root cause. The problem occurs because when using --linker=isolated, Bun creates symlinks in node_modules/ that point to packages in the isolated cache. The patch system expects regular files/directories but encounters symlinks, causing several issues:

    1. Git diff includes symlink metadata: The generated patch contains new file mode 120000 (symlink mode) instead of actual file content
    2. Patch parser fails: The bad_file_mode error occurs in src/patch.zig:524 because the patch parser doesn't handle symlink modes
    3. File operations fail: Subsequent patch operations fail with FileNotFound because the system tries to operate on broken symlinks

    Implementation Plan

    Here's a comprehensive fix approach:

    1. Detect Symlinked Packages in Patch Commands

    Files to modify: src/install/PackageManager/patchPackage.zig

    • In preparePatch() (~line 546): Detect if the target package folder is a symlink
    • In doPatchCommit() (~line 12): Add symlink detection for both path and name-based arguments

    2. Resolve Symlinks Before Git Operations

    Files to modify: src/install/PackageManager/patchPackage.zig

    • In doPatchCommit(), before calling gitDiffPreprocessPaths() (~line 330):
      • Check if changes_dir is an isolated symlink
      • If so, resolve to the actual cache directory path
      • Use the resolved path for git diff operations

    3. Enhance Patch File Mode Parsing

    Files to modify: src/patch.zig

    • Update parseFileMode() (~line 657) to handle symlink modes gracefully
    • Add symlink mode constants and handling logic

    4. Update Error Handling

    Files to modify: src/patch.zig

    • Improve error messages to indicate symlink-related issues
    • Add specific guidance for isolated linker mode

    5. Add Integration Tests

    Files to create: test/regression/issue/21212-patch-isolated-symlinks.test.ts

    • Test patching packages installed with --linker=isolated
    • Verify both patch creation and application work correctly
    • Test error scenarios and edge cases

    Testing Strategy

    1. Unit Tests: Test symlink detection and resolution functions
    2. Integration Tests: Full patch workflow with isolated packages
    3. Edge Cases: Nested symlinks, broken symlinks, permission issues
    4. Cross-platform: Ensure compatibility across Linux, macOS, and Windows

    Alternative Approaches Considered

    1. Always copy files for patches: Would work but increases disk usage
    2. Modify git diff to follow symlinks: More complex, platform-specific issues
    3. Current approach: Detect and resolve symlinks only when needed (recommended)

    This fix should resolve the issue while maintaining compatibility with existing patch workflows.

  4. i-love-neko-girls commented on Aug 14, 2025

    @i-love-neko-girls
    Author

    thank you claude can you fix it then

  5. self-assigned this
    on Oct 7, 2025
  6. iivvaannxx commented on Oct 10, 2025

    @iivvaannxx

    As a workaround, you can temporarily not use the isolated strategy, reinstall all the dependencies, then generate the patch and --commit it. After that, the patched package will be stored in the cache and you can set the isolated setting again. In my case this worked and I have my patched dependency successfully installed with isolated dependencies, even if I delete all node_modules in my workspace and reinstall again it works seamlessly.

  7. dylan-conway commented on Oct 10, 2025

    @dylan-conway
    Member

    Fixed in #23311, and will be included in Bun v1.3 when it's released. In the meantime you can also use a canary build that has the bug fix (bun upgrade --canary).

    Closing as complete. Please comment on this issue if you continue to have issues with patches and isolated installs, or create a new issue.

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

Metadata

Metadata

Assignees

Labels

bugbun install

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions