Skip to content

Add crate_package_default_visibility to render_config#4003

Open
tamasvajk wants to merge 2 commits intobazelbuild:mainfrom
tamasvajk:crate-package-visibility
Open

Add crate_package_default_visibility to render_config#4003
tamasvajk wants to merge 2 commits intobazelbuild:mainfrom
tamasvajk:crate-package-visibility

Conversation

@tamasvajk
Copy link
Copy Markdown
Contributor

Summary

Add crate_package_default_visibility option to render_config for controlling the visibility of individual crate BUILD packages generated by crate_universe.

Problem

All generated crate BUILD packages use //visibility:public unconditionally. In monorepos, this allows any target to directly depend on versioned crate targets (e.g. @crates__tokio-1.2.3//:tokio) instead of going through the top-level aliases (e.g. @crates//:tokio). Direct references to versioned targets are fragile and bypass any dependency governance the aliases provide.

Fix

Add an optional crate_package_default_visibility parameter to render_config(). When set, generated crate BUILD packages use the specified visibility instead of //visibility:public. The top-level aliases BUILD is unaffected and remains public.

Example usage:

render_config(
    crate_package_default_visibility = ["//third_party/rust:__subpackages__"],
)

Changes:

  • crate_universe/private/generate_utils.bzl: add parameter and pass through to JSON config
  • crate_universe/src/config.rs: add crate_package_default_visibility field to RenderConfig
  • crate_universe/src/rendering.rs: use crate_package helper method that respects the config

Note: This PR was largely AI-generated using Claude Code, with human review and guidance throughout.

Allow configuring the default_visibility of individual crate BUILD
packages generated by crate_universe. By default, crate packages use
//visibility:public. Setting crate_package_default_visibility restricts
direct references to crate targets, encouraging consumers to go through
top-level aliases instead.

This is useful in monorepos where visibility control helps enforce
dependency hygiene.

Example usage in render_config():
  render_config(
      crate_package_default_visibility = ["//third_party/rust:__subpackages__"],
  )
@tamasvajk tamasvajk force-pushed the crate-package-visibility branch 3 times, most recently from 8161340 to 8f02225 Compare April 28, 2026 16:24
Two tests in rendering.rs:
- render_crate_package_default_visibility: verifies custom visibility
  appears in the generated BUILD file instead of //visibility:public
- render_default_visibility_when_not_configured: verifies the default
  behavior (//visibility:public) when the option is not set

Use skip_serializing_if on the new field to preserve lockfile digest
compatibility when the option is not set (None).
@tamasvajk tamasvajk force-pushed the crate-package-visibility branch from 8f02225 to ab9eb40 Compare April 28, 2026 16:53
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.

1 participant