Skip to content

add support for "workspaces.nohoist" and "workspaces.hoistingLimits" #20124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Jun 4, 2025

Conversation

dylan-conway
Copy link
Member

@dylan-conway dylan-conway commented Jun 1, 2025

What does this PR do?

Adds support for "workspaces.nohoist" and "workspaces.hoistingLimits" in bun install.

{
    "name": "hoisting-settings",
    "workspaces": {
        "nohoist": ["pkg1/react", "pkg2/**", "**/react"],
        "hoistingLimits": "dependencies", // or "workspaces" or "none"
        "packages": ["pkg1", "pkg2"]
    },
    "dependencies": {
        "react": "^18"
    }
}

closes #6850

"nohoist"

The "nohoist" field prevents a package from hoisting if any of the specified patterns match it's package path. The patterns support glob syntax. Example:

package.json

{
    "name": "nohoist-react",
    "workspaces": {
        "packages": [
            "pkg1"
        ],
        "nohoist": [
            "pkg1/react"
        ]
    }
}

pkg1/package.json:

{
    "name": "pkg1",
    "dependencies": {
        "react": "18.3.1"
    }
}

This package.json will prevent [email protected] from hoisting out of pkg1. The transitive dependencies of react are still able to hoist to the root. The result of an install would be:

./node_modules
├── [email protected]
├── [email protected]
└── pkg1@workspace:pkg1
    └── [email protected]

If you wanted to prevent react and all it's dependencies from hoisting, "pkg1/react/**" could be added to nohoist:

  "nohoist": [
+   "pkg1/react/**",
    "pkg1/react"
  }

An install would create:

./node_modules
└── pkg1@workspace:pkg1
    └── [email protected]
        └── [email protected]
            └── [email protected]

"hoistingLimits"

The "hoistingLimits" field allows limiting dependency hoisting to workspaces or the direct dependencies of workspaces. There are three valid values: "none", "workspaces", and "dependencies".

"none"

This is the default value. No limitation, all dependencies are allowed to hoist like they normally are.

"workspaces"

Prevents dependencies from hoisting out of workspaces. Example:

package.json

{
    "name": "hoisting-limits-workspaces",
    "workspaces": {
        "packages": ["pkg1", "pkg2"],
        "hoistingLimits": "workspaces"
    }
}

pkg1/package.json

{
    "name": "pkg1",
    "dependencies": {
        "react": "18.3.1"
    }
}

pkg2/package.json

{
    "name": "pkg2",
    "dependencies": {
        "react": "18.3.1"
    }
}

pkg1 and pkg2 both depend on the same version of react, and setting "hoistingLimits": "workspaces" will prevent react and it's transitive dependencies from hoisting beyond their workspaces, resulting in separate copies of these packages. An installs creates:

./node_modules
├── pkg1@workspace:pkg1
│   ├── [email protected]
│   ├── [email protected]
│   └── [email protected]
└── pkg2@workspace:pkg2
    ├── [email protected]
    ├── [email protected]
    └── [email protected]

"dependencies"

"dependencies" does the same as "workspaces" and additionally prevents transitive dependencies from hoisting beyond direct dependencies. This can be used to prevent phantom dependencies. With the same example with pkg1 and pkg2, an install with "hoistingLimits": "dependencies" would create:

./node_modules
├── pkg1@workspace:pkg1
│   └── [email protected]
│       ├── [email protected]
│       └── [email protected]
└── pkg2@workspace:pkg2
    └── [email protected]
        ├── [email protected]
        └── [email protected]

How did you verify your code works?

TODO:

  • root package nohoist patterns
  • matching packages in a workspace
  • match transitive dependency of a hoisted dependency
  • test adding/removing patterns from nohoist across multiple installs
  • match all packages
  • find out why webpack-cli and "nohoist": ["**"] hangs
  • text and binary lockfile serialize/deserialize nohoist array correctly

@robobun
Copy link

robobun commented Jun 1, 2025

Updated 1:11 PM PT - Jun 3rd, 2025

@dylan-conway, your commit 504b433 has 4 failures in Build #18047:


🧪   To try this PR locally:

bunx bun-pr 20124

That installs a local version of the PR into your bun-20124 executable, so you can run:

bun-20124 --bun

@dylan-conway dylan-conway changed the title add support for "workspaces.nohoist" add support for "workspaces.nohoist" and "workspaces.hoistingLimits" Jun 2, 2025
@dylan-conway dylan-conway marked this pull request as ready for review June 3, 2025 01:06
@Jarred-Sumner Jarred-Sumner merged commit 11070b8 into main Jun 4, 2025
57 of 60 checks passed
@Jarred-Sumner Jarred-Sumner deleted the dylan/nohoist branch June 4, 2025 06:44
Jarred-Sumner added a commit that referenced this pull request Jun 4, 2025
@dylan-conway dylan-conway restored the dylan/nohoist branch June 4, 2025 06:51
@dylan-conway
Copy link
Member Author

dylan-conway commented Jun 4, 2025

This pr was reverted and reopened here #20178

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.

Support for nohoist / multiple versions of the same package
3 participants