Skip to content

Commit c548ef2

Browse files
committed
[ty] Squash false positive logs for failing to find builtins as a real module
I recently started noticing this showing up in the logs for every scope based completion request: ``` 2025-12-11 11:25:35.704329935 DEBUG request{id=29 method="textDocument/completion"}:map_stub_definition: Module `builtins` not found while looking in parent dirs ``` And in particular, it was repeated several times. This was confusing to me because, well, of course `builtins` should resolve. This particular code path comes from looking for the docstrings of completion items. This involves a spelunking that ultimately tries to resolve a "real" module if the stub doesn't have available docstrings. But I guess there is no "real" `builtins` module, so `resolve_real_module` fails. Which is fine, but the noisy logs were annoying since this is an expected case. So here, we carve out a short circuit for `builtins` and also improve the log message.
1 parent 5a9d6a9 commit c548ef2

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

crates/ty_python_semantic/src/module_resolver/resolver.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,14 @@ fn desperately_resolve_module<'db>(
265265
let _span = tracing::trace_span!("desperately_resolve_module", %name).entered();
266266

267267
let Some(resolved) = desperately_resolve_name(db, importing_file, name, mode) else {
268-
tracing::debug!("Module `{name}` not found while looking in parent dirs");
268+
let extra = match module_name.mode(db) {
269+
ModuleResolveMode::StubsAllowed => "neither stub nor real module file",
270+
ModuleResolveMode::StubsNotAllowed => "stubs not allowed",
271+
ModuleResolveMode::StubsNotAllowedSomeShadowingAllowed => {
272+
"stubs not allowed but some shadowing allowed"
273+
}
274+
};
275+
tracing::debug!("Module `{name}` not found while looking in parent dirs ({extra})");
269276
return None;
270277
};
271278

crates/ty_python_semantic/src/types/ide_support.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,7 @@ mod resolve_definition {
843843
use ruff_db::system::SystemPath;
844844
use ruff_db::vendored::VendoredPathBuf;
845845
use ruff_python_ast as ast;
846+
use ruff_python_stdlib::sys::is_builtin_module;
846847
use rustc_hash::FxHashSet;
847848
use tracing::trace;
848849

@@ -1160,6 +1161,14 @@ mod resolve_definition {
11601161
// here because there isn't really an importing file. However this `resolve_real_module`
11611162
// can be understood as essentially `import .`, which is also what `file_to_module` is,
11621163
// so this is in fact exactly the file we want to consider the importer.
1164+
//
1165+
// ... unless we have a builtin module. i.e., A module embedded
1166+
// into the interpreter. In which case, all we have are stubs.
1167+
// `resolve_real_module` will always return `None` for this case, but
1168+
// it will emit false positive logs. And this saves us some work.
1169+
if is_builtin_module(db.python_version().minor, stub_module.name(db)) {
1170+
return None;
1171+
}
11631172
let real_module =
11641173
resolve_real_module(db, stub_file_for_module_lookup, stub_module.name(db))?;
11651174
trace!("Found real module: {}", real_module.name(db));

0 commit comments

Comments
 (0)