Skip to content

fix: Make rust-analyzer.files.excludeDirs work, actually #18998

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

Merged
merged 2 commits into from
Feb 11, 2025
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
4 changes: 3 additions & 1 deletion crates/load-cargo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ pub fn load_workspace(
let contents = loader.load_sync(path);
let path = vfs::VfsPath::from(path.to_path_buf());
vfs.set_file_contents(path.clone(), contents);
vfs.file_id(&path)
vfs.file_id(&path).and_then(|(file_id, excluded)| {
(excluded == vfs::FileExcluded::No).then_some(file_id)
})
},
extra_env,
);
Expand Down
12 changes: 8 additions & 4 deletions crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ config_data! {
completion_snippets_custom: FxHashMap<String, SnippetDef> = Config::completion_snippets_default(),


/// These directories will be ignored by rust-analyzer. They are
/// These paths (file/directories) will be ignored by rust-analyzer. They are
/// relative to the workspace root, and globs are not supported. You may
/// also need to add the folders to Code's `files.watcherExclude`.
files_excludeDirs: Vec<Utf8PathBuf> = vec![],
files_exclude | files_excludeDirs: Vec<Utf8PathBuf> = vec![],



Expand Down Expand Up @@ -1787,7 +1787,7 @@ impl Config {

fn discovered_projects(&self) -> Vec<ManifestOrProjectJson> {
let exclude_dirs: Vec<_> =
self.files_excludeDirs().iter().map(|p| self.root_path.join(p)).collect();
self.files_exclude().iter().map(|p| self.root_path.join(p)).collect();

let mut projects = vec![];
for fs_proj in &self.discovered_projects_from_filesystem {
Expand Down Expand Up @@ -1909,10 +1909,14 @@ impl Config {
}
_ => FilesWatcher::Server,
},
exclude: self.files_excludeDirs().iter().map(|it| self.root_path.join(it)).collect(),
exclude: self.excluded().collect(),
}
}

pub fn excluded(&self) -> impl Iterator<Item = AbsPathBuf> + use<'_> {
self.files_exclude().iter().map(|it| self.root_path.join(it))
}

pub fn notifications(&self) -> NotificationsConfig {
NotificationsConfig {
cargo_toml_not_found: self.notifications_cargoTomlNotFound().to_owned(),
Expand Down
25 changes: 17 additions & 8 deletions crates/rust-analyzer/src/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,15 +650,17 @@ impl GlobalStateSnapshot {
RwLockReadGuard::map(self.vfs.read(), |(it, _)| it)
}

pub(crate) fn url_to_file_id(&self, url: &Url) -> anyhow::Result<FileId> {
/// Returns `None` if the file was excluded.
pub(crate) fn url_to_file_id(&self, url: &Url) -> anyhow::Result<Option<FileId>> {
url_to_file_id(&self.vfs_read(), url)
}

pub(crate) fn file_id_to_url(&self, id: FileId) -> Url {
file_id_to_url(&self.vfs_read(), id)
}

pub(crate) fn vfs_path_to_file_id(&self, vfs_path: &VfsPath) -> anyhow::Result<FileId> {
/// Returns `None` if the file was excluded.
pub(crate) fn vfs_path_to_file_id(&self, vfs_path: &VfsPath) -> anyhow::Result<Option<FileId>> {
vfs_path_to_file_id(&self.vfs_read(), vfs_path)
}

Expand Down Expand Up @@ -750,14 +752,21 @@ pub(crate) fn file_id_to_url(vfs: &vfs::Vfs, id: FileId) -> Url {
url_from_abs_path(path)
}

pub(crate) fn url_to_file_id(vfs: &vfs::Vfs, url: &Url) -> anyhow::Result<FileId> {
/// Returns `None` if the file was excluded.
pub(crate) fn url_to_file_id(vfs: &vfs::Vfs, url: &Url) -> anyhow::Result<Option<FileId>> {
let path = from_proto::vfs_path(url)?;
let res = vfs.file_id(&path).ok_or_else(|| anyhow::format_err!("file not found: {path}"))?;
Ok(res)
vfs_path_to_file_id(vfs, &path)
}

pub(crate) fn vfs_path_to_file_id(vfs: &vfs::Vfs, vfs_path: &VfsPath) -> anyhow::Result<FileId> {
let res =
/// Returns `None` if the file was excluded.
pub(crate) fn vfs_path_to_file_id(
vfs: &vfs::Vfs,
vfs_path: &VfsPath,
) -> anyhow::Result<Option<FileId>> {
let (file_id, excluded) =
vfs.file_id(vfs_path).ok_or_else(|| anyhow::format_err!("file not found: {vfs_path}"))?;
Ok(res)
match excluded {
vfs::FileExcluded::Yes => Ok(None),
vfs::FileExcluded::No => Ok(Some(file_id)),
}
}
16 changes: 13 additions & 3 deletions crates/rust-analyzer/src/handlers/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::{
mem_docs::DocumentData,
reload,
target_spec::TargetSpec,
try_default,
};

pub(crate) fn handle_cancel(state: &mut GlobalState, params: CancelParams) -> anyhow::Result<()> {
Expand Down Expand Up @@ -74,6 +75,14 @@ pub(crate) fn handle_did_open_text_document(
tracing::error!("duplicate DidOpenTextDocument: {}", path);
}

if let Some(abs_path) = path.as_path() {
if state.config.excluded().any(|excluded| abs_path.starts_with(&excluded)) {
tracing::trace!("opened excluded file {abs_path}");
state.vfs.write().0.insert_excluded_file(path);
return Ok(());
}
}

let contents = params.text_document.text.into_bytes();
state.vfs.write().0.set_file_contents(path, Some(contents));
if state.config.discover_workspace_config().is_some() {
Expand Down Expand Up @@ -127,7 +136,8 @@ pub(crate) fn handle_did_close_text_document(
tracing::error!("orphan DidCloseTextDocument: {}", path);
}

if let Some(file_id) = state.vfs.read().0.file_id(&path) {
// Clear diagnostics also for excluded files, just in case.
if let Some((file_id, _)) = state.vfs.read().0.file_id(&path) {
state.diagnostics.clear_native_for(file_id);
}

Expand All @@ -146,7 +156,7 @@ pub(crate) fn handle_did_save_text_document(
) -> anyhow::Result<()> {
if let Ok(vfs_path) = from_proto::vfs_path(&params.text_document.uri) {
let snap = state.snapshot();
let file_id = snap.vfs_path_to_file_id(&vfs_path)?;
let file_id = try_default!(snap.vfs_path_to_file_id(&vfs_path)?);
let sr = snap.analysis.source_root_id(file_id)?;

if state.config.script_rebuild_on_save(Some(sr)) && state.build_deps_changed {
Expand Down Expand Up @@ -290,7 +300,7 @@ fn run_flycheck(state: &mut GlobalState, vfs_path: VfsPath) -> bool {
let _p = tracing::info_span!("run_flycheck").entered();

let file_id = state.vfs.read().0.file_id(&vfs_path);
if let Some(file_id) = file_id {
if let Some((file_id, vfs::FileExcluded::No)) = file_id {
let world = state.snapshot();
let invocation_strategy_once = state.config.flycheck(None).invocation_strategy_once();
let may_flycheck_workspace = state.config.flycheck_workspace(None);
Expand Down
Loading