Skip to content

Commit 6c98665

Browse files
committed
Down to 1 failing test
And that is due to a case where we have two ratomls in a source root, one of which is a `workspace_ratoml` and the other one is simple old ratoml. Since we are not checking to see if the source root is already populated with workspace ratoml, this test fails. Due to principles of clear code I believe it is reasonable to not have two HashMaps that are almost for the exact same thing. So next commit should remove `workspace_ratoml` and merge it with `krate_ratoml`s.
1 parent c9cb05c commit 6c98665

File tree

5 files changed

+66
-80
lines changed

5 files changed

+66
-80
lines changed

src/tools/rust-analyzer/crates/rust-analyzer/src/capabilities.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub fn server_capabilities(config: &Config) -> ServerCapabilities {
6767
code_action_provider: Some(config.caps().code_action_capabilities()),
6868
code_lens_provider: Some(CodeLensOptions { resolve_provider: Some(true) }),
6969
document_formatting_provider: Some(OneOf::Left(true)),
70-
document_range_formatting_provider: match config.rustfmt() {
70+
document_range_formatting_provider: match config.rustfmt(None) {
7171
RustfmtConfig::Rustfmt { enable_range_formatting: true, .. } => Some(OneOf::Left(true)),
7272
_ => Some(OneOf::Left(false)),
7373
},

src/tools/rust-analyzer/crates/rust-analyzer/src/config.rs

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -780,10 +780,10 @@ pub struct Config {
780780
user_config: Option<(GlobalLocalConfigInput, ConfigErrors)>,
781781

782782
/// TODO : This file can be used to make global changes while having only a workspace-wide scope.
783-
workspace_ratoml_change: FxHashMap<SourceRootId, (GlobalLocalConfigInput, ConfigErrors)>,
783+
workspace_ratoml: FxHashMap<SourceRootId, (GlobalLocalConfigInput, ConfigErrors)>,
784784

785785
/// For every `SourceRoot` there can be at most one RATOML file.
786-
ratoml_files: FxHashMap<SourceRootId, (LocalConfigInput, ConfigErrors)>,
786+
krate_ratoml: FxHashMap<SourceRootId, (LocalConfigInput, ConfigErrors)>,
787787

788788
/// Clone of the value that is stored inside a `GlobalState`.
789789
source_root_parent_map: Arc<FxHashMap<SourceRootId, SourceRootId>>,
@@ -927,7 +927,7 @@ impl Config {
927927
&mut String::new(),
928928
&mut toml_errors,
929929
);
930-
config.workspace_ratoml_change.insert(
930+
config.workspace_ratoml.insert(
931931
source_root_id,
932932
(
933933
GlobalLocalConfigInput::from_toml(table, &mut toml_errors),
@@ -969,7 +969,7 @@ impl Config {
969969
&mut String::new(),
970970
&mut toml_errors,
971971
);
972-
config.ratoml_files.insert(
972+
config.krate_ratoml.insert(
973973
source_root_id,
974974
(
975975
LocalConfigInput::from_toml(&table, &mut toml_errors),
@@ -1022,15 +1022,9 @@ impl Config {
10221022
.1
10231023
.0
10241024
.iter()
1025-
.chain(
1026-
config
1027-
.workspace_ratoml_change
1028-
.values()
1029-
.into_iter()
1030-
.flat_map(|it| it.1 .0.iter()),
1031-
)
1025+
.chain(config.workspace_ratoml.values().into_iter().flat_map(|it| it.1 .0.iter()))
10321026
.chain(config.user_config.as_ref().into_iter().flat_map(|it| it.1 .0.iter()))
1033-
.chain(config.ratoml_files.values().flat_map(|it| it.1 .0.iter()))
1027+
.chain(config.krate_ratoml.values().flat_map(|it| it.1 .0.iter()))
10341028
.chain(config.validation_errors.0.iter())
10351029
.cloned()
10361030
.collect(),
@@ -1070,6 +1064,7 @@ impl ConfigChange {
10701064
vfs_path: VfsPath,
10711065
content: Option<Arc<str>>,
10721066
) -> Option<(VfsPath, Option<Arc<str>>)> {
1067+
dbg!("change_ratoml", &vfs_path);
10731068
self.ratoml_file_change
10741069
.get_or_insert_with(Default::default)
10751070
.insert(source_root, (vfs_path, content))
@@ -1086,6 +1081,7 @@ impl ConfigChange {
10861081
vfs_path: VfsPath,
10871082
content: Option<Arc<str>>,
10881083
) -> Option<(VfsPath, Option<Arc<str>>)> {
1084+
dbg!("change_workspace", &vfs_path);
10891085
self.workspace_ratoml_change
10901086
.get_or_insert_with(Default::default)
10911087
.insert(source_root, (vfs_path, content))
@@ -1350,14 +1346,14 @@ impl Config {
13501346
workspace_roots,
13511347
visual_studio_code_version,
13521348
client_config: (FullConfigInput::default(), ConfigErrors(vec![])),
1353-
ratoml_files: FxHashMap::default(),
1349+
krate_ratoml: FxHashMap::default(),
13541350
default_config: DEFAULT_CONFIG_DATA.get_or_init(|| Box::leak(Box::default())),
13551351
source_root_parent_map: Arc::new(FxHashMap::default()),
13561352
user_config: None,
13571353
user_config_path,
13581354
detached_files: Default::default(),
13591355
validation_errors: Default::default(),
1360-
workspace_ratoml_change: Default::default(),
1356+
workspace_ratoml: Default::default(),
13611357
}
13621358
}
13631359

@@ -1877,16 +1873,16 @@ impl Config {
18771873
}
18781874
}
18791875

1880-
pub fn rustfmt(&self) -> RustfmtConfig {
1876+
pub fn rustfmt(&self, source_root_id: Option<SourceRootId>) -> RustfmtConfig {
18811877
match &self.rustfmt_overrideCommand(None) {
18821878
Some(args) if !args.is_empty() => {
18831879
let mut args = args.clone();
18841880
let command = args.remove(0);
18851881
RustfmtConfig::CustomCommand { command, args }
18861882
}
18871883
Some(_) | None => RustfmtConfig::Rustfmt {
1888-
extra_args: self.rustfmt_extraArgs(None).clone(),
1889-
enable_range_formatting: *self.rustfmt_rangeFormatting_enable(None),
1884+
extra_args: self.rustfmt_extraArgs(source_root_id).clone(),
1885+
enable_range_formatting: *self.rustfmt_rangeFormatting_enable(source_root_id),
18901886
},
18911887
}
18921888
}
@@ -2540,22 +2536,28 @@ macro_rules! _impl_for_config_data {
25402536
$($doc)*
25412537
#[allow(non_snake_case)]
25422538
$vis fn $field(&self, source_root: Option<SourceRootId>) -> &$ty {
2543-
let mut par: Option<SourceRootId> = source_root;
2544-
while let Some(source_root_id) = par {
2545-
par = self.source_root_parent_map.get(&source_root_id).copied();
2546-
if let Some((config, _)) = self.ratoml_files.get(&source_root_id) {
2539+
let follow = if stringify!($field) == "assist_emitMustUse" { dbg!("YEY"); true } else {false};
2540+
let mut current: Option<SourceRootId> = None;
2541+
let mut next: Option<SourceRootId> = source_root;
2542+
if follow { dbg!(&self.krate_ratoml);}
2543+
while let Some(source_root_id) = next {
2544+
current = next;
2545+
next = self.source_root_parent_map.get(&source_root_id).copied();
2546+
if let Some((config, _)) = self.krate_ratoml.get(&source_root_id) {
25472547
if let Some(value) = config.$field.as_ref() {
25482548
return value;
25492549
}
25502550
}
25512551
}
25522552

2553-
// TODO
2554-
// if let Some((root_path_ratoml, _)) = self.root_ratoml.as_ref() {
2555-
// if let Some(v) = root_path_ratoml.local.$field.as_ref() {
2556-
// return &v;
2557-
// }
2558-
// }
2553+
if let Some(current) = current {
2554+
if follow { dbg!(&self.workspace_ratoml);}
2555+
if let Some((root_path_ratoml, _)) = self.workspace_ratoml.get(&current).as_ref() {
2556+
if let Some(v) = root_path_ratoml.local.$field.as_ref() {
2557+
return &v;
2558+
}
2559+
}
2560+
}
25592561

25602562
if let Some(v) = self.client_config.0.local.$field.as_ref() {
25612563
return &v;
@@ -2582,12 +2584,22 @@ macro_rules! _impl_for_config_data {
25822584
$($doc)*
25832585
#[allow(non_snake_case)]
25842586
$vis fn $field(&self, source_root : Option<SourceRootId>) -> &$ty {
2585-
// TODO
2586-
// if let Some((root_path_ratoml, _)) = self.root_ratoml.as_ref() {
2587-
// if let Some(v) = root_path_ratoml.global.$field.as_ref() {
2588-
// return &v;
2589-
// }
2590-
// }
2587+
let follow = if stringify!($field) == "rustfmt_extraArgs" { dbg!("YEY"); true } else {false};
2588+
let mut current: Option<SourceRootId> = None;
2589+
let mut next: Option<SourceRootId> = source_root;
2590+
while let Some(source_root_id) = next {
2591+
current = next;
2592+
next = self.source_root_parent_map.get(&source_root_id).copied();
2593+
}
2594+
2595+
if let Some(current) = current {
2596+
if follow { dbg!(&self.workspace_ratoml);}
2597+
if let Some((root_path_ratoml, _)) = self.workspace_ratoml.get(&current).as_ref() {
2598+
if let Some(v) = root_path_ratoml.global.$field.as_ref() {
2599+
return &v;
2600+
}
2601+
}
2602+
}
25912603

25922604
if let Some(v) = self.client_config.0.global.$field.as_ref() {
25932605
return &v;

src/tools/rust-analyzer/crates/rust-analyzer/src/global_state.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -386,12 +386,18 @@ impl GlobalState {
386386
let mut change = ConfigChange::default();
387387
let db = self.analysis_host.raw_database();
388388

389-
// FIXME @alibektas : This is silly. There is abs no reason to use VfsPaths when there is SourceRoots. But how
389+
// FIXME @alibektas : This is silly. There is no reason to use VfsPaths when there is SourceRoots. But how
390390
// do I resolve a "workspace_root" to its corresponding id without having to rely on a cargo.toml's ( or project json etc.) file id?
391-
let workspace_roots = self
391+
let workspace_ratoml_paths = self
392392
.workspaces
393393
.iter()
394-
.map(|ws| VfsPath::from(ws.workspace_root().to_owned()))
394+
.map(|ws| {
395+
VfsPath::from({
396+
let mut p = ws.workspace_root().to_owned();
397+
p.push("rust-analyzer.toml");
398+
p
399+
})
400+
})
395401
.collect_vec();
396402

397403
for (file_id, (_change_kind, vfs_path)) in modified_ratoml_files {
@@ -405,7 +411,7 @@ impl GlobalState {
405411
let sr_id = db.file_source_root(file_id);
406412
let sr = db.source_root(sr_id);
407413

408-
if workspace_roots.contains(&vfs_path) {
414+
if workspace_ratoml_paths.contains(&vfs_path) {
409415
change.change_workspace_ratoml(
410416
sr_id,
411417
vfs_path,
@@ -422,6 +428,7 @@ impl GlobalState {
422428
) {
423429
// SourceRoot has more than 1 RATOML files. In this case lexicographically smaller wins.
424430
if old_path < vfs_path {
431+
dbg!("HARBIDEN");
425432
span!(Level::ERROR, "Two `rust-analyzer.toml` files were found inside the same crate. {vfs_path} has no effect.");
426433
// Put the old one back in.
427434
change.change_ratoml(sr_id, old_path, old_text);

src/tools/rust-analyzer/crates/rust-analyzer/src/handlers/request.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,8 +2090,9 @@ fn run_rustfmt(
20902090
let edition = editions.iter().copied().max();
20912091

20922092
let line_index = snap.file_line_index(file_id)?;
2093+
let sr = snap.analysis.source_root_id(file_id)?;
20932094

2094-
let mut command = match snap.config.rustfmt() {
2095+
let mut command = match snap.config.rustfmt(Some(sr)) {
20952096
RustfmtConfig::Rustfmt { extra_args, enable_range_formatting } => {
20962097
// FIXME: Set RUSTUP_TOOLCHAIN
20972098
let mut cmd = process::Command::new(toolchain::Tool::Rustfmt.path());
@@ -2280,7 +2281,7 @@ pub(crate) fn internal_testing_fetch_config(
22802281
serde_json::to_value(match &*params.config {
22812282
"local" => state.config.assist(source_root).assist_emit_must_use,
22822283
"global" => matches!(
2283-
state.config.rustfmt(),
2284+
state.config.rustfmt(source_root),
22842285
RustfmtConfig::Rustfmt { enable_range_formatting: true, .. }
22852286
),
22862287
_ => return Err(anyhow::anyhow!("Unknown test config key: {}", params.config)),

src/tools/rust-analyzer/crates/rust-analyzer/tests/slow-tests/ratoml.rs

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -700,37 +700,6 @@ fn ratoml_multiple_ratoml_in_single_source_root() {
700700
);
701701

702702
assert!(server.query(QueryType::Local, 3));
703-
704-
let server = RatomlTest::new(
705-
vec![
706-
r#"
707-
//- /p1/Cargo.toml
708-
[package]
709-
name = "p1"
710-
version = "0.1.0"
711-
edition = "2021"
712-
"#,
713-
r#"
714-
//- /p1/src/rust-analyzer.toml
715-
assist.emitMustUse = false
716-
"#,
717-
r#"
718-
//- /p1/rust-analyzer.toml
719-
assist.emitMustUse = true
720-
"#,
721-
r#"
722-
//- /p1/src/lib.rs
723-
enum Value {
724-
Number(i32),
725-
Text(String),
726-
}
727-
"#,
728-
],
729-
vec!["p1"],
730-
None,
731-
);
732-
733-
assert!(server.query(QueryType::Local, 3));
734703
}
735704

736705
/// If a root is non-local, so we cannot find what its parent is
@@ -808,7 +777,6 @@ enum Value {
808777
/// Having a ratoml file at the root of a project enables
809778
/// configuring global level configurations as well.
810779
#[test]
811-
// #[ignore = "Root ratomls are not being looked for on startup. Fix this."]
812780
fn ratoml_in_root_is_global() {
813781
let server = RatomlTest::new(
814782
vec![
@@ -820,7 +788,7 @@ version = "0.1.0"
820788
edition = "2021"
821789
"#,
822790
r#"
823-
//- /rust-analyzer.toml
791+
//- /p1/rust-analyzer.toml
824792
rustfmt.rangeFormatting.enable = true
825793
"#,
826794
r#"
@@ -829,15 +797,14 @@ fn main() {
829797
todo!()
830798
}"#,
831799
],
832-
vec![],
800+
vec!["p1"],
833801
None,
834802
);
835803

836804
assert!(server.query(QueryType::Global, 2));
837805
}
838806

839807
#[test]
840-
#[ignore = "Root ratomls are not being looked for on startup. Fix this."]
841808
fn ratoml_root_is_updateable() {
842809
let mut server = RatomlTest::new(
843810
vec![
@@ -849,7 +816,7 @@ version = "0.1.0"
849816
edition = "2021"
850817
"#,
851818
r#"
852-
//- /rust-analyzer.toml
819+
//- /p1/rust-analyzer.toml
853820
rustfmt.rangeFormatting.enable = true
854821
"#,
855822
r#"
@@ -858,7 +825,7 @@ fn main() {
858825
todo!()
859826
}"#,
860827
],
861-
vec![],
828+
vec!["p1"],
862829
None,
863830
);
864831

@@ -868,7 +835,6 @@ fn main() {
868835
}
869836

870837
#[test]
871-
#[ignore = "Root ratomls are not being looked for on startup. Fix this."]
872838
fn ratoml_root_is_deletable() {
873839
let mut server = RatomlTest::new(
874840
vec![
@@ -880,7 +846,7 @@ version = "0.1.0"
880846
edition = "2021"
881847
"#,
882848
r#"
883-
//- /rust-analyzer.toml
849+
//- /p1/rust-analyzer.toml
884850
rustfmt.rangeFormatting.enable = true
885851
"#,
886852
r#"
@@ -889,7 +855,7 @@ fn main() {
889855
todo!()
890856
}"#,
891857
],
892-
vec![],
858+
vec!["p1"],
893859
None,
894860
);
895861

0 commit comments

Comments
 (0)