Improve logic for finding VSCode / Cursor settings files (#32721)

* Fixes a bug where for Cursor, `config_dir()` (Zed's config dir) was
being used instead of `dirs::config_dir` (`~/.config` /
`$XDG_CONFIG_HOME`).

* Adds support for windows, before it was using the user profile folder
+ `/.config` which is incorrect.

* Now looks using a variety of product names - `["Code", "Code - OSS",
"Code Dev", "Code - OSS Dev", "code-oss-dev", "VSCodium"]`.

* Now shows settings path that was read before confirming import.

Including this path in the confirmation modal is a bit ugly (making it
link-styled and clickable would be nice), but I think it's better to
include it now that it is selecting the first match of a list of
candidate paths:


![image](https://github.com/user-attachments/assets/ceada4c2-96a6-4a84-a188-a1d93521ab26)

Release Notes:

- Added more settings file locations to check for VS Code / Cursor
settings import.
This commit is contained in:
Michael Sloan 2025-06-14 21:39:54 -06:00 committed by GitHub
parent afa70034d5
commit a5ceef35fa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 138 additions and 59 deletions

View file

@ -1,8 +1,8 @@
use anyhow::Result;
use anyhow::{Context as _, Result, anyhow};
use fs::Fs;
use paths::{cursor_settings_file_paths, vscode_settings_file_paths};
use serde_json::{Map, Value};
use std::sync::Arc;
use std::{path::Path, rc::Rc, sync::Arc};
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum VsCodeSettingsSource {
@ -21,26 +21,59 @@ impl std::fmt::Display for VsCodeSettingsSource {
pub struct VsCodeSettings {
pub source: VsCodeSettingsSource,
pub path: Rc<Path>,
content: Map<String, Value>,
}
impl VsCodeSettings {
#[cfg(any(test, feature = "test-support"))]
pub fn from_str(content: &str, source: VsCodeSettingsSource) -> Result<Self> {
Ok(Self {
source,
path: Path::new("/example-path/Code/User/settings.json").into(),
content: serde_json_lenient::from_str(content)?,
})
}
pub async fn load_user_settings(source: VsCodeSettingsSource, fs: Arc<dyn Fs>) -> Result<Self> {
let path = match source {
VsCodeSettingsSource::VsCode => paths::vscode_settings_file(),
VsCodeSettingsSource::Cursor => paths::cursor_settings_file(),
let candidate_paths = match source {
VsCodeSettingsSource::VsCode => vscode_settings_file_paths(),
VsCodeSettingsSource::Cursor => cursor_settings_file_paths(),
};
let content = fs.load(path).await?;
let mut path = None;
for candidate_path in candidate_paths.iter() {
if fs.is_file(candidate_path).await {
path = Some(candidate_path.clone());
}
}
let Some(path) = path else {
return Err(anyhow!(
"No settings file found, expected to find it in one of the following paths:\n{}",
candidate_paths
.into_iter()
.map(|path| path.to_string_lossy().to_string())
.collect::<Vec<_>>()
.join("\n")
));
};
let content = fs.load(&path).await.with_context(|| {
format!(
"Error loading {} settings file from {}",
source,
path.display()
)
})?;
let content = serde_json_lenient::from_str(&content).with_context(|| {
format!(
"Error parsing {} settings file from {}",
source,
path.display()
)
})?;
Ok(Self {
source,
content: serde_json_lenient::from_str(&content)?,
path: path.into(),
content,
})
}