Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1d26b23
Follow #{track_caller] to something that likely meant to use it
cormacrelf Oct 27, 2025
b7c3b48
Ignore target/ folder for OSS buck2 developers
cormacrelf Oct 30, 2025
d6b44d0
Document why not to use EvalMessage::from_any_error
cormacrelf Oct 27, 2025
8e1e05d
Add note about unlikely wanted behaviour
cormacrelf Nov 2, 2025
7f6e105
Always attach StarlarkContext
cormacrelf Nov 1, 2025
2e497be
Store a real CallStack in StarlarkContext
cormacrelf Nov 1, 2025
ee8b40c
Add a missing enum value to match starlark_syntax::ErrorKind
cormacrelf Nov 3, 2025
3676039
Document recover_crate_error
cormacrelf Nov 3, 2025
ccd8ebc
Rename error_with_starlark_context to inject_starlark_context
cormacrelf Nov 3, 2025
d4747e6
Fix a misplaced backtick
cormacrelf Nov 3, 2025
824ebbd
Add buck2_error::Error::find_starlark_context for tests
cormacrelf Nov 3, 2025
11cbcb4
Make StarlarkContext replace the root error, at format time
cormacrelf Nov 1, 2025
1b0c4c7
Update out-of-date doc strings on buck2_error::ErrorKind
cormacrelf Nov 1, 2025
5d7853c
impl Debug for ContextValue
cormacrelf Nov 1, 2025
c11eca4
Find relevant spans for lsp file in call stacks as well as directly a…
cormacrelf Nov 2, 2025
fb14575
Add show_span_in_buck_output flag on StarlarkContext
cormacrelf Nov 1, 2025
77e2c47
Add a function to create_starlark_context
cormacrelf Nov 1, 2025
4d88c85
Test for formatting starlark errors via buck
cormacrelf Nov 1, 2025
6f03ee0
buck2_error::Error::into_lsp_diagnostic
cormacrelf Nov 1, 2025
12bb357
Integrate buck2_error::Error::into_lsp_diagnostic with LSP
cormacrelf Nov 1, 2025
30db68b
Add test_recover_starlark_span_through_context
cormacrelf Nov 1, 2025
149ad42
Add starlark span to 'From load at BUCK:4'
cormacrelf Nov 1, 2025
6b440bf
Add starlark span to invalid load errors
cormacrelf Nov 2, 2025
f44bb75
Add a test for create_starlark_context
cormacrelf Nov 4, 2025
16d5bb0
Preserve error messages when we concatenate starlark call stacks
cormacrelf Nov 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions app/buck2_error/src/context_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::fmt;
use std::sync::Arc;

use smallvec::SmallVec;
use starlark_syntax::call_stack::CallStack;
use starlark_syntax::codemap::FileSpan;
use starlark_syntax::span_display::span_display;

Expand Down Expand Up @@ -106,22 +107,24 @@ impl<T: TypedContext> From<T> for ContextValue {

#[derive(Clone, allocative::Allocative, Debug, PartialEq, Eq, Hash)]
pub struct StarlarkContext {
// TODO(minglunli): We could take in the CallStack type and do some magic to make it look nicer
// but I think just concatenating the string form and it's accurate and look good enough
pub call_stack: String,
pub call_stack: CallStack,
pub error_msg: String,
pub span: Option<FileSpan>,
}

impl StarlarkContext {
pub fn concat(&self, other: Option<Self>) -> Self {
if let Some(ctx) = other {
let trimmed = ctx
let frames = self
.call_stack
.trim_start_matches(starlark_syntax::call_stack::CALL_STACK_TRACEBACK_PREFIX);
let call_stack = format!("{}{}", self.call_stack, trimmed);
.frames
.iter()
.chain(ctx.call_stack.frames.iter())
.cloned()
Comment on lines +120 to +149
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(nit)

                .frames
                .iter()
                .cloned()
                .chain(ctx.call_stack.frames)

And maybe self by value and you can avoid the remaining clones?

.collect();

Self {
call_stack,
call_stack: CallStack { frames },
error_msg: ctx.error_msg.clone(),
span: ctx.span.clone(),
}
Expand Down
31 changes: 23 additions & 8 deletions app/buck2_error/src/starlark_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ fn from_starlark_impl(
) -> crate::Error {
let starlark_context = if e.has_diagnostic() && !skip_stacktrace {
Some(StarlarkContext {
call_stack: format!("{}", e.call_stack()),
call_stack: e.call_stack().clone(),
error_msg: format!("{}", e.without_diagnostic()),
span: e.span().cloned(),
})
Expand Down Expand Up @@ -232,6 +232,8 @@ mod tests {
use std::sync::Arc;

use allocative::Allocative;
use starlark_syntax::call_stack::CallStack;
use starlark_syntax::frame::Frame;

use crate::any::ProvidableMetadata;
use crate::buck2_error;
Expand Down Expand Up @@ -272,6 +274,15 @@ mod tests {
}
}

fn example_call_stack() -> CallStack {
CallStack {
frames: vec![Frame {
name: "frame".to_owned(),
location: None,
}],
}
}

#[test]
fn test_roundtrip_starlark() {
// Tests buck2_error->starlark->buck2_error
Expand Down Expand Up @@ -312,7 +323,7 @@ mod tests {
let context_key = "Some context key";
let error_tag = crate::ErrorTag::IoWindowsSharingViolation;
let starlark_context = StarlarkContext {
call_stack: "Some call stack".to_owned(),
call_stack: example_call_stack(),
error_msg: "Some error message".to_owned(),
span: None,
};
Expand All @@ -333,9 +344,9 @@ mod tests {
fn test_pop_base_error_from_context() {
let base_error = "Some base error";
let starlark_error = "Starlark error message";
let starlark_call_stack = "Some call stack";
let starlark_call_stack = example_call_stack();
let starlark_context = StarlarkContext {
call_stack: starlark_call_stack.to_owned(),
call_stack: starlark_call_stack.clone(),
error_msg: starlark_error.to_owned(),
span: None,
};
Expand All @@ -345,7 +356,11 @@ mod tests {
let base_replaced = error_with_starlark_context(&e, starlark_context);

assert!(!base_replaced.to_string().contains(base_error));
assert!(base_replaced.to_string().contains(starlark_call_stack));
assert!(
base_replaced
.to_string()
.contains(&starlark_call_stack.to_string())
);
assert!(base_replaced.to_string().contains(starlark_error));
}

Expand All @@ -358,18 +373,18 @@ mod tests {
|| FullMetadataError,
);

let starlark_call_stack = "Some call stack";
let starlark_call_stack = example_call_stack();
let starlark_error_msg = "Starlark error message";
let starlark_context = StarlarkContext {
call_stack: starlark_call_stack.to_owned(),
call_stack: starlark_call_stack.clone(),
error_msg: starlark_error_msg.to_owned(),
span: None,
};

let starlark_err = error_with_starlark_context(&e, starlark_context);
let starlark_err_string = format!("{starlark_err:#}");

assert!(starlark_err_string.contains(starlark_call_stack));
assert!(starlark_err_string.contains(&starlark_call_stack.to_string()));
assert!(starlark_err_string.contains(starlark_error_msg));
// Root error shouldn't be lost
assert!(starlark_err_string.contains(base_error));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ Traceback (most recent call last):
outer_fail()
* outer_import.bzl:4, in outer_fail
outer_rust_failure()

* assert.bzl:3, in <module>
* assert.bzl:3, in outer_rust_failure
should_fail()
* imported.bzl:9, in should_fail
rust_failure()
Expand Down
4 changes: 3 additions & 1 deletion starlark-rust/starlark_syntax/src/call_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ use std::fmt;
use std::fmt::Debug;
use std::fmt::Display;

use allocative::Allocative;

use crate::frame::Frame;

pub const CALL_STACK_TRACEBACK_PREFIX: &str = "Traceback (most recent call last):";

/// Owned call stack.
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Allocative)]
pub struct CallStack {
/// The frames.
pub frames: Vec<Frame>,
Expand Down
4 changes: 3 additions & 1 deletion starlark-rust/starlark_syntax/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ use std::fmt;
use std::fmt::Display;
use std::fmt::Formatter;

use allocative::Allocative;

use crate::codemap::FileSpan;
use crate::fast_string;
use crate::fast_string::CharIndex;

/// A frame of the call-stack.
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
#[derive(Debug, PartialEq, Eq, Hash, Clone, Allocative)]
pub struct Frame {
/// The name of the entry on the call-stack.
pub name: String,
Expand Down