Skip to content

[Bug]: "Cannot find module" error for dependency that is a peerDep of a first-party package #1092

@etlovett

Description

@etlovett

What happened?

We have two rules_js-based libraries; let's call them leaf and consumer. consumer depends on leaf, and each have a set of third-party dependencies. Applications might choose to depend on either or both. Our expectation is that we should be able to do the following:

// leaf/package.json
{
  "peerDependencies": {
    "third-party-dep-1": "whatever version",
    "third-party-dep-2": "whatever version",
  },
  "devDependencies": {
    "third-party-dep-1": "whatever version",
    "third-party-dep-2": "whatever version",
  }
}

// consumer/package.json
{
  "peerDependencies": {
    "@first-party/leaf": "workspace:*",
    "third-party-dep-1": "whatever version",
    "third-party-dep-2": "whatever version",
  },
  "devDependencies": {
    "@first-party/leaf": "workspace:*",
    "third-party-dep-1": "whatever version",
    "third-party-dep-2": "whatever version",
  }
}

// some-app/package.json
{
  "dependencies": {
    "@first-party/consumer": "workspace:*",
    "@first-party/leaf": "workspace:*", // optional
    "third-party-dep-1": "whatever version",
    "third-party-dep-2": "whatever version",
  }
}

I’m trying to set up just the two libraries right now, using essentially the package.json files above. However, when I do all the pnpm installs and then run the Jest tests in consumer (bazel test //path/to/consumer/src:test), I get the following two errors in the output:

Cannot find module 'third-party-dep-1' from '../../../../node_modules/.aspect_rules_js/@first-party+leaf@0.0.0/node_modules/@first-party/leaf/src/some-file.js'
Cannot find module 'third-party-dep-2' from '../../../../../../../../../../../../../../../../../../execroot/rh/bazel-out/darwin_arm64-fastbuild/bin/bazel/js/jest/config/src/jest-env.mjs'

Changing all of consumer’s peerDeps/devDeps to (regular) deps doesn’t fix anything. Changing all of leaf’s peerDeps to (regular) deps does fix it, since then it can find its own dependencies, but this doesn’t seem to us like the right philosophical approach for a monorepo.

It also seems odd that utils has itself in its own node_modules, and we’re not sure why that would be.

Are the first-party package's peerDeps not getting properly passed along the Bazel graph?

Version

Development (host) and target OS/architectures: Running on macOS

Output of bazel --version:bazel 6.2.0

Version of the Aspect rules, or other relevant rules from your WORKSPACE or MODULE.bazel file: We're running off commit 8dc6c8be10a44900408c26935b6d7182253a0b2f.

Language(s) and/or frameworks involved: TS and Jest

How to reproduce

No response

Any other information?

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingneed: discussionWe have to do some design work in a meeting

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions