Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(error-tracking): reverse ruby pre_context before assigning line offsets (#63110)

Hugues Pouillot7559fd0dcbe8
diff --git a/rust/cymbal/src/langs/ruby.rs b/rust/cymbal/src/langs/ruby.rs
index dcc29049c474..b41ba51ae7c7 100644
--- a/rust/cymbal/src/langs/ruby.rs
+++ b/rust/cymbal/src/langs/ruby.rs
@@ -56,6 +56,7 @@ impl RawRubyFrame {
let before = self
.pre_context
.iter()
+ .rev() // pre_context arrives in file order, newest line last
.enumerate()
.map(|(i, line)| ContextLine::new_rel(lineno, -(i as i32) - 1, line.clone()))
.collect();
@@ -97,3 +98,62 @@ impl From<&RawRubyFrame> for Frame {
}
}
}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+
+ fn frame(lineno: u32, pre_context: &[&str], post_context: &[&str]) -> RawRubyFrame {
+ RawRubyFrame {
+ path: None,
+ context_line: Some(format!("line {lineno}")),
+ filename: "app.rb".to_string(),
+ function: "call".to_string(),
+ lineno: Some(lineno),
+ pre_context: pre_context.iter().map(|s| s.to_string()).collect(),
+ post_context: post_context.iter().map(|s| s.to_string()).collect(),
+ meta: CommonFrameMetadata::default(),
+ }
+ }
+
+ #[test]
+ fn test_get_context_pairs_lines_with_numbers() {
+ // pre/post_context arrive in file order; before is emitted
+ // nearest-line-first and the frontend sorts by line number
+ type Case = (u32, &'static [&'static str], &'static [(u32, &'static str)]);
+ let cases: &[Case] = &[
+ (
+ 10,
+ &["line 7", "line 8", "line 9"],
+ &[(9, "line 9"), (8, "line 8"), (7, "line 7")],
+ ),
+ // SDK omitted context lines entirely
+ (10, &[], &[]),
+ // more pre-context lines than lines above lineno: offsets saturate at 0
+ (2, &["a", "b", "c"], &[(1, "c"), (0, "b"), (0, "a")]),
+ ];
+
+ for (lineno, pre, expected_before) in cases {
+ let context = frame(*lineno, pre, &["next 1", "next 2"])
+ .get_context()
+ .unwrap();
+
+ assert_eq!(
+ context.line,
+ ContextLine::new(*lineno, format!("line {lineno}"))
+ );
+ let expected: Vec<ContextLine> = expected_before
+ .iter()
+ .map(|(number, line)| ContextLine::new(*number, *line))
+ .collect();
+ assert_eq!(context.before, expected);
+ assert_eq!(
+ context.after,
+ vec![
+ ContextLine::new(lineno + 1, "next 1"),
+ ContextLine::new(lineno + 2, "next 2"),
+ ]
+ );
+ }
+ }
+}