Skip to content

Commit 9323968

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

File tree

6 files changed

+39
-41
lines changed

6 files changed

+39
-41
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: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
//! Of particular interest is the `feature_flags` hash map: while other fields
44
//! configure the server itself, feature flags are passed into analysis, and
55
//! tweak things like automatic insertion of `()` in completions.
6-
use std::{
7-
env, fmt, iter,
8-
ops::Not,
9-
sync::{LazyLock, OnceLock},
10-
};
6+
use std::{env, fmt, iter, ops::Not, sync::OnceLock};
117

128
use cfg::{CfgAtom, CfgDiff};
139
use hir::Symbol;
@@ -805,16 +801,13 @@ impl std::ops::Deref for Config {
805801

806802
impl Config {
807803
/// Path to the user configuration dir. This can be seen as a generic way to define what would be `$XDG_CONFIG_HOME/rust-analyzer` in Linux.
808-
pub fn user_config_dir_path() -> Option<&'static AbsPath> {
809-
static USER_CONFIG_PATH: LazyLock<Option<AbsPathBuf>> = LazyLock::new(|| {
810-
let user_config_path = if let Some(path) = env::var_os("__TEST_RA_USER_CONFIG_DIR") {
811-
std::path::PathBuf::from(path)
812-
} else {
813-
dirs::config_dir()?.join("rust-analyzer")
814-
};
815-
Some(AbsPathBuf::assert_utf8(user_config_path))
816-
});
817-
USER_CONFIG_PATH.as_deref()
804+
pub fn user_config_dir_path() -> Option<AbsPathBuf> {
805+
let user_config_path = if let Some(path) = env::var_os("__TEST_RA_USER_CONFIG_DIR") {
806+
std::path::PathBuf::from(path)
807+
} else {
808+
dirs::config_dir()?.join("rust-analyzer")
809+
};
810+
Some(AbsPathBuf::assert_utf8(user_config_path))
818811
}
819812

820813
pub fn same_source_root_parent_map(
@@ -1254,7 +1247,7 @@ pub struct NotificationsConfig {
12541247
pub cargo_toml_not_found: bool,
12551248
}
12561249

1257-
#[derive(Deserialize, Serialize, Debug, Clone)]
1250+
#[derive(Debug, Clone)]
12581251
pub enum RustfmtConfig {
12591252
Rustfmt { extra_args: Vec<String>, enable_range_formatting: bool },
12601253
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()?;
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/src/reload.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ impl GlobalState {
590590
}
591591

592592
watchers.extend(
593-
iter::once(Config::user_config_dir_path())
593+
iter::once(Config::user_config_dir_path().as_deref())
594594
.chain(self.workspaces.iter().map(|ws| ws.manifest().map(ManifestPath::as_ref)))
595595
.flatten()
596596
.map(|glob_pattern| lsp_types::FileSystemWatcher {
@@ -616,7 +616,7 @@ impl GlobalState {
616616
let project_folders = ProjectFolders::new(
617617
&self.workspaces,
618618
&files_config.exclude,
619-
Config::user_config_dir_path().to_owned(),
619+
Config::user_config_dir_path().as_deref(),
620620
);
621621

622622
if (self.proc_macro_clients.is_empty() || !same_workspaces)

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

Lines changed: 8 additions & 5 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,7 +91,8 @@ impl RatomlTest {
8991
let mut spl = spl.into_iter();
9092
if let Some(first) = spl.next() {
9193
if first == "$$CONFIG_DIR$$" {
92-
path = Config::user_config_dir_path().unwrap().to_path_buf().into();
94+
assert!(prelock, "this test requires prelock to be set");
95+
path = Config::user_config_dir_path().unwrap().into();
9396
} else {
9497
path = path.join(first);
9598
}
@@ -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: 13 additions & 11 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(());
145-
146-
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
144+
static CONFIG_DIR_LOCK: OnceLock<Mutex<()>> = OnceLock::new();
145+
146+
let config_dir_guard = if prelock {
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)