Skip to content

Commit

Permalink
Limit the max size of logged eqwalizer errors
Browse files Browse the repository at this point in the history
Summary:
When people turn on debug logging, it can generate strings that are too long for VS Code to handle, > 512MB.

These typically originate from eqwalizer, when it returns a problem.

Add function to limit a string length for logging, and apply it to possible origins in Eqwalizer ELP code.

There are two variants of the limiting function, one takes a max length, the other uses a constant value.

Reviewed By: VLanvin

Differential Revision: D69311050

fbshipit-source-id: 53188669f2096f25a5e5809a7e49cdc807cdc96f
  • Loading branch information
alanz authored and facebook-github-bot committed Feb 10, 2025
1 parent e832fa7 commit bfd3f5f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
17 changes: 17 additions & 0 deletions crates/base_db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,3 +509,20 @@ pub fn to_quoted_string(input: &str) -> Cow<str> {
Cow::Owned(format!("'{}'", &input))
}
}

// ---------------------------------------------------------------------

// We make the limit fairly generous, we hit problems on the other
// side with javascript strings > 512 Mb.
pub const MAX_LOGGED_STRING_LEN: usize = 100_000;
pub fn truncate_string(s: &str, n: usize) -> String {
let chars: Vec<char> = s.chars().collect();
if chars.len() > n {
format!("{}...", chars.iter().take(n).collect::<String>())
} else {
s.to_string()
}
}
pub fn limit_logged_string(s: &str) -> String {
truncate_string(s, MAX_LOGGED_STRING_LEN)
}
3 changes: 2 additions & 1 deletion crates/eqwalizer/src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::time::Duration;
use anyhow::bail;
use anyhow::Context;
use anyhow::Result;
use elp_base_db::limit_logged_string;
use elp_types_db::eqwalizer::types::Type;
use elp_types_db::eqwalizer::EqwalizerDiagnostic;
use fxhash::FxHashMap;
Expand Down Expand Up @@ -99,7 +100,7 @@ impl IpcHandle {
err, command_str, cmd, &attr
);
// Show up in error log
log::error!("{error_str}");
log::error!("{}", limit_logged_string(&error_str));
// And show up as an eqwalizer error
bail!(error_str);
}
Expand Down
9 changes: 5 additions & 4 deletions crates/eqwalizer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use anyhow::Context;
use anyhow::Result;
use ast::Error;
use ast::Pos;
use elp_base_db::limit_logged_string;
use elp_base_db::ModuleName;
use elp_base_db::ProjectId;
use elp_types_db::eqwalizer::types::Type;
Expand Down Expand Up @@ -327,8 +328,8 @@ fn do_typecheck(
}
msg => {
log::warn!(
"received unexpected message from eqwalizer, ignoring: {:?}",
msg
"received unexpected message from eqwalizer, ignoring: {}",
limit_logged_string(&format!("{:?}", msg))
)
}
}
Expand Down Expand Up @@ -464,8 +465,8 @@ fn get_module_diagnostics(
}
msg => {
log::warn!(
"received unexpected message from eqwalizer, ignoring: {:?}",
msg
"received unexpected message from eqwalizer, ignoring: {}",
limit_logged_string(&format!("{:?}", msg))
)
}
}
Expand Down
7 changes: 6 additions & 1 deletion crates/ide_db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::panic::RefUnwindSafe;
use std::path::PathBuf;
use std::sync::Arc;

use elp_base_db::limit_logged_string;
use elp_base_db::salsa;
use elp_base_db::AbsPathBuf;
use elp_base_db::FileId;
Expand Down Expand Up @@ -398,7 +399,11 @@ impl TypedSemantic for RootDatabase {
),
EqwalizerDiagnostics::NoAst { .. } => Some(vec![]),
EqwalizerDiagnostics::Error(err) => {
log::error!("EqWAlizer failed for {}: {}", module.as_str(), err);
log::error!(
"EqWAlizer failed for {}: {}",
module.as_str(),
limit_logged_string(err)
);
Some(vec![])
}
}
Expand Down

0 comments on commit bfd3f5f

Please sign in to comment.