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

Propagate rustc_env{,_files} from rust_test.crate #1443

Merged
merged 8 commits into from
Aug 10, 2022

Conversation

bsilver8192
Copy link
Contributor

rust_test.crate builds the same file as the rust_library it's pointing
to, which will almost certainly depend on the same environment variables
being set.

rust_test.crate builds the same file as the rust_library it's pointing
to, which will almost certainly depend on the same environment variables
being set.
Copy link
Collaborator

@UebelAndre UebelAndre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this seems good. Wondering if @scentini could provide a second option 😄

@@ -590,7 +590,7 @@ def collect_inputs(
],
)

build_env_files = getattr(files, "rustc_env_files", [])
build_env_files = getattr(files, "rustc_env_files", []) + crate_info.rustc_env_files
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think now we only need to look into the rustc_env_files attribute for backwards compatibility reasons - custom rules may create crate_infos that currently do not set crate_info.rustc_env_files.
Could we add a condition to only inspect the attribute when crate_info.rustc_env_files is empty? This way we won't be passing each rustc_env_file twice.

Ideally we would also add a incompatible_get_rustc_env_files_from_crate_info flag that we could flip in the future, but I think it's fine for that to be a separate PR with (potentially) a different author.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this the only place that reads the rustc_env_files from all the rules? And I think having them both is useful if a rust_test wants to add a file in addition to the ones inherited from its crate.

For backwards compatibility, I updated create_crate_info. Do you think that's sufficient, or should I change this to getattr(crate_info, "rustc_env_files", []) to handle custom rules which don't use create_crate_info?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I read the code correctly, rust_test would have put its file into its crate_info so we'd get the same file twice, once from the getattr(files, "rustc_env_files") and once from the crate_info. That's why I suggest that we only inspect rustc_env_files when crate_info.rustc_env_files is empty.

Ideally we would remove the getattr here altogether, but that is a backwards incompatible change as a custom non-OSS rule can currently rely on collect_inputs to access the attribute. In the future we can introduce an incompatible flag and remove it completely after we've given the users some time to adapt their code.

For backwards compatibility, I updated create_crate_info. Do you think that's sufficient, or should I change this to getattr(crate_info, "rustc_env_files", []) to handle custom rules which don't use create_crate_info?

It is sufficient. create_crate_info exists for the exact purpose of enabling us to do changes like this one in a backwards compatible way. I consider direct calls to CrateInfo() unsupported. We should also have create_*_info for the rest of our providers, but that's orthogonal to this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I read the code correctly, rust_test would have put its file into its crate_info so we'd get the same file twice, once from the getattr(files, "rustc_env_files") and once from the crate_info. That's why I suggest that we only inspect rustc_env_files when crate_info.rustc_env_files is empty.

I don't think that's how it works. With this:

rust_library(
    name = "lib",
    rustc_env_files = ["a"],
    ...
)

rust_test(
    name = "lib_test",
    crate = "lib",
    ...
)

Then getattr(files, "rustc_env_files") is [File("a")] for lib and [] for lib_test. There's no duplicate file.

With this:

rust_library(
    name = "lib",
    rustc_env_files = ["a"],
    ...
)

rust_test(
    name = "lib_test",
    crate = "lib",
    rustc_env_files = ["b"],
    ...
)

Then getattr(files, "rustc_env_files") is [File("a")] for lib and [File("b")] for lib_test. I don't see why it makes sense to ignore b in this case.

The only way I see to get duplicates is with rustc_env_files on separate rules that include the same file(s), which seems like a silly thing to do in the first place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any more thoughts @scentini ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delayed response. I think you'll understand the point I'm trying to make the easiest if you print(build_env_files) below this line. Running bazel test test/build_env:arbitrary_env_lib_test prints the following for me: [<generated file test/rustc_env_files/rustc_env_file>, <generated file test/rustc_env_files/rustc_env_file>]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I was confusing this code with the line in _rust_test_impl that adds them. Done

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I left a suggestion to still consider the rustc_env_files attribute in the case where crate_info.rustc_env_files is not populated. This is so that we keep backwards compatibility. While you have thoroughly populated crate_info.rustc_env_files in the rules_rust area, there can be Starlark rule writers out there that invoke our rustc_compile_action() and that rely on the rustc_env_files attribute being inspected.

frc971-automation pushed a commit to frc971/971-Robot-Code that referenced this pull request Jul 26, 2022
Backport of bazelbuild/rules_rust#1443.

Change-Id: Ic06f3bbf6dc3278071d44a98aa569a3b8719ad26
Signed-off-by: Brian Silverman <bsilver16384@gmail.com>
@scentini scentini self-requested a review August 1, 2022 08:28
rust/private/rustc.bzl Outdated Show resolved Hide resolved
@@ -590,7 +590,7 @@ def collect_inputs(
],
)

build_env_files = getattr(files, "rustc_env_files", [])
build_env_files = getattr(files, "rustc_env_files", []) + crate_info.rustc_env_files
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I left a suggestion to still consider the rustc_env_files attribute in the case where crate_info.rustc_env_files is not populated. This is so that we keep backwards compatibility. While you have thoroughly populated crate_info.rustc_env_files in the rules_rust area, there can be Starlark rule writers out there that invoke our rustc_compile_action() and that rely on the rustc_env_files attribute being inspected.

@scentini
Copy link
Collaborator

Thanks!

@scentini scentini merged commit 6ee7c80 into bazelbuild:main Aug 10, 2022
@bsilver8192 bsilver8192 deleted the rustc_env_crate branch August 10, 2022 20:37
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.

None yet

3 participants