From 9927c3862715b2d406da327dddc721d51c1bfa11 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Wed, 7 Aug 2024 15:18:04 +0200 Subject: [PATCH] Add some logging messages --- crates/red_knot/src/logging.rs | 11 ++++++----- crates/red_knot/src/main.rs | 1 + crates/red_knot_module_resolver/src/resolver.rs | 10 +++++++++- crates/red_knot_workspace/src/lint.rs | 4 ++-- crates/red_knot_workspace/src/site_packages.rs | 4 ++++ crates/red_knot_workspace/src/watch/watcher.rs | 7 ++++++- crates/red_knot_workspace/src/workspace.rs | 8 ++++++++ crates/ruff_db/src/files/path.rs | 11 +++++++++++ crates/ruff_db/src/vendored/path.rs | 13 +++++++++++++ 9 files changed, 60 insertions(+), 9 deletions(-) diff --git a/crates/red_knot/src/logging.rs b/crates/red_knot/src/logging.rs index 7a9c796b5e107b..c3266b32a6afd5 100644 --- a/crates/red_knot/src/logging.rs +++ b/crates/red_knot/src/logging.rs @@ -42,16 +42,16 @@ impl Verbosity { #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)] pub(crate) enum VerbosityLevel { - /// Default output level. Only shows ruff and red knot events up to the [`LogLevel::Warn`]. + /// Default output level. Only shows Ruff and Red Knot events up to the [`WARN`](tracing::Level::WARN). Default, - /// Enables verbose output. Emits ruff and red knot events up to the [`LogLevel::Info`] + /// Enables verbose output. Emits Ruff and Red Knot events up to the [`INFO`](tracing::Level::INFO) Verbose, - /// Enables a more verbose tracing format and emits ruff and red_knot events up to [`LogLevel::Debug`] Corresponds to `-vv` + /// Enables a more verbose tracing format and emits Ruff and Red Knot events up to [`DEBUG`](tracing::Level::DEBUG) Corresponds to `-vv` ExtraVerbose, - /// Enables all tracing events and uses a tree like output format. Corresponds to `-vvv`. + /// Enables all tracing events and uses a tree-like output format. Corresponds to `-vvv`. Trace, } @@ -145,6 +145,7 @@ pub(crate) fn setup_tracing(level: VerbosityLevel) -> anyhow::Result() -> ( Option>>, Option>>, @@ -208,7 +209,7 @@ where let formatted_level = level.to_string(); match *level { tracing::Level::TRACE => { - write!(writer, "{} ", formatted_level.purple().bold())? + write!(writer, "{} ", formatted_level.purple().bold())?; } tracing::Level::DEBUG => write!(writer, "{} ", formatted_level.blue().bold())?, tracing::Level::INFO => write!(writer, "{} ", formatted_level.green().bold())?, diff --git a/crates/red_knot/src/main.rs b/crates/red_knot/src/main.rs index 436ee52a947651..ffb7b6b174e720 100644 --- a/crates/red_knot/src/main.rs +++ b/crates/red_knot/src/main.rs @@ -217,6 +217,7 @@ impl MainLoop { let is_trace = self.verbosity.is_trace(); self.run(db); + #[allow(clippy::print_stderr)] if is_trace { eprintln!("Exit"); eprintln!("{}", countme::get_all()); diff --git a/crates/red_knot_module_resolver/src/resolver.rs b/crates/red_knot_module_resolver/src/resolver.rs index 14a7c826db2b9d..5833da5c5a472b 100644 --- a/crates/red_knot_module_resolver/src/resolver.rs +++ b/crates/red_knot_module_resolver/src/resolver.rs @@ -32,10 +32,18 @@ pub(crate) fn resolve_module_query<'db>( let name = module_name.name(db); let _span = tracing::trace_span!("resolve_module", %name).entered(); - let (search_path, module_file, kind) = resolve_name(db, name)?; + let Some((search_path, module_file, kind)) = resolve_name(db, name) else { + tracing::debug!("Module '{name}' not found in the search paths."); + return None; + }; let module = Module::new(name.clone(), kind, search_path, module_file); + tracing::debug!( + "Resolved module '{name}' to '{path}'.", + path = module_file.path(db) + ); + Some(module) } diff --git a/crates/red_knot_workspace/src/lint.rs b/crates/red_knot_workspace/src/lint.rs index 812f2c06128866..d7bdb09db28894 100644 --- a/crates/red_knot_workspace/src/lint.rs +++ b/crates/red_knot_workspace/src/lint.rs @@ -2,7 +2,7 @@ use std::cell::RefCell; use std::ops::Deref; use std::time::Duration; -use tracing::trace_span; +use tracing::debug_span; use red_knot_module_resolver::ModuleName; use red_knot_python_semantic::types::Type; @@ -76,7 +76,7 @@ fn lint_lines(source: &str, diagnostics: &mut Vec) { #[allow(unreachable_pub)] #[salsa::tracked(return_ref)] pub fn lint_semantic(db: &dyn Db, file_id: File) -> Diagnostics { - let _span = trace_span!("lint_semantic", file=?file_id.path(db)).entered(); + let _span = debug_span!("lint_semantic", file=?file_id.path(db)).entered(); let source = source_text(db.upcast(), file_id); let parsed = parsed_module(db.upcast(), file_id); diff --git a/crates/red_knot_workspace/src/site_packages.rs b/crates/red_knot_workspace/src/site_packages.rs index f076e1675a38f6..aecd0a8fdac1ae 100644 --- a/crates/red_knot_workspace/src/site_packages.rs +++ b/crates/red_knot_workspace/src/site_packages.rs @@ -117,6 +117,10 @@ fn site_packages_dir_from_sys_prefix( let site_packages_candidate = path.join("site-packages"); if system.is_directory(&site_packages_candidate) { + tracing::debug!( + "Resoled site-packages directory: {}", + site_packages_candidate + ); return Ok(site_packages_candidate); } } diff --git a/crates/red_knot_workspace/src/watch/watcher.rs b/crates/red_knot_workspace/src/watch/watcher.rs index 61205530a0544c..ff3e01009799bf 100644 --- a/crates/red_knot_workspace/src/watch/watcher.rs +++ b/crates/red_knot_workspace/src/watch/watcher.rs @@ -109,6 +109,8 @@ struct WatcherInner { impl Watcher { /// Sets up file watching for `path`. pub fn watch(&mut self, path: &SystemPath) -> notify::Result<()> { + tracing::debug!("Watching path: {path}."); + self.inner_mut() .watcher .watch(path.as_std_path(), RecursiveMode::Recursive) @@ -116,6 +118,8 @@ impl Watcher { /// Stops file watching for `path`. pub fn unwatch(&mut self, path: &SystemPath) -> notify::Result<()> { + tracing::debug!("Unwatching path: {path}."); + self.inner_mut().watcher.unwatch(path.as_std_path()) } @@ -125,6 +129,7 @@ impl Watcher { /// /// The call blocks until the watcher has stopped. pub fn stop(mut self) { + tracing::debug!("Stop file watcher"); self.set_stop(); } @@ -173,8 +178,8 @@ struct Debouncer { } impl Debouncer { - #[tracing::instrument(level = "trace", skip(self))] fn add_result(&mut self, result: notify::Result) { + tracing::trace!("Handling file watcher event: {result:?}."); match result { Ok(event) => self.add_event(event), Err(error) => self.add_error(error), diff --git a/crates/red_knot_workspace/src/workspace.rs b/crates/red_knot_workspace/src/workspace.rs index 584eae83dae165..672524f400586b 100644 --- a/crates/red_knot_workspace/src/workspace.rs +++ b/crates/red_knot_workspace/src/workspace.rs @@ -176,6 +176,8 @@ impl Workspace { /// Checks all open files in the workspace and its dependencies. #[tracing::instrument(level = "debug", skip_all)] pub fn check(self, db: &dyn Db) -> Vec { + tracing::debug!("Checking workspace"); + let mut result = Vec::new(); if let Some(open_files) = self.open_files(db) { @@ -274,6 +276,8 @@ impl Package { #[tracing::instrument(level = "debug", skip(db))] pub(crate) fn check(self, db: &dyn Db) -> Vec { + tracing::debug!("Checking package {}", self.root(db)); + let mut result = Vec::new(); for file in &self.files(db).read() { let diagnostics = check_file(db, file); @@ -327,6 +331,10 @@ impl Package { } pub(super) fn check_file(db: &dyn Db, file: File) -> Diagnostics { + let path = file.path(db); + let _span = tracing::debug_span!("check_file", path = ?path).entered(); + tracing::debug!("Checking file {path}"); + let mut diagnostics = Vec::new(); diagnostics.extend_from_slice(lint_syntax(db, file)); diagnostics.extend_from_slice(lint_semantic(db, file)); diff --git a/crates/ruff_db/src/files/path.rs b/crates/ruff_db/src/files/path.rs index 816eaf461a3db0..fddac0fa226fc4 100644 --- a/crates/ruff_db/src/files/path.rs +++ b/crates/ruff_db/src/files/path.rs @@ -2,6 +2,7 @@ use crate::files::{system_path_to_file, vendored_path_to_file, File}; use crate::system::{SystemPath, SystemPathBuf, SystemVirtualPath, SystemVirtualPathBuf}; use crate::vendored::{VendoredPath, VendoredPathBuf}; use crate::Db; +use std::fmt::{Display, Formatter}; /// Path to a file. /// @@ -209,3 +210,13 @@ impl PartialEq for VendoredPathBuf { other == self } } + +impl Display for FilePath { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self { + FilePath::System(path) => std::fmt::Display::fmt(path, f), + FilePath::SystemVirtual(path) => std::fmt::Display::fmt(path, f), + FilePath::Vendored(path) => std::fmt::Display::fmt(path, f), + } + } +} diff --git a/crates/ruff_db/src/vendored/path.rs b/crates/ruff_db/src/vendored/path.rs index 7144ae5a3df371..3fb154e171e00d 100644 --- a/crates/ruff_db/src/vendored/path.rs +++ b/crates/ruff_db/src/vendored/path.rs @@ -1,4 +1,5 @@ use std::borrow::Borrow; +use std::fmt::Formatter; use std::ops::Deref; use std::path; @@ -197,3 +198,15 @@ impl TryFrom for VendoredPathBuf { Ok(VendoredPathBuf(camino::Utf8PathBuf::try_from(value)?)) } } + +impl std::fmt::Display for VendoredPath { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + std::fmt::Display::fmt(&self.0, f) + } +} + +impl std::fmt::Display for VendoredPathBuf { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + std::fmt::Display::fmt(self.as_path(), f) + } +}