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

Replace aspect_repository with rustc_env for exposing workspace_name #1410

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions rust/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("//rust/private:rust_analyzer.bzl", "rust_analyzer_detect_sysroot")
load("//rust/private:rustfmt.bzl", "rustfmt_workspace_name")
load("//rust/private:stamp.bzl", "stamp_build_setting")
load("//rust/private:utils.bzl", "workspace_name")

bzl_library(
name = "bzl_lib",
Expand All @@ -26,7 +26,7 @@ rust_analyzer_detect_sysroot(
visibility = ["//visibility:public"],
)

rustfmt_workspace_name(
name = "rustfmt_workspace_name",
workspace_name(
name = "workspace_name.env",
visibility = ["//visibility:public"],
)
19 changes: 0 additions & 19 deletions rust/private/rustfmt.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -204,22 +204,3 @@ rustfmt_test = rule(
},
test = True,
)

def _rustfmt_workspace_name_impl(ctx):
output = ctx.actions.declare_file(ctx.label.name)

ctx.actions.write(
output = output,
content = "RUSTFMT_WORKSPACE={}".format(
ctx.workspace_name,
),
)

return [DefaultInfo(
files = depset([output]),
)]

rustfmt_workspace_name = rule(
implementation = _rustfmt_workspace_name_impl,
doc = "A rule for detecting the workspace name for Rustfmt runfiles.",
)
25 changes: 25 additions & 0 deletions rust/private/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -610,3 +610,28 @@ def _replace_all(string, substitutions):
string = string[:pattern_start] + replacement + string[after_pattern:]

return string

_RULES_RUST_CONSUMER_WORKSPACE = "RULES_RUST_CONSUMER_WORKSPACE"

def _workspace_name_impl(ctx):
output = ctx.actions.declare_file(ctx.label.name)

ctx.actions.write(
output = output,
content = "{}={}".format(
_RULES_RUST_CONSUMER_WORKSPACE,
ctx.workspace_name,
),
)

return [DefaultInfo(
files = depset([output]),
)]

workspace_name = rule(
implementation = _workspace_name_impl,
doc = """\
A rule for detecting the workspace name and writing it to a file for
exposing it as `{}` for use with `rustc_env_files` attributes on `rust_*`
rules.""".format(_RULES_RUST_CONSUMER_WORKSPACE),
)
7 changes: 3 additions & 4 deletions tools/rust_analyzer/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
load("//rust:defs.bzl", "rust_binary", "rust_clippy", "rust_library", "rust_test")
load("//tools:tool_utils.bzl", "aspect_repository")

rust_binary(
name = "gen_rust_project",
srcs = ["main.rs"],
edition = "2018",
rustc_env = {
"ASPECT_REPOSITORY": aspect_repository(),
},
rustc_env_files = [
"//rust/private:workspace_name.env",
],
visibility = ["//visibility:public"],
deps = [
":gen_rust_project_lib",
Expand Down
9 changes: 8 additions & 1 deletion tools/rust_analyzer/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ fn main() -> anyhow::Result<()> {
.as_ref()
.expect("failed to find execution root, is --execution-root set correctly?");

let rules_rust_name = env!("ASPECT_REPOSITORY");
// In order to support rust-analzyer within rules_rust itself, this binary needs
// to understand if the name of the current workspace is rules_rust to account for
// https://github.com/bazelbuild/bazel/issues/11734
let rules_rust_name = if env!("RULES_RUST_CONSUMER_WORKSPACE") == "rules_rust" {
""
} else {
"@rules_rust"
};

// Generate the crate specs.
generate_crate_info(
Expand Down
4 changes: 1 addition & 3 deletions tools/rustfmt/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
load("//rust:defs.bzl", "rust_binary", "rust_clippy", "rust_library")
load("//tools:tool_utils.bzl", "aspect_repository")

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

Expand Down Expand Up @@ -27,7 +26,7 @@ rust_library(
"RUSTFMT_CONFIG": "$(rootpath //:rustfmt.toml)",
},
rustc_env_files = [
"@rules_rust//rust/private:rustfmt_workspace_name",
"@rules_rust//rust/private:workspace_name.env",
],
deps = [
"//tools/runfiles",
Expand All @@ -44,7 +43,6 @@ rust_binary(
],
edition = "2018",
rustc_env = {
"ASPECT_REPOSITORY": aspect_repository(),
"RUST_DEFAULT_EDITION": "$(RUST_DEFAULT_EDITION)",
},
toolchains = [
Expand Down
20 changes: 16 additions & 4 deletions tools/rustfmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ pub struct RustfmtConfig {
pub fn parse_rustfmt_config() -> RustfmtConfig {
let runfiles = runfiles::Runfiles::create().unwrap();

let rustfmt = runfiles.rlocation(concat!(env!("RUSTFMT_WORKSPACE"), "/", env!("RUSTFMT")));
let rustfmt = runfiles.rlocation(concat!(
env!("RULES_RUST_CONSUMER_WORKSPACE"),
"/",
env!("RUSTFMT")
));
if !rustfmt.exists() {
panic!("rustfmt does not exist at: {}", rustfmt.display());
}

let config = runfiles.rlocation(concat!(
env!("RUSTFMT_WORKSPACE"),
env!("RULES_RUST_CONSUMER_WORKSPACE"),
"/",
env!("RUSTFMT_CONFIG")
));
Expand Down Expand Up @@ -76,7 +80,9 @@ pub fn parse_rustfmt_manifest(manifest: &Path) -> RustfmtManifest {
edition,
sources: lines
.into_iter()
.map(|src| runfiles.rlocation(format!("{}/{}", env!("RUSTFMT_WORKSPACE"), src)))
.map(|src| {
runfiles.rlocation(format!("{}/{}", env!("RULES_RUST_CONSUMER_WORKSPACE"), src))
})
.collect(),
}
}
Expand All @@ -95,7 +101,13 @@ pub fn find_manifests() -> Vec<PathBuf> {
std::env::var("RUSTFMT_MANIFESTS")
.map(|var| {
var.split(PATH_ENV_SEP)
.map(|path| runfiles.rlocation(format!("{}/{}", env!("RUSTFMT_WORKSPACE"), path)))
.map(|path| {
runfiles.rlocation(format!(
"{}/{}",
env!("RULES_RUST_CONSUMER_WORKSPACE"),
path
))
})
.collect()
})
.unwrap_or_default()
Expand Down
15 changes: 0 additions & 15 deletions tools/tool_utils.bzl

This file was deleted.