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

draft: fix data and compile_data for rust_doc #1741

Merged
merged 10 commits into from
Jan 6, 2023

Conversation

paulfariello
Copy link
Contributor

Try to fix #1572

Main issue is that data_paths do not inherit from original crate when building rust_doc (and probably rust_test).
The main idea is to rely on crate_info which inherit correctly from original crate.

Issue with this draft is that compile_data is filed with File instead of Target when building 3rd party crate.

ERROR: <redacted>__base64-0.13.1/BUILD.bazel:34:13: in rust_library rule @<redacted>__base64-0.13.1//:base64:
Traceback (most recent call last):
	File "external/rules_rust/rust/private/rust.bzl", line 197, column 32, in _rust_library_impl
		return _rust_library_common(ctx, "rlib")
	File "external/rules_rust/rust/private/rust.bzl", line 292, column 32, in _rust_library_common
		return rustc_compile_action(
	File "external/rules_rust/rust/private/rustc.bzl", line 1093, column 46, in rustc_compile_action
		args, env_from_args = construct_arguments(
	File "external/rules_rust/rust/private/rustc.bzl", line 915, column 38, in construct_arguments
		expand_list_element_locations(
	File "external/rules_rust/rust/private/utils.bzl", line 311, column 53, in expand_list_element_locations
		return [_expand_location_for_build_script_runner(ctx, arg, data) for arg in args]
	File "external/rules_rust/rust/private/utils.bzl", line 255, column 30, in _expand_location_for_build_script_runner
		dedup_expand_location(ctx, env, data),
	File "external/rules_rust/rust/private/utils.bzl", line 230, column 31, in dedup_expand_location
		return ctx.expand_location(input, _deduplicate(targets))
Error in expand_location: at index 0 of targets, got element of type File, want Target

@google-cla
Copy link

google-cla bot commented Jan 2, 2023

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@paulfariello paulfariello force-pushed the fix/rust_doc_compile_data branch 2 times, most recently from d8571bb to b90473b Compare January 2, 2023 17:00
@illicitonion
Copy link
Collaborator

Thanks for putting together this fix!

It looks like the issue is that whenever we construct a CrateInfo (which we do several times in rust/private/rust.bzl, for instance), we convert compile_data from an attribute (targets) to files as required by the CrateInfo provider (by using ctx.files.compile_data instead of ctx.attr.compile_data).

I think to fix this we'd need to:

  1. Modify CrateInfo to have an additional field, compile_data_targets here:
    CrateInfo = provider(
    doc = "A provider containing general Crate information.",
    fields = {
    "aliases": "Dict[Label, String]: Renamed and aliased crates",
    "compile_data": "depset[File]: Compile data required by this crate.",
    "deps": "depset[DepVariantInfo]: This crate's (rust or cc) dependencies' providers.",
    "edition": "str: The edition of this crate.",
    "is_test": "bool: If the crate is being compiled in a test context",
    "metadata": "File: The rmeta file produced for this crate. It is optional.",
    "name": "str: The name of this crate.",
    "output": "File: The output File that will be produced, depends on crate type.",
    "owner": "Label: The label of the target that produced this CrateInfo",
    "proc_macro_deps": "depset[DepVariantInfo]: This crate's rust proc_macro dependencies' providers.",
    "root": "File: The source File entrypoint to this crate, eg. lib.rs",
    "rustc_env": "Dict[String, String]: Additional `\"key\": \"value\"` environment variables to set for rustc.",
    "rustc_env_files": "[File]: Files containing additional environment variables to set for rustc.",
    "srcs": "depset[File]: All source Files that are part of the crate.",
    "type": (
    "str: The type of this crate " +
    "(see [rustc --crate-type](https://doc.rust-lang.org/rustc/command-line-arguments.html#--crate-type-a-list-of-types-of-crates-for-the-compiler-to-emit))."
    ),
    "wrapped_crate_type": (
    "str, optional: The original crate type for targets generated using a previously defined " +
    "crate (typically tests using the `rust_test::crate` attribute)"
    ),
    },
    )
  2. Wherever we construct a CrateInfo, start also setting this field (to ctx.attr.compile_data rather than ctx.files.compile_data)
  3. In your change, consume compile_data_targets rather than compile_data.

Then I suspect your change will start working 🤞

@paulfariello paulfariello force-pushed the fix/rust_doc_compile_data branch 2 times, most recently from 4585922 to 7dd5c9f Compare January 4, 2023 10:16
Copy link
Collaborator

@illicitonion illicitonion left a comment

Choose a reason for hiding this comment

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

LGTM! Last up, can you add a test which was failing without this change, which shows rust_doc now works with compile_data?

Thanks!

proto/proto.bzl Outdated Show resolved Hide resolved
rust/private/rustc.bzl Outdated Show resolved Hide resolved
Paul Fariello added 4 commits January 6, 2023 10:57
Add compile_data_targets attribute to CrateInfo in order to inherit
properly in rust_doc and rust_test.
@paulfariello
Copy link
Contributor Author

For the record, the added test would fail on main branch at build time with the following error:

INFO: Invocation ID: 6aec52b5-1be1-471a-8513-af33247b3398
ERROR: /home/redacted/rules_rust/test/unit/compile_data/BUILD.bazel:3:24: in rust_doc rule //test/unit/compile_data:compile_data_env_rust_doc: label '//test/unit/compile_data:compile_data.txt' in $(location) expression is not a declared prerequisite of this rule
ERROR: /home/redacted/rules_rust/test/unit/compile_data/BUILD.bazel:3:24: in (an implicit dependency) attribute of rust_doc rule //test/unit/compile_data:compile_data_env_rust_doc: $(location) not defined. Since this rule was created by the macro 'compile_data_test_suite', the error might have been caused by the macro implementation
ERROR: /home/redacted/rules_rust/test/unit/compile_data/BUILD.bazel:3:24: Analysis of target '//test/unit/compile_data:compile_data_env_rust_doc' failed
ERROR: Analysis of target '//test/unit/compile_data:compile_data_env_rust_doc' failed; build aborted:
INFO: Elapsed time: 8.315s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (107 packages loaded, 1951 targets configured)
ERROR: Couldn't start the build. Unable to run tests

Copy link
Collaborator

@illicitonion illicitonion left a comment

Choose a reason for hiding this comment

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

LGTM - thanks for putting this all together! Two tiny suggestions and we're good to go!

test/unit/compile_data/compile_data_test.bzl Outdated Show resolved Hide resolved
rust/private/rustc.bzl Outdated Show resolved Hide resolved
@illicitonion illicitonion merged commit 9803d30 into bazelbuild:main Jan 6, 2023
@paulfariello paulfariello deleted the fix/rust_doc_compile_data branch January 6, 2023 13:03
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.

rust_doc_test fails for libraries with compile_data
2 participants