Skip to content

Commit

Permalink
fix: Broken root finder, prevent panics
Browse files Browse the repository at this point in the history
  • Loading branch information
Desdaemon committed Aug 6, 2024
1 parent 3961eec commit a16fda9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct Backend {
pub record_ranges: DashMap<String, Box<[ByteRange]>>,
pub ast_map: DashMap<String, Tree>,
pub index: Index,
/// Roots added during initial setup. Not to be confused with [Index::roots].
pub roots: DashSet<PathBuf>,
pub capabilities: Capabilities,
pub root_setup: CondVar,
Expand Down Expand Up @@ -97,8 +98,9 @@ impl Backend {
/// Maximum file line count to process diagnostics each on_change
pub const DIAGNOSTICS_LINE_LIMIT: usize = 1200;

#[tracing::instrument(skip_all, ret)]
pub fn find_root_of(&self, path: &Path) -> Option<PathBuf> {
for root_ in self.roots.iter() {
for root_ in self.index.roots.iter() {
if path.starts_with(root_.key()) {
return Some(root_.key().to_owned());
}
Expand Down
17 changes: 12 additions & 5 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ enum Output {
Components(HashMap<ComponentName, Component>),
}

#[derive(Debug)]
pub struct AddRootResults {
pub module_count: usize,
pub record_count: usize,
Expand All @@ -138,6 +139,7 @@ impl Index {
self.records.retain(|_, record| !record.deleted);
info!("(mark_n_sweep) deleted {} records", pre - self.records.len());
}
#[tracing::instrument(skip_all, fields(root=format!("{}", root.display())), ret)]
pub async fn add_root(
&self,
root: &Path,
Expand Down Expand Up @@ -172,11 +174,16 @@ impl Index {
let interner = interner();
let root_key = interner.get_or_intern(root.to_string_lossy());
for manifest in manifests {
let manifest = manifest.into_diagnostic()?;
let module_dir = manifest
.path()
.parent()
.ok_or_else(|| miette::diagnostic!("Unexpected empty path"))?;
let manifest = match manifest {
Ok(manifest) => manifest,
Err(err) => {
warn!(err = %err, "error traversing manifest");
continue;
}
};
let Some(module_dir) = manifest.path().parent() else {
continue;
};
fn matched_top_to_bottom(gitignore: &Gitignore, path: &Path) -> bool {
let ancestors = path.ancestors().collect::<Vec<_>>();
for ancestor in ancestors.into_iter().rev() {
Expand Down
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ impl LanguageServer for Backend {
self.capabilities.pull_diagnostics.store(true, Relaxed);
}


Ok(InitializeResult {
server_info: None,
offset_encoding: None,
Expand Down Expand Up @@ -349,7 +348,7 @@ impl LanguageServer for Backend {
.await
.unwrap_or(false)
{
if let Some(file_path) = path_.parent() {
if let Some(file_path) = path_.parent().and_then(|p| p.parent()) {
_ = self
.index
.add_root(file_path, None, false)
Expand Down
11 changes: 10 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ impl<T> std::ops::Deref for MaxVec<T> {

pub trait TryResultExt {
type Result: Sized;
/// Panics if this is [`TryResult::Locked`].
fn expect(self, msg: &str) -> Option<Self::Result>;
}

Expand All @@ -370,12 +371,20 @@ pub fn init_for_test() {
.init();
}

#[derive(Default)]
pub struct CondVar {
should_wait: AtomicBool,
notifier: tokio::sync::Notify,
}

impl Default for CondVar {
fn default() -> Self {
Self {
should_wait: AtomicBool::new(true),
notifier: Default::default(),
}
}
}

pub struct Blocker<'a>(&'a CondVar);

impl CondVar {
Expand Down

0 comments on commit a16fda9

Please sign in to comment.