Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for rule visibility for private tools/dependencies. #7377

Closed
sergiocampama opened this issue Feb 7, 2019 · 15 comments
Closed

Add support for rule visibility for private tools/dependencies. #7377

sergiocampama opened this issue Feb 7, 2019 · 15 comments
Labels
P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee) stale Issues or PRs that are stale (no activity for 30 days) team-Loading-API BUILD file and macro processing: labels, package(), visibility, glob type: feature request

Comments

@sergiocampama
Copy link
Contributor

Description of the problem / feature request:

Currently, if a rule has a private tool that it needs to invoke as part of its logic, this tool needs to have public visibility since visibility resolution is per target, not per rule.

Bazel should support a mechanism to allow any target of a specific rule to have access to the private tools/dependencies, but not having to expose these dependencies publicly.

Take for example: https://github.com/bazelbuild/rules_apple/blob/master/tools/codesigningtool/BUILD#L9

There's no available mechanism through which to make this target private to instances of ios_application, since this rule can be created in any package and maintaining a whitelist is a huge burden.

Feature requests: what underlying problem are you trying to solve with this feature?

Lack of visibility enforcement and needing to expose all internals of rules into potential abuse, making it harder to control affected targets and therefore make improvements/changes.

@laurentlb laurentlb added type: feature request P3 We're not considering working on this, but happy to review a PR. (No assignee) and removed untriaged labels Feb 18, 2019
@aiuto
Copy link
Contributor

aiuto commented Feb 28, 2019

Is this really Starlark? I think the bulk of the work is analysis.

@aiuto
Copy link
Contributor

aiuto commented Apr 25, 2019

I also think we should raise this above P3. Without the mechanism, it is impossible to build a pair of (rule, helper tool) where the implementation of the helper is truly private.

@laurentlb
Copy link
Contributor

Starlark team has no ressources to work on it this year. Feel free to reassign.

@haberman
Copy link
Contributor

Same issue happens with protobuf rules. The protobuf C++ runtime has APIs and .h files that are only meant for use by the generated code. We would like to visibility restrict these so that nobody can #include them from hand-written code. But currently we have to make them public to everybody.

I would like to write visibility restrictions that say that only the implementation of cc_proto_library() can depend on the cc_library(name="runtime_internal") target.

@laurentlb
Copy link
Contributor

Design: https://github.com/bazelbuild/proposals/blob/master/designs/2019-10-15-tool-visibility.md

This can be tested with the flag --incompatible_visibility_private_attributes_at_definition.

@haberman
Copy link
Contributor

haberman commented May 5, 2020

This looks great. A couple comments/questions:

  1. The design mentions "tools" throughout, but for protobuf it is a cc_library() implicit dep that we want to restrict. Our runtime library is used by the generated code, and it has some interfaces that are meant only for the generated code. Will this design work for cc_library() deps, as well as tools?

  2. I know of at least one rule at Google that is attempting to simulate rule visibility, and will be hit by Private attribute as a replacement for rule visibility. Is any mitigation for this planned?

@aiuto
Copy link
Contributor

aiuto commented May 5, 2020

What I would like to see is a document which gathers some use cases so that we can test solutions against it. Some things which come to my mind are

  1. rule with private tool
  • rule depends on helper tool.
  • helper tool grants visibility to the __pkg__ containing the rule
  • this is essentially the tool-visibility proposal
  1. macro with private rule
  • we have a macro which coordinates one or more rules
  • the user API is the macro, not the individual rules
  1. Rule use limited to specific packages
  • This is actually easy. For rule foo() we add a private attribute _foo_lock of type label, which points to a zero sized file foo_visibility_lock. Use visibility on the exports_files of that to lock it down.

Are there others?
@haberman Can I try to paraphrase what you stated above?

  • a macro
    • calls rule which generates some code
    • calls rule (cc_library) that takes generated code and //protobuf:special_hdrs as input
    • You only want //protobuf:special headers to be visible when called this way

Does that get the general sense of what you want?

@haberman
Copy link
Contributor

haberman commented May 5, 2020

@aiuto that is close, but I am talking about rules like cc_proto_library(), upb_proto_library(), etc, not macros.

Right now in my aspect-based rules, I have to resort to obnoxious long names as a pseudo-visibility: https://github.com/protocolbuffers/upb/blob/22182e6e54e892f93f26d0522487997d30f604af/bazel/upb_proto_library.bzl#L235

@laurentlb
Copy link
Contributor

Private attribute as a replacement for rule visibility. Is any mitigation for this planned?

That's the reason why the flag is not enabled by default. There's no planned rollout at this time.

@haberman
Copy link
Contributor

haberman commented May 6, 2020

That's the reason why the flag is not enabled by default. There's no planned rollout at this time.

Can we let rules specify this behavior in the label itself, so it doesn't need to be a global flag flip?

Right now I have:

_upb_proto_library_aspect = aspect(
    attrs = {
        # [...]
        "_upb": attr.label_list(default = [
            "//:generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me",
            "//:upb",
        ]),
    },
    # ...
)

Could I instead write:

_upb_proto_library_aspect = aspect(
    attrs = {
        # [...]
        "_upb": attr.label_list(
            default = [
                "//:generated_code_support",
               "//:upb",
            ]
            # With this flag, the visibility check will pass if these
            # targets are visible from where this aspect is defined.
            inherit_visibility = True,
        ),
    },
    # ...
)

bazel-io pushed a commit that referenced this issue May 13, 2020
Fixes #8982
Documents #7377

RELNOTES: None.
PiperOrigin-RevId: 311376318
@haberman
Copy link
Contributor

Ping @laurentlb ? I don't see why this needs to be a global flag. If it can be an attribute on label_list, we can get the best of both worlds.

@brandjon brandjon added P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee) team-Build-Language and removed P3 We're not considering working on this, but happy to review a PR. (No assignee) team-Starlark labels Feb 19, 2021
@aiuto
Copy link
Contributor

aiuto commented Oct 19, 2022

@brandjon
Does the starlark visibility stuff close this?

@brandjon
Copy link
Member

No, this is a separate matter of target visibility. Still on my radar but not this release cycle.

@brandjon brandjon added team-Loading-API BUILD file and macro processing: labels, package(), visibility, glob and removed team-Build-Language labels Nov 4, 2022
Copy link

github-actions bot commented Jan 9, 2024

Thank you for contributing to the Bazel repository! This issue has been marked as stale since it has not had any activity in the last 1+ years. It will be closed in the next 90 days unless any other activity occurs or one of the following labels is added: "not stale", "awaiting-bazeler". Please reach out to the triage team (@bazelbuild/triage) if you think this issue is still relevant or you are interested in getting the issue resolved.

@github-actions github-actions bot added the stale Issues or PRs that are stale (no activity for 30 days) label Jan 9, 2024
@fmeum
Copy link
Collaborator

fmeum commented Jan 9, 2024

This has been fixed in Bazel 7: #19330

@fmeum fmeum closed this as completed Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee) stale Issues or PRs that are stale (no activity for 30 days) team-Loading-API BUILD file and macro processing: labels, package(), visibility, glob type: feature request
Projects
None yet
Development

No branches or pull requests

7 participants