Skip to content

Commit 1fb0580

Browse files
committed
Fix config guard lock for ratoml tests
1 parent 4fcecbb commit 1fb0580

File tree

5 files changed

+26
-21
lines changed

5 files changed

+26
-21
lines changed

crates/load-cargo/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl ProjectFolders {
156156
pub fn new(
157157
workspaces: &[ProjectWorkspace],
158158
global_excludes: &[AbsPathBuf],
159-
user_config_dir_path: Option<&'static AbsPath>,
159+
user_config_dir_path: Option<&AbsPath>,
160160
) -> ProjectFolders {
161161
let mut res = ProjectFolders::default();
162162
let mut fsc = FileSetConfig::builder();
@@ -302,7 +302,7 @@ impl ProjectFolders {
302302
p
303303
};
304304

305-
let file_set_roots: Vec<VfsPath> = vec![VfsPath::from(ratoml_path.to_owned())];
305+
let file_set_roots = vec![VfsPath::from(ratoml_path.to_owned())];
306306
let entry = vfs::loader::Entry::Files(vec![ratoml_path.to_owned()]);
307307

308308
res.watch.push(res.load.len());

crates/rust-analyzer/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,7 @@ pub struct NotificationsConfig {
12541254
pub cargo_toml_not_found: bool,
12551255
}
12561256

1257-
#[derive(Deserialize, Serialize, Debug, Clone)]
1257+
#[derive(Debug, Clone)]
12581258
pub enum RustfmtConfig {
12591259
Rustfmt { extra_args: Vec<String>, enable_range_formatting: bool },
12601260
CustomCommand { command: String, args: Vec<String> },

crates/rust-analyzer/src/global_state.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -392,13 +392,13 @@ impl GlobalState {
392392
|| !self.config.same_source_root_parent_map(&self.local_roots_parent_map)
393393
{
394394
let config_change = {
395-
let user_config_path = {
396-
let mut p = Config::user_config_dir_path().unwrap().to_path_buf();
395+
let user_config_path = (|| {
396+
let mut p = Config::user_config_dir_path()?.to_path_buf();
397397
p.push("rust-analyzer.toml");
398-
p
399-
};
398+
Some(p)
399+
})();
400400

401-
let user_config_abs_path = Some(user_config_path.as_path());
401+
let user_config_abs_path = user_config_path.as_deref();
402402

403403
let mut change = ConfigChange::default();
404404
let db = self.analysis_host.raw_database();

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ struct RatomlTest {
2020
urls: Vec<Url>,
2121
server: Server,
2222
tmp_path: Utf8PathBuf,
23+
config_locked: bool,
2324
}
2425

2526
impl RatomlTest {
@@ -65,13 +66,14 @@ impl RatomlTest {
6566

6667
let server = project.server_with_lock(prelock).wait_until_workspace_is_loaded();
6768

68-
let mut case = Self { urls: vec![], server, tmp_path };
69-
let urls = fixtures.iter().map(|fixture| case.fixture_path(fixture)).collect::<Vec<_>>();
69+
let mut case = Self { urls: vec![], server, tmp_path, config_locked: prelock };
70+
let urls =
71+
fixtures.iter().map(|fixture| case.fixture_path(fixture, prelock)).collect::<Vec<_>>();
7072
case.urls = urls;
7173
case
7274
}
7375

74-
fn fixture_path(&self, fixture: &str) -> Url {
76+
fn fixture_path(&self, fixture: &str, prelock: bool) -> Url {
7577
let mut lines = fixture.trim().split('\n');
7678

7779
let mut path =
@@ -89,6 +91,7 @@ impl RatomlTest {
8991
let mut spl = spl.into_iter();
9092
if let Some(first) = spl.next() {
9193
if first == "$$CONFIG_DIR$$" {
94+
assert!(prelock, "this test requires prelock to be set");
9295
path = Config::user_config_dir_path().unwrap().to_path_buf().into();
9396
} else {
9497
path = path.join(first);
@@ -109,7 +112,7 @@ impl RatomlTest {
109112
}
110113

111114
fn create(&mut self, fixture_path: &str, text: String) {
112-
let url = self.fixture_path(fixture_path);
115+
let url = self.fixture_path(fixture_path, self.config_locked);
113116

114117
self.server.notification::<DidOpenTextDocument>(DidOpenTextDocumentParams {
115118
text_document: TextDocumentItem {

crates/rust-analyzer/tests/slow-tests/support.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{
22
cell::{Cell, RefCell},
33
env, fs,
4-
sync::Once,
4+
sync::{Once, OnceLock},
55
time::Duration,
66
};
77

@@ -141,12 +141,17 @@ impl Project<'_> {
141141
/// file in the config dir after server is run, something where our naive approach comes short.
142142
/// Using a `prelock` allows us to force a lock when we know we need it.
143143
pub(crate) fn server_with_lock(self, prelock: bool) -> Server {
144-
static CONFIG_DIR_LOCK: Mutex<()> = Mutex::new(());
144+
static CONFIG_DIR_LOCK: OnceLock<Mutex<()>> = OnceLock::new();
145145

146146
let mut config_dir_guard = if prelock {
147-
let v = Some(CONFIG_DIR_LOCK.lock());
148-
env::set_var("__TEST_RA_USER_CONFIG_DIR", TestDir::new().path());
149-
v
147+
Some(
148+
CONFIG_DIR_LOCK
149+
.get_or_init(|| {
150+
env::set_var("__TEST_RA_USER_CONFIG_DIR", TestDir::new().keep().path());
151+
Mutex::new(())
152+
})
153+
.lock(),
154+
)
150155
} else {
151156
None
152157
};
@@ -185,10 +190,7 @@ impl Project<'_> {
185190

186191
for entry in fixture {
187192
if let Some(pth) = entry.path.strip_prefix("/$$CONFIG_DIR$$") {
188-
if config_dir_guard.is_none() {
189-
config_dir_guard = Some(CONFIG_DIR_LOCK.lock());
190-
env::set_var("__TEST_RA_USER_CONFIG_DIR", TestDir::new().path());
191-
}
193+
assert!(config_dir_guard.is_some(), "this test requires prelock to be set to true");
192194

193195
let path = Config::user_config_dir_path().unwrap().join(&pth['/'.len_utf8()..]);
194196
fs::create_dir_all(path.parent().unwrap()).unwrap();

0 commit comments

Comments
 (0)