Skip to content

Bun workspaces have no dependency edges #11628

Closed

Description

@DmitriyLewen

Description

The dependency graph Trivy builds from bun.lock has no edges between workspaces and their direct dependencies.
Workspace packages (Relationship: workspace) and direct dependencies (Relationship: direct) are found, but workspaces have an empty DependsOn.
As a result, JSON, SBOM and the dependency tree do not show which workspace pulls in a package, or through which chain a vulnerable package gets into the project.

This describes the behavior after #11071 (the lock file is generated by bun 1.4.2).

package.json:

{
  "name": "bun-ws-edges",
  "workspaces": ["packages/*"],
  "dependencies": {
    "lodash": "4.17.21"
  }
}

packages/a/package.json:

{
  "name": "a",
  "version": "1.0.0",
  "dependencies": {
    "b": "workspace:*",
    "ms": "2.0.0"
  },
  "devDependencies": {
    "minimist": "1.2.8"
  }
}

packages/b/package.json:

{
  "name": "b",
  "version": "1.0.0",
  "dependencies": {
    "lodash": "4.17.15"
  }
}
bun.lock (after bun install)
{
  "lockfileVersion": 2,
  "configVersion": 1,
  "workspaces": {
    "": {
      "name": "bun-ws-edges",
      "dependencies": {
        "lodash": "4.17.21",
      },
    },
    "packages/a": {
      "name": "a",
      "version": "1.0.0",
      "dependencies": {
        "b": "workspace:*",
        "ms": "2.0.0",
      },
      "devDependencies": {
        "minimist": "1.2.8",
      },
    },
    "packages/b": {
      "name": "b",
      "version": "1.0.0",
      "dependencies": {
        "lodash": "4.17.15",
      },
    },
  },
  "packages": {
    "a": ["a@workspace:packages/a"],

    "b": ["b@workspace:packages/b"],

    "lodash": ["lodash@4.17.21", "", {}, "sha512-..."],

    "minimist": ["minimist@1.2.8", "", {}, "sha512-..."],

    "ms": ["ms@2.0.0", "", {}, "sha512-..."],

    "b/lodash": ["lodash@4.17.15", "", {}, "sha512-..."],
  }
}

Packages in the output of trivy fs --include-dev-deps -f json . (fields ID, Relationship, Dev, DependsOn):

{"ID": "a@1.0.0", "Relationship": "workspace"}
{"ID": "b@1.0.0", "Relationship": "workspace"}
{"ID": "lodash@4.17.15", "Relationship": "direct"}
{"ID": "lodash@4.17.21", "Relationship": "direct"}
{"ID": "minimist@1.2.8", "Relationship": "direct", "Dev": true}
{"ID": "ms@2.0.0", "Relationship": "direct"}

No package has DependsOn: there is no a → b, no a → ms, and no b → lodash@4.17.15.

For comparison, the yarn analyzer builds the workspace → dependency edges (pkg/fanal/analyzer/language/nodejs/yarn/yarn.go).

Root cause

The parser (pkg/dependency/parser/nodejs/bun/parse.go) builds edges only from the metadata of an entry in packages (dependencies in the metadata object).
Since lockfileVersion: 1, a workspace entry in packages is just an identifier ("a": ["a@workspace:packages/a"]), without metadata.
bun writes the dependencies of a workspace only to the workspaces section, and the parser uses that section only to set Relationship: direct and the Dev flag.

Implementation challenges

1. The format depends on the lock file version

In lockfileVersion: 0 (bun 1.1.x), a workspace entry in packages has metadata with its dependencies, and the workspace → dependency edges do appear today:

"a": ["a@workspace:packages/a", { "dependencies": { ... }, "devDependencies": { ... } }],

A dependency on another workspace is written there as a path ("a": "packages/a") rather than "workspace:*".
Since lockfileVersion: 1, workspaces have no metadata.
As a result, the graph of the same project differs depending on the bun version that generated the lock file.

At the same time, version 0 is a legacy format: bun 1.4.2 still reads it, but rewrites the file as version 1 on the next bun install, and bun install --frozen-lockfile fails on it with lockfile had changes, but lockfile is frozen.
So it may not be worth adding dedicated handling for version 0: it may be enough that such lock files keep being parsed, while the workspace edges are built for version 1 and later.

2. Dev dependencies of workspaces

The Dev flag is computed by walking the graph from the direct prod dependencies (walkProdPackages), and workspaces are currently skipped by that walk.
Once a workspace has edges, including edges to its devDependencies, the dev dependencies of workspaces must not become prod (in the example above, minimist@1.2.8 must stay Dev: true).

3. Resolving workspace dependencies

A direct dependency of a workspace can be a nested copy (b/lodash for packages/b) or a top-level one (ms for packages/a).
The install path of a workspace comes from its <name>@workspace:<dir> entry.
A dependency on another workspace ("b": "workspace:*") points to a package with Relationship: workspace rather than a regular package.

4. Tests

The expected DependsOn in the existing parser tests (multiple workspaces, workspace with nested direct dependencies) currently has no workspace edges.
The fixtures are generated by bun 1.4.2; there is no workspace fixture for lockfileVersion: 0.

Related

Before implementation

Please describe the chosen approach in this issue and agree on it with the maintainers before opening a PR.

Activity

  1. changed the title [-]fix(nodejs): Bun workspaces have no dependency edges[/-] [+]Bun workspaces have no dependency edges[/+] on Nov 12, 2026
  2. drawliin commented on Nov 12, 2026

    @drawliin
    Contributor

    hi @DmitriyLewen.. i'd like to work on this. here is the approach i have in mind.. on top of #11071 since it already resolves workspace dependencies by install path.

    1. workspace edges.. for each entry in workspaces except the root, resolve its dependencies, optionalDependencies, peerDependencies and devDependencies with resolveDependency(wsPaths[wsDir], name, pkgs) and add them to deps[wsPaths[wsDir]].. deduplicated with any edges already built from metadata. the nearest copy rule gives an edge from b to b/lodash (4.17.15) and from a to ms.. and "b": "workspace:*" resolves to the workspace entry b. the root workspace has no entry in packages so it gets no edges.. same as today.
    2. dev flag.. walkProdPackages keeps starting only from the direct prod dependencies, which skip workspaces.. so the walk never goes through a workspace. a workspace's devDependencies get an edge but stay Dev: true unless a prod package also reaches them.. this matches the yarn analyzer (minimist@1.2.8 in your example).
    3. lockfileVersion: 0.. no dedicated handling. workspace entries there already carry metadata so their edges keep coming from it.. the edges from workspaces only merge with them.
    4. tests.. update the expected DependsOn in multiple workspaces and workspace with nested direct dependencies.. and add the lock file from this issue (bun 1.4.2) as a case that checks the edges from a to b, ms and minimist and from b to lodash@4.17.15.. plus that minimist stays dev.

    i'll wait for #11071 to be merged and start from main after that.. is it suitable for you?

  3. DmitriyLewen commented on Nov 13, 2026

    @DmitriyLewen
    ContributorAuthor

    Hello @drawliin thanks, the approach looks good, assigned issue to you.
    Feel free to pick it up once #11071 is merged.

    A couple of notes on the tests:

    • No new fixture is needed: the existing ones already cover all cases from the example.
      bun_workspace_nested.lock has a workspace depending on another workspace (b → a), nested copies (a/lodash etc.) and a dev dependency of a workspace (minimist@0.0.10 in b), and bun_multiple_ws.lock has a workspace dependency resolved from the top level (my-app → lodash).
      It's enough to update the expected DependsOn and check that minimist@0.0.10 stays Dev: true.
    • multiple workspaces currently doesn't check DependsOn at all: multipleWsDeps is nil, and the test compares the dependencies only when wantDeps != nil.
      Please set the expected edges there (my-app → lodash, my-lib → chalk), otherwise the new edges aren't checked by this case.

    Regards, Dmitriy

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions