Skip to content

Feature Request: link_deps attribute for native library linkage #4023

@Suyashagarw

Description

@Suyashagarw

Description

Currently, the deps attribute in rules_rust is overloaded: it accepts both CrateInfo (Rust crates) and CcInfo (C++ libraries).

This overloading creates ambiguity in the dependency model. Specifically, it makes it difficult to link a dependency purely for its native symbols (e.g., satisfying manual FFI via extern "C") without also exposing it as a formal Rust module to the compiler. In large build graphs, this can lead to unnecessary namespace pollution and "leaky" dependency abstractions where internal linkage requirements are exposed as public API dependencies.

Proposed Architectural Change

I propose the introduction of a dedicated link_deps attribute to the core Rust rules (rust_library, rust_binary, rust_test).

Key Goals:

  1. Explicit intent: Create a clear distinction between deps (contributing to the Rust module hierarchy) and link_deps (contributing strictly to the linker command line).
  2. Private Linkage Support: Allow link_deps to accept both C++ and Rust libraries, but only extract the linkage context (CcInfo). This enables "private" linkage of Rust libraries where binary symbols are satisfied but the crate metadata is hidden from the consumer.
  3. Non-Breaking addition: Maintain support for C++ libraries in the existing deps attribute while proactively notifying users (e.g., via analysis-phase warnings) to encourage migration to the more explicit link_deps without breaking current build workflows.

Justification

This feature will eliminate the "silent failure" or cryptic error messages that occur when the wrong type of library is placed in deps. For example, currently, if a cc_library is incorrectly placed in deps, the Bazel analysis phase succeeds, but the build later fails during compilation with a confusing can't find crate for... error from rustc. By separating these concerns, we can provide proactive feedback during the analysis phase.

Impact on Existing Rules

  • rust_library, rust_binary, rust_test: Gain the link_deps attribute.
  • rust_proc_macro: Intentionally remains focused on host-platform requirements to avoid host-vs-target linkage complexity.
  • Backward Compatibility: Zero impact on existing builds.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    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