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
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/flatten.md
Original file line number Diff line number Diff line change
Expand Up @@ -1351,7 +1351,7 @@ A test rule for performing `rustfmt --check` on a set of targets

<pre>
CrateInfo(<a href="#CrateInfo-aliases">aliases</a>, <a href="#CrateInfo-compile_data">compile_data</a>, <a href="#CrateInfo-deps">deps</a>, <a href="#CrateInfo-edition">edition</a>, <a href="#CrateInfo-is_test">is_test</a>, <a href="#CrateInfo-name">name</a>, <a href="#CrateInfo-output">output</a>, <a href="#CrateInfo-owner">owner</a>, <a href="#CrateInfo-proc_macro_deps">proc_macro_deps</a>, <a href="#CrateInfo-root">root</a>,
<a href="#CrateInfo-rustc_env">rustc_env</a>, <a href="#CrateInfo-srcs">srcs</a>, <a href="#CrateInfo-type">type</a>, <a href="#CrateInfo-wrapped_crate_type">wrapped_crate_type</a>)
<a href="#CrateInfo-rustc_env">rustc_env</a>, <a href="#CrateInfo-rustc_env_files">rustc_env_files</a>, <a href="#CrateInfo-srcs">srcs</a>, <a href="#CrateInfo-type">type</a>, <a href="#CrateInfo-wrapped_crate_type">wrapped_crate_type</a>)
</pre>

A provider containing general Crate information.
Expand All @@ -1372,6 +1372,7 @@ A provider containing general Crate information.
| <a id="CrateInfo-proc_macro_deps"></a>proc_macro_deps | depset[DepVariantInfo]: This crate's rust proc_macro dependencies' providers. |
| <a id="CrateInfo-root"></a>root | File: The source File entrypoint to this crate, eg. lib.rs |
| <a id="CrateInfo-rustc_env"></a>rustc_env | Dict[String, String]: Additional <code>"key": "value"</code> environment variables to set for rustc. |
| <a id="CrateInfo-rustc_env_files"></a>rustc_env_files | [File]: Files containing additional environment variables to set for rustc. |
| <a id="CrateInfo-srcs"></a>srcs | depset[File]: All source Files that are part of the crate. |
| <a id="CrateInfo-type"></a>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)). |
| <a id="CrateInfo-wrapped_crate_type"></a>wrapped_crate_type | str, optional: The original crate type for targets generated using a previously defined crate (typically tests using the <code>rust_test::crate</code> attribute) |
Expand Down
3 changes: 2 additions & 1 deletion docs/providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<pre>
CrateInfo(<a href="#CrateInfo-aliases">aliases</a>, <a href="#CrateInfo-compile_data">compile_data</a>, <a href="#CrateInfo-deps">deps</a>, <a href="#CrateInfo-edition">edition</a>, <a href="#CrateInfo-is_test">is_test</a>, <a href="#CrateInfo-name">name</a>, <a href="#CrateInfo-output">output</a>, <a href="#CrateInfo-owner">owner</a>, <a href="#CrateInfo-proc_macro_deps">proc_macro_deps</a>, <a href="#CrateInfo-root">root</a>,
<a href="#CrateInfo-rustc_env">rustc_env</a>, <a href="#CrateInfo-srcs">srcs</a>, <a href="#CrateInfo-type">type</a>, <a href="#CrateInfo-wrapped_crate_type">wrapped_crate_type</a>)
<a href="#CrateInfo-rustc_env">rustc_env</a>, <a href="#CrateInfo-rustc_env_files">rustc_env_files</a>, <a href="#CrateInfo-srcs">srcs</a>, <a href="#CrateInfo-type">type</a>, <a href="#CrateInfo-wrapped_crate_type">wrapped_crate_type</a>)
</pre>

A provider containing general Crate information.
Expand All @@ -32,6 +32,7 @@ A provider containing general Crate information.
| <a id="CrateInfo-proc_macro_deps"></a>proc_macro_deps | depset[DepVariantInfo]: This crate's rust proc_macro dependencies' providers. |
| <a id="CrateInfo-root"></a>root | File: The source File entrypoint to this crate, eg. lib.rs |
| <a id="CrateInfo-rustc_env"></a>rustc_env | Dict[String, String]: Additional <code>"key": "value"</code> environment variables to set for rustc. |
| <a id="CrateInfo-rustc_env_files"></a>rustc_env_files | [File]: Files containing additional environment variables to set for rustc. |
| <a id="CrateInfo-srcs"></a>srcs | depset[File]: All source Files that are part of the crate. |
| <a id="CrateInfo-type"></a>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)). |
| <a id="CrateInfo-wrapped_crate_type"></a>wrapped_crate_type | str, optional: The original crate type for targets generated using a previously defined crate (typically tests using the <code>rust_test::crate</code> attribute) |
Expand Down
2 changes: 2 additions & 0 deletions rust/private/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def _create_crate_info(**kwargs):
"""
if not "wrapped_crate_type" in kwargs:
kwargs.update({"wrapped_crate_type": None})
if not "rustc_env_files" in kwargs:
kwargs.update({"rustc_env_files": []})
return CrateInfo(**kwargs)

rust_common = struct(
Expand Down
1 change: 1 addition & 0 deletions rust/private/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ CrateInfo = provider(
"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 " +
Expand Down
9 changes: 8 additions & 1 deletion rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ def _rust_library_common(ctx, crate_type):
output = rust_lib,
edition = get_edition(ctx.attr, toolchain, ctx.label),
rustc_env = ctx.attr.rustc_env,
rustc_env_files = ctx.files.rustc_env_files,
is_test = False,
compile_data = depset(ctx.files.compile_data),
owner = ctx.label,
Expand Down Expand Up @@ -378,6 +379,7 @@ def _rust_binary_impl(ctx):
output = output,
edition = get_edition(ctx.attr, toolchain, ctx.label),
rustc_env = ctx.attr.rustc_env,
rustc_env_files = ctx.files.rustc_env_files,
is_test = False,
compile_data = depset(ctx.files.compile_data),
owner = ctx.label,
Expand Down Expand Up @@ -422,6 +424,9 @@ def _rust_test_impl(ctx):
compile_data = depset(ctx.files.compile_data, transitive = [crate.compile_data])
else:
compile_data = depset(ctx.files.compile_data)
rustc_env_files = ctx.files.rustc_env_files + crate.rustc_env_files
rustc_env = dict(crate.rustc_env)
rustc_env.update(**ctx.attr.rustc_env)

# Build the test binary using the dependency's srcs.
crate_info = rust_common.create_crate_info(
Expand All @@ -434,7 +439,8 @@ def _rust_test_impl(ctx):
aliases = ctx.attr.aliases,
output = output,
edition = crate.edition,
rustc_env = ctx.attr.rustc_env,
rustc_env = rustc_env,
rustc_env_files = rustc_env_files,
is_test = True,
compile_data = compile_data,
wrapped_crate_type = crate.type,
Expand Down Expand Up @@ -465,6 +471,7 @@ def _rust_test_impl(ctx):
output = output,
edition = get_edition(ctx.attr, toolchain, ctx.label),
rustc_env = ctx.attr.rustc_env,
rustc_env_files = ctx.files.rustc_env_files,
is_test = True,
compile_data = depset(ctx.files.compile_data),
owner = ctx.label,
Expand Down
2 changes: 1 addition & 1 deletion rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ def collect_inputs(
],
)

build_env_files = getattr(files, "rustc_env_files", [])
build_env_files = crate_info.rustc_env_files
bsilver8192 marked this conversation as resolved.
Show resolved Hide resolved
compile_inputs, out_dir, build_env_file, build_flags_files = _process_build_scripts(build_info, dep_info, compile_inputs)
if build_env_file:
build_env_files = [f for f in build_env_files] + [build_env_file]
Expand Down
1 change: 1 addition & 0 deletions rust/private/rustdoc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def _strip_crate_info_output(crate_info):
output = None,
edition = crate_info.edition,
rustc_env = crate_info.rustc_env,
rustc_env_files = crate_info.rustc_env_files,
is_test = crate_info.is_test,
compile_data = crate_info.compile_data,
)
Expand Down
1 change: 1 addition & 0 deletions rust/private/rustdoc_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def _rust_doc_test_impl(ctx):
output = crate.output,
edition = crate.edition,
rustc_env = crate.rustc_env,
rustc_env_files = crate.rustc_env_files,
is_test = True,
compile_data = crate.compile_data,
wrapped_crate_type = crate.type,
Expand Down
35 changes: 34 additions & 1 deletion test/build_env/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ load(
"//cargo:cargo_build_script.bzl",
"cargo_build_script",
)
load("//rust:defs.bzl", "rust_test")
load("//rust:defs.bzl", "rust_library", "rust_test")

package(default_visibility = ["//visibility:public"])

Expand All @@ -13,6 +13,39 @@ rust_test(
edition = "2018",
)

rust_library(
name = "arbitrary_env_lib",
srcs = ["tests/arbitrary_env_lib.rs"],
edition = "2018",
rustc_env = {
"USER_DEFINED_KEY": "USER_DEFINED_VALUE",
},
)

rust_test(
name = "arbitrary_env_lib_test",
crate = ":arbitrary_env_lib",
edition = "2018",
)

rust_library(
name = "arbitrary_env_lib_in_test",
srcs = ["tests/arbitrary_env_lib.rs"],
edition = "2018",
rustc_env = {
"USER_DEFINED_KEY": "DIFFERENT_USER_DEFINED_VALUE",
},
)

rust_test(
name = "arbitrary_env_lib_test_in_test",
crate = ":arbitrary_env_lib_in_test",
edition = "2018",
rustc_env = {
"USER_DEFINED_KEY": "USER_DEFINED_VALUE",
},
)

rust_test(
name = "arbitrary_env_test",
srcs = ["tests/arbitrary_env.rs"],
Expand Down
16 changes: 16 additions & 0 deletions test/build_env/tests/arbitrary_env_lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
pub fn from_lib() -> &'static str {
env!("USER_DEFINED_KEY")
}

#[cfg(test)]
mod tests {
#[test]
fn verify_from_lib() {
assert_eq!(super::from_lib(), "USER_DEFINED_VALUE");
}

#[test]
fn verify_from_test() {
assert_eq!(env!("USER_DEFINED_KEY"), "USER_DEFINED_VALUE");
}
}
14 changes: 13 additions & 1 deletion test/rustc_env_files/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("//rust:defs.bzl", "rust_binary")
load("//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")

package(default_visibility = ["//visibility:public"])

Expand All @@ -26,3 +26,15 @@ sh_test(
args = ["$(rootpath :hello_env)"],
data = [":hello_env"],
)

rust_library(
name = "hello_env_crate",
srcs = ["src/lib.rs"],
edition = "2018",
rustc_env_files = [":generate_rustc_env_file"],
)

rust_test(
name = "hello_env_crate_test",
crate = ":hello_env_crate",
)
16 changes: 16 additions & 0 deletions test/rustc_env_files/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
pub fn from_lib() -> &'static str {
env!("GREETING")
}

#[cfg(test)]
mod tests {
#[test]
fn verify_from_lib() {
assert_eq!(super::from_lib(), "Howdy");
}

#[test]
fn verify_from_test() {
assert_eq!(env!("GREETING"), "Howdy");
}
}