Skip to content

Commit

Permalink
chore(core): better debug logging (#4891)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico authored Jan 14, 2025
1 parent d43aa7e commit 4f07cc8
Show file tree
Hide file tree
Showing 42 changed files with 432 additions and 657 deletions.
12 changes: 0 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ syn = "1.0.109"
termcolor = "1.4.1"
terminal_size = "0.4.1"
tokio = "1.42.0"
tracing = { version = "0.1.41", default-features = false, features = ["std"] }
tracing = { version = "0.1.41", default-features = false, features = ["std", "attributes"] }
tracing-subscriber = "0.3.19"
unicode-bom = "2.0.3"
unicode-width = "0.1.12"
Expand Down
32 changes: 32 additions & 0 deletions crates/biome_analyze/src/categories.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use enumflags2::{bitflags, BitFlags};
use std::borrow::Cow;
use std::fmt::{Display, Formatter};

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(
Expand All @@ -23,6 +24,17 @@ pub enum RuleCategory {
Transformation,
}

impl Display for RuleCategory {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
RuleCategory::Syntax => write!(f, "Syntax"),
RuleCategory::Lint => write!(f, "Lint"),
RuleCategory::Action => write!(f, "Action"),
RuleCategory::Transformation => write!(f, "Transformation"),
}
}
}

/// Actions that suppress rules should start with this string
pub const SUPPRESSION_INLINE_ACTION_CATEGORY: &str = "quickfix.suppressRule.inline";
pub const SUPPRESSION_TOP_LEVEL_ACTION_CATEGORY: &str = "quickfix.suppressRule.topLevel";
Expand Down Expand Up @@ -231,6 +243,26 @@ pub(crate) enum Categories {
/// Use [RuleCategoriesBuilder] to generate the categories you want to query.
pub struct RuleCategories(BitFlags<Categories>);

impl Display for RuleCategories {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if self.0.is_empty() {
write!(f, "No categories")
} else {
let mut list = f.debug_list();
if self.0.contains(Categories::Syntax) {
list.entry(&RuleCategory::Syntax);
}
if self.0.contains(Categories::Lint) {
list.entry(&RuleCategory::Lint);
}
if self.0.contains(Categories::Assist) {
list.entry(&RuleCategory::Action);
}
list.finish()
}
}
}

impl RuleCategories {
pub fn empty() -> Self {
let empty: BitFlags<Categories> = BitFlags::empty();
Expand Down
2 changes: 0 additions & 2 deletions crates/biome_analyze/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::cmp::Ordering;
use std::collections::{BTreeMap, BinaryHeap};
use std::fmt::{Debug, Display, Formatter};
use std::ops;
use tracing::trace;

mod analyzer_plugin;
mod categories;
Expand Down Expand Up @@ -293,7 +292,6 @@ where
/// Runs phase 0 over nodes and tokens to process line breaks and
/// suppression comments
fn run_first_phase(mut self) -> ControlFlow<Break> {
trace!("Running first analyzer phase");
let iter = self.root.syntax().preorder_with_tokens(Direction::Next);
for event in iter {
let node_event = match event {
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ bpaf = { workspace = true, features = ["bright-color"] }
camino = { workspace = true }
crossbeam = { workspace = true }
dashmap = { workspace = true }
hdrhistogram = { version = "7.5.4", default-features = false }
path-absolutize = { version = "3.1.1", optional = false, features = ["use_unix_paths_on_wasm"] }
quick-junit = "0.5.1"
rayon = { workspace = true }
Expand All @@ -59,6 +58,7 @@ tracing-appender = "0.2.3"
tracing-subscriber = { workspace = true, features = ["env-filter", "json"] }
tracing-tree = "0.4.0"


[target.'cfg(unix)'.dependencies]
libc = "0.2.169"
tokio = { workspace = true, features = ["process"] }
Expand Down
10 changes: 6 additions & 4 deletions crates/biome_cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use biome_service::{Workspace, WorkspaceError};
use bpaf::Bpaf;
use camino::Utf8PathBuf;
use std::ffi::OsString;
use tracing::info;

pub(crate) mod check;
pub(crate) mod ci;
Expand Down Expand Up @@ -588,10 +589,6 @@ impl BiomeCommand {
}
}

pub const fn has_metrics(&self) -> bool {
false
}

pub fn is_verbose(&self) -> bool {
self.cli_options()
.map_or(false, |cli_options| cli_options.verbose)
Expand Down Expand Up @@ -779,6 +776,11 @@ pub(crate) trait CommandRunner: Sized {
cli_options.verbose,
)?;
}
info!(
"Configuration file loaded: {:?}, diagnostics detected {}",
loaded_configuration.file_path,
loaded_configuration.diagnostics.len(),
);
let configuration_path = loaded_configuration.directory_path.clone();
let configuration = self.merge_configuration(loaded_configuration, fs, console)?;
let vcs_base_path = configuration_path.clone().or(fs.working_directory());
Expand Down
129 changes: 76 additions & 53 deletions crates/biome_cli/src/execute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use biome_service::workspace::{
use camino::{Utf8Path, Utf8PathBuf};
use std::ffi::OsString;
use std::fmt::{Display, Formatter};
use tracing::info;
use tracing::{info, instrument};

/// Useful information during the traversal of files and virtual content
#[derive(Debug, Clone)]
Expand All @@ -45,42 +45,6 @@ pub struct Execution {
max_diagnostics: u32,
}

impl Execution {
pub fn new_format(project_key: ProjectKey, vcs_targeted: VcsTargeted) -> Self {
Self {
traversal_mode: TraversalMode::Format {
project_key,
ignore_errors: false,
write: false,
stdin: None,
vcs_targeted,
},
report_mode: ReportMode::default(),
max_diagnostics: 0,
}
}

pub fn report_mode(&self) -> &ReportMode {
&self.report_mode
}
}

impl Execution {
pub(crate) fn to_feature(&self) -> FeatureName {
match self.traversal_mode {
TraversalMode::Format { .. } => FeaturesBuilder::new().with_formatter().build(),
TraversalMode::Lint { .. } => FeaturesBuilder::new().with_linter().build(),
TraversalMode::Check { .. } | TraversalMode::CI { .. } => FeaturesBuilder::new()
.with_formatter()
.with_linter()
.with_assist()
.build(),
TraversalMode::Migrate { .. } => FeatureName::empty(),
TraversalMode::Search { .. } => FeaturesBuilder::new().with_search().build(),
}
}
}

#[derive(Debug, Clone, Copy)]
pub enum ExecutionEnvironment {
GitHub,
Expand Down Expand Up @@ -390,24 +354,29 @@ impl Execution {
matches!(self.traversal_mode, TraversalMode::Lint { .. })
}

pub(crate) const fn is_check_apply(&self) -> bool {
matches!(
self.traversal_mode,
TraversalMode::Check {
fix_file_mode: Some(FixFileMode::SafeFixes),
..
#[instrument(level = "debug", skip(self), fields(result))]
pub(crate) fn is_safe_fixes_enabled(&self) -> bool {
let result = match self.traversal_mode {
TraversalMode::Check { fix_file_mode, .. } => {
fix_file_mode == Some(FixFileMode::SafeFixes)
}
)
_ => false,
};
tracing::Span::current().record("result", result);
result
}

pub(crate) const fn is_check_apply_unsafe(&self) -> bool {
matches!(
self.traversal_mode,
TraversalMode::Check {
fix_file_mode: Some(FixFileMode::SafeAndUnsafeFixes),
..
#[instrument(level = "debug", skip(self), fields(result))]
pub(crate) fn is_safe_and_unsafe_fixes_enabled(&self) -> bool {
let result = match self.traversal_mode {
TraversalMode::Check { fix_file_mode, .. } => {
fix_file_mode == Some(FixFileMode::SafeAndUnsafeFixes)
}
)
_ => false,
};

tracing::Span::current().record("result", result);
result
}

pub(crate) const fn is_format(&self) -> bool {
Expand Down Expand Up @@ -463,6 +432,60 @@ impl Execution {
TraversalMode::Search { .. } => false,
}
}

pub fn new_format(project_key: ProjectKey, vcs_targeted: VcsTargeted) -> Self {
Self {
traversal_mode: TraversalMode::Format {
project_key,
ignore_errors: false,
write: false,
stdin: None,
vcs_targeted,
},
report_mode: ReportMode::default(),
max_diagnostics: 0,
}
}

pub fn report_mode(&self) -> &ReportMode {
&self.report_mode
}
pub(crate) fn to_feature(&self) -> FeatureName {
match self.traversal_mode {
TraversalMode::Format { .. } => FeaturesBuilder::new().with_formatter().build(),
TraversalMode::Lint { .. } => FeaturesBuilder::new().with_linter().build(),
TraversalMode::Check { .. } | TraversalMode::CI { .. } => FeaturesBuilder::new()
.with_formatter()
.with_linter()
.with_assist()
.build(),
TraversalMode::Migrate { .. } => FeatureName::empty(),
TraversalMode::Search { .. } => FeaturesBuilder::new().with_search().build(),
}
}

#[instrument(level = "debug", skip(self), fields(result))]
pub(crate) fn should_write(&self) -> bool {
let result = match self.traversal_mode {
TraversalMode::Format { write, .. } => write,

_ => self.is_safe_fixes_enabled() || self.is_safe_and_unsafe_fixes_enabled(),
};
tracing::Span::current().record("result", result);
result
}

#[instrument(level = "debug", skip(self), fields(result))]
pub(crate) fn should_ignore_errors(&self) -> bool {
let result = match self.traversal_mode {
TraversalMode::Format { ignore_errors, .. } => ignore_errors,

_ => false,
};
tracing::Span::current().record("result", result);

result
}
}

/// Based on the [mode](TraversalMode), the function might launch a traversal of the file system
Expand Down Expand Up @@ -643,12 +666,12 @@ pub fn execute_mode(
} else if errors > 0 || should_exit_on_warnings {
let category = execution.as_diagnostic_category();
if should_exit_on_warnings {
if execution.is_check_apply() {
if execution.is_safe_fixes_enabled() {
Err(CliDiagnostic::apply_warnings(category))
} else {
Err(CliDiagnostic::check_warnings(category))
}
} else if execution.is_check_apply() {
} else if execution.is_safe_fixes_enabled() {
Err(CliDiagnostic::apply_error(category))
} else {
Err(CliDiagnostic::check_error(category))
Expand Down
27 changes: 11 additions & 16 deletions crates/biome_cli/src/execute/process_file/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use crate::execute::process_file::workspace_file::WorkspaceFile;
use crate::execute::process_file::{
DiffKind, FileResult, FileStatus, Message, SharedTraversalOptions,
};
use crate::execute::TraversalMode;
use biome_analyze::RuleCategoriesBuilder;
use biome_diagnostics::{category, Diagnostic, DiagnosticExt, Error, Severity};
use biome_fs::{BiomePath, TraversalContext};
use biome_service::diagnostics::FileTooLarge;
use biome_service::file_handlers::{AstroFileHandler, SvelteFileHandler, VueFileHandler};
use std::sync::atomic::Ordering;
use tracing::debug;
use tracing::{debug, instrument};

#[instrument(name = "cli_format", level = "debug", skip(ctx, path))]
pub(crate) fn format<'ctx>(
ctx: &'ctx SharedTraversalOptions<'ctx, '_>,
path: BiomePath,
Expand All @@ -30,11 +30,11 @@ pub(crate) fn format<'ctx>(
}
}

#[instrument(level = "debug", skip(ctx, workspace_file))]
pub(crate) fn format_with_guard<'ctx>(
ctx: &'ctx SharedTraversalOptions<'ctx, '_>,
workspace_file: &mut WorkspaceFile,
) -> FileResult {
let _ = tracing::info_span!("Format", path =? workspace_file.path).entered();
let max_diagnostics = ctx.remaining_diagnostics.load(Ordering::Relaxed);
let diagnostics_result = workspace_file
.guard()
Expand All @@ -47,20 +47,11 @@ pub(crate) fn format_with_guard<'ctx>(
.with_file_path_and_code(workspace_file.path.to_string(), category!("format"))?;

let input = workspace_file.input()?;
let (should_write, ignore_errors) = match ctx.execution.traversal_mode {
TraversalMode::Format {
write,
ignore_errors,
..
} => (write, ignore_errors),
let should_write = ctx.execution.should_write();
let ignore_errors = ctx.execution.should_ignore_errors();

_ => (
ctx.execution.is_check_apply() || ctx.execution.is_check_apply_unsafe(),
false,
),
};
debug!("Should write the file to disk? {}", should_write);
debug!("Should ignore errors? {}", ignore_errors);
tracing::Span::current().record("should_write", tracing::field::display(&should_write));
tracing::Span::current().record("ignore_errors", tracing::field::display(&ignore_errors));

if diagnostics_result.errors > 0 && ignore_errors {
return Err(Message::from(
Expand Down Expand Up @@ -119,6 +110,10 @@ pub(crate) fn format_with_guard<'ctx>(
_ => {}
}

debug!(
"Format output is different from intput: {}",
output != input
);
if output != input {
if should_write {
workspace_file.update_file(output)?;
Expand Down
Loading

0 comments on commit 4f07cc8

Please sign in to comment.