Skip to content

Commit bfd3f5f

Browse files
alanzfacebook-github-bot
authored andcommitted
Limit the max size of logged eqwalizer errors
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
1 parent e832fa7 commit bfd3f5f

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

crates/base_db/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,3 +509,20 @@ pub fn to_quoted_string(input: &str) -> Cow<str> {
509509
Cow::Owned(format!("'{}'", &input))
510510
}
511511
}
512+
513+
// ---------------------------------------------------------------------
514+
515+
// We make the limit fairly generous, we hit problems on the other
516+
// side with javascript strings > 512 Mb.
517+
pub const MAX_LOGGED_STRING_LEN: usize = 100_000;
518+
pub fn truncate_string(s: &str, n: usize) -> String {
519+
let chars: Vec<char> = s.chars().collect();
520+
if chars.len() > n {
521+
format!("{}...", chars.iter().take(n).collect::<String>())
522+
} else {
523+
s.to_string()
524+
}
525+
}
526+
pub fn limit_logged_string(s: &str) -> String {
527+
truncate_string(s, MAX_LOGGED_STRING_LEN)
528+
}

crates/eqwalizer/src/ipc.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use std::time::Duration;
2121
use anyhow::bail;
2222
use anyhow::Context;
2323
use anyhow::Result;
24+
use elp_base_db::limit_logged_string;
2425
use elp_types_db::eqwalizer::types::Type;
2526
use elp_types_db::eqwalizer::EqwalizerDiagnostic;
2627
use fxhash::FxHashMap;
@@ -99,7 +100,7 @@ impl IpcHandle {
99100
err, command_str, cmd, &attr
100101
);
101102
// Show up in error log
102-
log::error!("{error_str}");
103+
log::error!("{}", limit_logged_string(&error_str));
103104
// And show up as an eqwalizer error
104105
bail!(error_str);
105106
}

crates/eqwalizer/src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use anyhow::Context;
2121
use anyhow::Result;
2222
use ast::Error;
2323
use ast::Pos;
24+
use elp_base_db::limit_logged_string;
2425
use elp_base_db::ModuleName;
2526
use elp_base_db::ProjectId;
2627
use elp_types_db::eqwalizer::types::Type;
@@ -327,8 +328,8 @@ fn do_typecheck(
327328
}
328329
msg => {
329330
log::warn!(
330-
"received unexpected message from eqwalizer, ignoring: {:?}",
331-
msg
331+
"received unexpected message from eqwalizer, ignoring: {}",
332+
limit_logged_string(&format!("{:?}", msg))
332333
)
333334
}
334335
}
@@ -464,8 +465,8 @@ fn get_module_diagnostics(
464465
}
465466
msg => {
466467
log::warn!(
467-
"received unexpected message from eqwalizer, ignoring: {:?}",
468-
msg
468+
"received unexpected message from eqwalizer, ignoring: {}",
469+
limit_logged_string(&format!("{:?}", msg))
469470
)
470471
}
471472
}

crates/ide_db/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use std::panic::RefUnwindSafe;
1313
use std::path::PathBuf;
1414
use std::sync::Arc;
1515

16+
use elp_base_db::limit_logged_string;
1617
use elp_base_db::salsa;
1718
use elp_base_db::AbsPathBuf;
1819
use elp_base_db::FileId;
@@ -398,7 +399,11 @@ impl TypedSemantic for RootDatabase {
398399
),
399400
EqwalizerDiagnostics::NoAst { .. } => Some(vec![]),
400401
EqwalizerDiagnostics::Error(err) => {
401-
log::error!("EqWAlizer failed for {}: {}", module.as_str(), err);
402+
log::error!(
403+
"EqWAlizer failed for {}: {}",
404+
module.as_str(),
405+
limit_logged_string(err)
406+
);
402407
Some(vec![])
403408
}
404409
}

0 commit comments

Comments
 (0)