[ty] Require that we can find a root when listing sub-modules

This is similar to a change made in the "list top-level modules"
implementation that had been masked by poor Salsa failure modes.
Basically, if we can't find a root here, it *must* be a bug. And if we
just silently skip over it, we risk voiding Salsa's purity contract,
leading to more difficult to debug panics.

This did cause one test to fail, but only because the test wasn't
properly setting up roots.
This commit is contained in:
Andrew Gallant 2025-08-26 14:55:19 -04:00 committed by Andrew Gallant
parent 3b913ce652
commit 9cea752934
4 changed files with 23 additions and 5 deletions

1
Cargo.lock generated
View file

@ -4241,6 +4241,7 @@ name = "ty_ide"
version = "0.0.0"
dependencies = [
"bitflags 2.9.3",
"camino",
"get-size2",
"insta",
"itertools 0.14.0",

View file

@ -33,7 +33,7 @@ smallvec = { workspace = true }
tracing = { workspace = true }
[dev-dependencies]
camino = { workspace = true }
insta = { workspace = true, features = ["filters"] }
[lints]

View file

@ -286,10 +286,12 @@ impl HasNavigationTargets for TypeDefinition<'_> {
#[cfg(test)]
mod tests {
use camino::Utf8Component;
use insta::internals::SettingsBindDropGuard;
use ruff_db::Db;
use ruff_db::diagnostic::{Diagnostic, DiagnosticFormat, DisplayDiagnosticConfig};
use ruff_db::files::{File, system_path_to_file};
use ruff_db::files::{File, FileRootKind, system_path_to_file};
use ruff_db::system::{DbWithWritableSystem, SystemPath, SystemPathBuf};
use ruff_python_trivia::textwrap::dedent;
use ruff_text_size::TextSize;
@ -378,6 +380,19 @@ mod tests {
db.write_file(path, contents)
.expect("write to memory file system to be successful");
// Add a root for the top-most component.
let top = path.components().find_map(|c| match c {
Utf8Component::Normal(c) => Some(c),
_ => None,
});
if let Some(top) = top {
let top = SystemPath::new(top);
if db.system().is_directory(top) {
db.files()
.try_add_root(&db, top, FileRootKind::LibrarySearchPath);
}
}
let file = system_path_to_file(&db, path).expect("newly written file to existing");
if let Some(offset) = cursor_offset {

View file

@ -161,9 +161,11 @@ fn all_submodule_names_for_package(db: &dyn Db, file: File) -> Option<Vec<Name>>
// tree. When the revision gets bumped, the cache
// that Salsa creates does for this routine will be
// invalidated.
if let Some(root) = db.files().root(db, parent_directory) {
let _ = root.revision(db);
}
let root = db
.files()
.root(db, parent_directory)
.expect("System search path should have a registered root");
let _ = root.revision(db);
db.system()
.read_directory(parent_directory)