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

Refactor CrateInfo construction #2161

Merged

Conversation

daivinhtran
Copy link
Contributor

@daivinhtran daivinhtran commented Sep 15, 2023

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 fix rust_library rule. The follow-up PR will address the remaining rules and aspects.

Summary

  • Create create_crate_info_dict function in rust/private/utils.bzl to create a mutable dict repsenting CrateInfo
  • Move _determine_lib_name, get_edition, _transform_sources functions to rust/private/utils.bzl to avoid cyclic dependency when create_crate_info_dict function use these
  • Introduce optional create_crate_info_callback attribute to rustc_compile_action to allow creating CrateInfo inside rustc_compile_action. This optional attribute allows scoping the refactoring in this PR to just rust_library rule.
  • Introduce optional skip_expanding_rustc_env attribute to rustc_compile_action to skip expanding rustc_env attr. This is useful for clippy aspect and rust_test rule because the CrateInfo provided from the depended rust_library already expands all the env vars before returning the provider downstream.

Notes

  • rustc_env_attr is a temporary field in CrateInfo and needed by the rules and aspects not migrated to create CrateInfo in rustc_compile_action yet. It will be remove in the next PR after CrateInfo.rustc_env is always expanded.
  • create_crate_info_callback will be removed from rustc_compile_action after all CrateInfos are created inside rustc_compile_action.

@daivinhtran daivinhtran force-pushed the refactor-crate_info-construction branch from 7465ff7 to 6d5f62a Compare September 18, 2023 01:44
rust/private/rustc.bzl Outdated Show resolved Hide resolved
if getattr(attr, "edition"):
return attr.edition
elif not toolchain.default_edition:
fail("Attribute `edition` is required for {}.".format(label))
Copy link
Contributor

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?

Copy link
Contributor Author

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.

"""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
Copy link
Contributor

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?

Copy link
Contributor Author

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.

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
Copy link
Contributor

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?

Copy link
Contributor Author

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.

@daivinhtran daivinhtran marked this pull request as draft September 18, 2023 04:16
@daivinhtran
Copy link
Contributor Author

daivinhtran commented Sep 18, 2023

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 pwd. I'll make sure to address your comments.

@daivinhtran daivinhtran force-pushed the refactor-crate_info-construction branch 2 times, most recently from 9db35ca to c07f822 Compare September 22, 2023 15:20
@daivinhtran daivinhtran force-pushed the refactor-crate_info-construction branch from c07f822 to b07102f Compare September 22, 2023 15:24
@daivinhtran daivinhtran force-pushed the refactor-crate_info-construction branch 11 times, most recently from 4fd87dd to 0792154 Compare September 25, 2023 19:54
@daivinhtran daivinhtran force-pushed the refactor-crate_info-construction branch from 0792154 to 1d6dbe4 Compare September 25, 2023 20:17
@daivinhtran daivinhtran force-pushed the refactor-crate_info-construction branch 3 times, most recently from 790efa4 to ab05446 Compare September 26, 2023 19:09
@daivinhtran daivinhtran force-pushed the refactor-crate_info-construction branch from ab05446 to e138ec9 Compare September 26, 2023 19:12
@daivinhtran daivinhtran marked this pull request as ready for review September 26, 2023 20:15
@scentini scentini self-requested a review September 29, 2023 16:16
Copy link
Collaborator

@scentini scentini left a comment

Choose a reason for hiding this comment

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

Thanks a lot!!

@scentini scentini merged commit 8b548d2 into bazelbuild:main Oct 6, 2023
3 checks passed
@daivinhtran daivinhtran deleted the refactor-crate_info-construction branch October 9, 2023 14:20
@daivinhtran
Copy link
Contributor Author

@scentini #2188 is the follow-up PR. PTAL!

scentini added a commit that referenced this pull request Oct 17, 2023
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]>
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.

3 participants