Skip to content

Propagate dependency features through build graph instead of relying on crate_features #155

Description

@sin-ack

Currently, the way features are enabled for the dependencies of a crate is to have them explicitly specified through crate_features. This is bad for multiple reasons:

  • Crates will be built with unnecessary features, especially if a rules_rs-equipped Bazel module is used as a dependency of another.
  • First-party crates must unconditionally enable all features that other crates depend on.
  • A Bazel-declared crate not usable when it is consumed as a Bazel module dependency unless it enables all its features ahead-of-time, which makes compilation times worse for everything that doesn't use those features. (In our case we have a crate that can optionally enable egui-related features gated behind an "egui" feature, but this increases compilation times significantly for when that crate is used for a CLI tool, for example, slowing down iteration speed).

I think a better approach is:

  1. During the lockfile resolution/repo generation phase, collect all features of a crate's dependencies into data.bzl.
  2. Add something like dependency_features to rust_library which can be populated with a function like all_crate_features().

The only downside I can see usability-wise is that this necessitates a workspace-hack crate similar to regular Cargo if one wants to avoid multiple copies of the same crate with different features. Another problem is how to encode the final artifact since multiple artifacts can be generated from the same rust_library target with different feature combinations.

Since rust_library etc. machinery seems to be taken directly from rules_rust this might not be a trivial change (and it breaks parity with upstream, even if we're using a fork), so I understand if it's not feasible to do this at this stage.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    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