-
Notifications
You must be signed in to change notification settings - Fork 402
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
Enhance label.rs to support implicit targets and expose absolute-ness. #1046
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution! The implicit target stuff, and general rework, looks great, but I'm not so sure about exposing the absolute-ness of the label - what's your use-case for consuming it?
It feels like in general important questions are things like "Are these in the same package" rather than "Was this written absolutely in a BUILD file", so I'm curious as to how you're looking to use the API
The use case is to allow callers to distinguish labels like It would be nice to be able to distinguish these cases if:
My use case is the former (parsing labels in Rust source code where I want to disallow relative labels); this is related to #1008. Specifically, I'd like to use this parsing library in the proc_macro implementation (assuming I contribute the macro upstream, too), instead of writing my own parser. |
Come to think of it, it's not even possible to answer the question "Are these in the same package" with the current implementation. It's possible to have two Exposing the |
Aha, that's an interesting corner! I was generally thinking about BUILD files, where relative labels aren't allowed at all; things either need to be listed as absolute labels ( How would you feel about adding a "context" param (i.e. the directory from which the label is being resolved, relative to the workspace root)? That way we can always normalise labels to be canonically represented, and get rid of that ambiguity completely. |
Let me caveat my answer by saying I'm not a Bazel expert :) but: that would work for the use case of analyzing a BUILD file (or more generally, analyzing something when you know where you are in the repo), but I worry that it wouldn't help my use case. I want to analyze/rewrite labels in Rust source via a proc_macro, to turn something like: import! {
"//some_project:utils";
"//some_project:ugly_name" as something_else;
"//third_party/rust/serde/v1:serde";
"//third_party/rust/clap/v2:clap" as aliased_clap;
} into extern crate some_project_COLON_utils as utils;
extern crate some_project_COLON_ugly_name as something_else;
extern crate serde;
extern crate clap as aliased_clap; In order to provide the "context" to that macro (to resolve relative labels), I'd need to know the Bazel package of the file that invoked the macro. There's a nightly API that would let me grab the source file of the caller, but I'd still need to know what package that file is in, which (to my knowledge) wouldn't be possible without doing a On top of that complication, I don't even want to resolve relative labels in this macro invocation; I want to prohibit them entirely. (Requiring labels to be absolute makes large scale changes, e.g. moving a widely-depended-upon crate, more straightforward: I can just do a simple find/replace of the absolute label, instead of worrying about all the ways it could be specified relatively.) So even if I could easily provide the context, it still wouldn't give me a way to do what I want. Maybe an idea is to have the parser accept an optional context? That way, the parser can either resolve relative labels unambiguously (if context is |
That sounds good to me (or even having two public entry-points, one which requires a context and supports relative, and the other which does neither. But - it sounds like we'd both be happy simply rejecting labels which don't either take one of the following forms:
I don't really see a use for supporting |
Yup, I'd be happy with that - then I could just check that everything had a non- Actually, according to https://docs.bazel.build/versions/4.2.1/build-ref.html#labels, |
I agree with all you said, and I like the PR, thank you @cfredric. Regarding the relative labels in the form @illicitonion please feel free to merge this PR in after reviewing/approving. Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM - thanks!
This PR enhances the label parsing library (label.rs) to allow it to support implicit targets (e.g.
//foo/bar
has an implicit target ofbar
, and is equivalent to//foo/bar:bar
), and to expose whether the label that was parsed was absolute (e.g. to distinguish//foo
andfoo
).I also reworked some of the implementation of
consume_package_name
, to make it a little more idiomatic and easier to follow (IMO).