Skip to content

Commit

Permalink
rename some fields in rust-project.json
Browse files Browse the repository at this point in the history
Summary:
This diff makes a few changes to rust-project.json to align with the changes in rust-analyzer in rust-lang/rust-analyzer#16840. They are:
- Remove any mentions of Buck from `rust-project.json`
- Move the older fields to a `deprecated` module and emit both the new and the old options. The `deprecated` module is removed in D58201152, which will land a few days after this diff lands.

Reviewed By: Wilfred

Differential Revision: D58150427

fbshipit-source-id: 17554b9226f4547c8eb89675b954f5dbeb084cec
  • Loading branch information
davidbarsky authored and facebook-github-bot committed Jun 6, 2024
1 parent 3349d02 commit 7169723
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 36 deletions.
50 changes: 42 additions & 8 deletions integrations/rust-project/src/buck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ use tracing::trace;
use tracing::warn;
use tracing::Level;

use crate::json_project::BuckExtensions;
use crate::json_project::deprecated::{self};
use crate::json_project::Build;
use crate::json_project::Edition;
use crate::json_project::JsonProject;
use crate::json_project::Runnables;
use crate::json_project::Runnable;
use crate::json_project::RunnableKind;
use crate::json_project::Source;
use crate::json_project::Sysroot;
use crate::json_project::TargetSpec;
use crate::target::AliasedTargetInfo;
use crate::target::ExpandedAndResolved;
use crate::target::Kind;
Expand Down Expand Up @@ -152,12 +153,24 @@ pub(crate) fn to_json_project(
include_dirs.insert(parent.to_owned());
}

let spec = if info.in_workspace {
let spec = TargetSpec {
let build = if info.in_workspace {
let build = Build {
label: target.clone(),
build_file: build_file.to_owned(),
target_kind: info.kind.clone().into(),
};
Some(build)
} else {
None
};

// FIXME: remove this after a few days; it is only for backwards compatibility.
let target_spec = if info.in_workspace {
let spec = deprecated::TargetSpec {
manifest_file: build_file.to_owned(),
target_label: target.to_string(),
target_kind: info.kind.clone().into(),
runnables: Runnables {
runnables: deprecated::Runnables {
check: vec!["build".to_owned(), target.to_string()],
run: vec!["run".to_owned(), target.to_string()],
test: vec![
Expand All @@ -178,7 +191,7 @@ pub(crate) fn to_json_project(
let crate_info = Crate {
display_name: Some(info.display_name()),
root_module,
buck_extensions: BuckExtensions {
buck_extensions: deprecated::BuckExtensions {
label: target.to_owned(),
build_file: build_file.to_owned(),
},
Expand All @@ -191,7 +204,8 @@ pub(crate) fn to_json_project(
}),
cfg: info.cfg(),
env,
target_spec: spec,
target_spec,
build,
is_proc_macro: info.proc_macro.unwrap_or(false),
proc_macro_dylib_path,
..Default::default()
Expand All @@ -206,6 +220,26 @@ pub(crate) fn to_json_project(
let jp = JsonProject {
sysroot,
crates,
runnables: vec![
Runnable {
program: "buck".to_owned(),
args: vec!["build".to_owned(), "{label}".to_owned()],
cwd: project_root.to_owned(),
kind: RunnableKind::Check,
},
Runnable {
program: "buck".to_owned(),
args: vec![
"test".to_owned(),
"{label}".to_owned(),
"--".to_owned(),
"{test_id}".to_owned(),
"--print-passing-details".to_owned(),
],
cwd: project_root.to_owned(),
kind: RunnableKind::TestOne,
},
],
// needed to ignore the generated `rust-project.json` in diffs, but including the actual
// string will mark this file as generated
generated: String::from("\x40generated"),
Expand Down
87 changes: 62 additions & 25 deletions integrations/rust-project/src/json_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub(crate) struct JsonProject {
/// Must include all transitive dependencies as well as sysroot crate (libstd,
/// libcore, etc.).
pub(crate) crates: Vec<Crate>,
pub(crate) runnables: Vec<Runnable>,
pub(crate) generated: String,
}

Expand All @@ -43,7 +44,7 @@ pub(crate) struct Crate {
pub(crate) display_name: Option<String>,
/// The path to the root module of the crate.
pub(crate) root_module: PathBuf,
pub(crate) buck_extensions: BuckExtensions,
pub(crate) buck_extensions: deprecated::BuckExtensions,
pub(crate) edition: Edition,
pub(crate) deps: Vec<Dep>,
/// Should this crate be treated as a member of
Expand Down Expand Up @@ -84,7 +85,9 @@ pub(crate) struct Crate {
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) target: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) target_spec: Option<TargetSpec>,
pub(crate) build: Option<Build>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) target_spec: Option<deprecated::TargetSpec>,
/// Environment for the crate, often used by `env!`.
pub(crate) env: FxHashMap<String, String>,
/// Whether the crate is a proc-macro crate/
Expand All @@ -95,18 +98,6 @@ pub(crate) struct Crate {
pub(crate) proc_macro_dylib_path: Option<PathBuf>,
}

/// Buck-specific extensions to the `rust-project.json` format.
///
/// This is needed to add support for reloading `rust-analyzer` when
/// a `TARGETS` file is changed.
#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Eq)]
pub(crate) struct BuckExtensions {
/// Path corresponding to the BUCK defining the crate.
pub(crate) build_file: PathBuf,
/// A name corresponding to the Buck target of the crate.
pub(crate) label: Target,
}

/// Build system-specific additions the `rust-project.json`.
///
/// rust-analyzer encodes Cargo-specific knowledge in features
Expand Down Expand Up @@ -145,13 +136,11 @@ pub(crate) struct BuckExtensions {
/// }
/// ```
#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Eq)]
pub(crate) struct TargetSpec {
/// `manifest_file` corresponds to the `BUCK`/`TARGETS` file.
pub(crate) manifest_file: PathBuf,
pub(crate) target_label: String,
pub(crate) struct Build {
pub(crate) label: Target,
/// `build_file` corresponds to the `BUCK`/`TARGETS` file.
pub(crate) build_file: PathBuf,
pub(crate) target_kind: TargetKind,
pub(crate) runnables: Runnables,
pub(crate) flycheck_command: Vec<String>,
}

#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -179,11 +168,22 @@ impl From<crate::target::Kind> for TargetKind {
}
}

#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Eq)]
pub(crate) struct Runnables {
pub(crate) check: Vec<String>,
pub(crate) run: Vec<String>,
pub(crate) test: Vec<String>,
#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Runnable {
pub program: String,
pub args: Vec<String>,
pub cwd: PathBuf,
pub kind: RunnableKind,
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub enum RunnableKind {
Check,
Flycheck,
Run,
TestOne,
}

#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -245,3 +245,40 @@ pub(crate) struct Sysroot {
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) sysroot_src: Option<PathBuf>,
}

// FIXME: remove these structs after a few days; they're only here for backwards compability
// and not break users.
pub(crate) mod deprecated {
use super::*;

#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Eq)]
#[serde(rename = "TargetSpec")]
pub(crate) struct TargetSpec {
/// `manifest_file` corresponds to the `BUCK`/`TARGETS` file.
pub(crate) manifest_file: PathBuf,
pub(crate) target_label: String,
pub(crate) target_kind: TargetKind,
pub(crate) runnables: deprecated::Runnables,
pub(crate) flycheck_command: Vec<String>,
}

#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Eq)]
#[serde(rename = "Runnables")]
pub(crate) struct Runnables {
pub(crate) check: Vec<String>,
pub(crate) run: Vec<String>,
pub(crate) test: Vec<String>,
}

/// Buck-specific extensions to the `rust-project.json` format.
///
/// This is needed to add support for reloading `rust-analyzer` when
/// a `TARGETS` file is changed.
#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Eq)]
pub(crate) struct BuckExtensions {
/// Path corresponding to the BUCK defining the crate.
pub(crate) build_file: PathBuf,
/// A name corresponding to the Buck target of the crate.
pub(crate) label: Target,
}
}
13 changes: 10 additions & 3 deletions integrations/rust-project/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,9 @@ fn handle_did_save_buck_file(
};

let targets = crates
.iter()
.map(|krate| krate.buck_extensions.label.clone())
.into_iter()
.filter_map(|krate| krate.build.clone())
.map(|build| build.label)
.collect::<Vec<Target>>();

info!(?params.text_document, crates = ?targets, "got document");
Expand Down Expand Up @@ -404,7 +405,13 @@ fn find_changed_crate<'a>(
let impacted_crates = project
.crates
.iter()
.filter(|krate| krate.buck_extensions.build_file == changed_file)
.filter(|krate| {
let Some(build) = &krate.build else {
return false;
};

build.build_file == changed_file
})
.collect::<Vec<&Crate>>();
if !impacted_crates.is_empty() {
return Some((idx, impacted_crates));
Expand Down

0 comments on commit 7169723

Please sign in to comment.