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

feat: Allow declaring cfg groups in rust-project.json, to help sharing common cfgs #17857

Merged
merged 1 commit into from
Aug 23, 2024
Merged
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
34 changes: 30 additions & 4 deletions crates/project-model/src/project_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
use base_db::{CrateDisplayName, CrateName};
use cfg::CfgAtom;
use paths::{AbsPath, AbsPathBuf, Utf8PathBuf};
use rustc_hash::FxHashMap;
use rustc_hash::{FxHashMap, FxHashSet};
use serde::{de, Deserialize, Serialize};
use span::Edition;

Expand Down Expand Up @@ -122,6 +122,25 @@ impl ProjectJson {
None => None,
};

let cfg = crate_data
.cfg_groups
.iter()
.flat_map(|cfg_extend| {
let cfg_group = data.cfg_groups.get(cfg_extend);
match cfg_group {
Some(cfg_group) => cfg_group.0.iter().cloned(),
None => {
tracing::error!(
"Unknown cfg group `{cfg_extend}` in crate `{}`",
crate_data.display_name.as_deref().unwrap_or("<unknown>"),
);
[].iter().cloned()
Comment on lines +133 to +137
Copy link
Contributor

Choose a reason for hiding this comment

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

More of a large meta point, but I think it's be a good idea to us to consider using https://github.com/rust-lang/annotate-snippets-rs for rust-analyzer-specific errors.

Copy link
Member

Choose a reason for hiding this comment

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

That seems tailored to CLI output, doesn't sound too useful for us

}
}
})
.chain(crate_data.cfg.0)
.collect();

Crate {
display_name: crate_data
.display_name
Expand All @@ -131,7 +150,7 @@ impl ProjectJson {
edition: crate_data.edition.into(),
version: crate_data.version.as_ref().map(ToString::to_string),
deps: crate_data.deps,
cfg: crate_data.cfg,
cfg,
target: crate_data.target,
env: crate_data.env,
proc_macro_dylib_path: crate_data
Expand Down Expand Up @@ -306,11 +325,17 @@ pub enum RunnableKind {
pub struct ProjectJsonData {
sysroot: Option<Utf8PathBuf>,
sysroot_src: Option<Utf8PathBuf>,
#[serde(default)]
cfg_groups: FxHashMap<String, CfgList>,
crates: Vec<CrateData>,
#[serde(default)]
runnables: Vec<RunnableData>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq, Default)]
#[serde(transparent)]
struct CfgList(#[serde(with = "cfg_")] Vec<CfgAtom>);

#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
struct CrateData {
display_name: Option<String>,
Expand All @@ -320,8 +345,9 @@ struct CrateData {
version: Option<semver::Version>,
deps: Vec<Dep>,
#[serde(default)]
#[serde(with = "cfg_")]
cfg: Vec<CfgAtom>,
cfg_groups: FxHashSet<String>,
#[serde(default)]
cfg: CfgList,
target: Option<String>,
#[serde(default)]
env: FxHashMap<String, String>,
Expand Down
6 changes: 6 additions & 0 deletions crates/project-model/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ fn rust_project_hello_world_project_model() {
);
}

#[test]
fn rust_project_cfg_groups() {
let (crate_graph, _proc_macros) = load_rust_project("cfg-groups.json");
check_crate_graph(crate_graph, expect_file!["../test_data/output/rust_project_cfg_groups.txt"]);
}

#[test]
fn rust_project_is_proc_macro_has_proc_macro_dep() {
let (crate_graph, _proc_macros) = load_rust_project("is-proc-macro-project.json");
Expand Down
26 changes: 26 additions & 0 deletions crates/project-model/test_data/cfg-groups.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"sysroot_src": null,
"cfg_groups": {
"group1": ["group1_cfg=\"some_config\"", "group1_other_cfg=\"other_config\""],
"group2": ["group2_cfg=\"yet_another_config\""]
},
"crates": [
{
"display_name": "hello_world",
"root_module": "$ROOT$src/lib.rs",
"edition": "2018",
"cfg_groups": ["group1", "group2"],
"deps": [],
"is_workspace_member": true
},
{
"display_name": "other_crate",
"root_module": "$ROOT$src/lib.rs",
"edition": "2018",
"cfg_groups": ["group2"],
"cfg": ["group2_cfg=\"fourth_config\"", "unrelated_cfg"],
"deps": [],
"is_workspace_member": true
}
]
}
Loading