-
Notifications
You must be signed in to change notification settings - Fork 442
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
Refactor CrateInfo construction #2161
Refactor CrateInfo construction #2161
Conversation
7465ff7
to
6d5f62a
Compare
rust/private/rustc.bzl
Outdated
if getattr(attr, "edition"): | ||
return attr.edition | ||
elif not toolchain.default_edition: | ||
fail("Attribute `edition` is required for {}.".format(label)) |
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.
Preexisting, but the first time I hit this error it wasn't obvious that you could set a default edition in the toolchain, might be worth a mention in this error?
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.
I'm leaving your comments unresolved in this PR and will address in a separate PR because they're not within scope.
rust/private/rustc.bzl
Outdated
"""Creates symlinks of the source files if needed. | ||
|
||
Rustc assumes that the source files are located next to the crate root. | ||
In case of a mix between generated and non-generated source files, this |
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.
I think we want to snip "this" at the end of this line?
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.
I'm leaving your comments unresolved in this PR and will address in a separate PR because they're not within scope.
rust/private/rustc.bzl
Outdated
Returns: | ||
Tuple(List[File], File): The transformed srcs and crate_root | ||
""" | ||
has_generated_sources = len([src for src in srcs if not src.is_source]) > 0 |
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.
nit: any([src for src in srcs if not src.is_source])
is shorter and you can bail out a little quicker?
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.
I'm leaving your comments unresolved in this PR and will address in a separate PR because they're not within scope.
Hi @dzbarsky . Thanks for the review! It's still WIP and I just marked the PR as draft now while I'm working to fix the issue with |
9db35ca
to
c07f822
Compare
c07f822
to
b07102f
Compare
4fd87dd
to
0792154
Compare
0792154
to
1d6dbe4
Compare
790efa4
to
ab05446
Compare
ab05446
to
e138ec9
Compare
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 a lot!!
In #2161, I anticipated to make [create_crate_info_dict](https://github.com/bazelbuild/rules_rust/pull/2161/files#diff-b75adb75969318e09670c45b8eea5aaf147fdc1d2cf229a72a17bd39edf49163R853) reusable across all rules and aspects. The logic to construct `crate_info` is significantly different among the rules and aspects. As I tried to to expand the arguments to make `create_crate_info_dict` universally reusable, the arguments grow which make it not worth anymore. This PR changes the refactoring approach. Before (result from #2161) ``` def _rust_library_common() return rustc_compile_action( # the callback allows us to slowly refactor one rule or aspect at a time. create_crate_info_callback = create_crate_info_dict, ) def rustc_compile_action(create_crate_info_callback) if create_crate_info_callback: crate_info_dict = create_crate_info_callback() ``` After (result from this PR) ``` def _rust_library_common() # create crate_info_dict return rustc_compile_action( crate_info_dict = crate_info_dict, ) def rustc_compile_action(crate_info_dict) # crate_info_dict always exists ``` Instead of creating `crate_info_dict` at the beginning of `rustc_compile_action`, this PR lifted the creation logic to the rules and aspects completely so that they can add any custom logic without caring about the `rustc_compile_action`'s implementation. --------- Co-authored-by: scentini <[email protected]>
Description
This PR addresses #2013 to ensure rust-analyzer can access all the env vars the rust rules pass to
rustc
at compile time. This is a large refactoring in almost all the rules and aspects so I aim this PR to just fixrust_library
rule. The follow-up PR will address the remaining rules and aspects.Summary
create_crate_info_dict
function inrust/private/utils.bzl
to create a mutable dict repsenting CrateInfo_determine_lib_name
,get_edition
,_transform_sources
functions torust/private/utils.bzl
to avoid cyclic dependency whencreate_crate_info_dict
function use thesecreate_crate_info_callback
attribute torustc_compile_action
to allow creatingCrateInfo
insiderustc_compile_action
. This optional attribute allows scoping the refactoring in this PR to justrust_library
rule.skip_expanding_rustc_env
attribute torustc_compile_action
to skip expandingrustc_env
attr. This is useful forclippy
aspect andrust_test
rule because theCrateInfo
provided from the dependedrust_library
already expands all the env vars before returning the provider downstream.Notes
rustc_env_attr
is a temporary field inCrateInfo
and needed by the rules and aspects not migrated to createCrateInfo
inrustc_compile_action
yet. It will be remove in the next PR afterCrateInfo.rustc_env
is always expanded.create_crate_info_callback
will be removed fromrustc_compile_action
after allCrateInfo
s are created insiderustc_compile_action
.