difftreelog
feat display nix stacktraces
in: trunk
6 files changed
cmds/fleet/src/cmds/build_systems.rsdiffbeforeafterboth--- a/cmds/fleet/src/cmds/build_systems.rs
+++ b/cmds/fleet/src/cmds/build_systems.rs
@@ -118,7 +118,7 @@
{
Ok(path) => path,
Err(e) => {
- error!("failed to build host system closure: {:#}", e);
+ error!("failed to build host system closure: {:?}", e);
return;
}
};
crates/nix-eval/build.rsdiffbeforeafterboth--- a/crates/nix-eval/build.rs
+++ b/crates/nix-eval/build.rs
@@ -16,6 +16,7 @@
// Link nix C++ libraries for cxx
for lib in &[
"nix-util",
+ "nix-util-c",
"nix-store",
"nix-expr",
"nix-flake",
@@ -34,12 +35,12 @@
cxx_build::bridge("src/logging.rs")
.file("src/logging.cc")
- .std("c++20")
+ .std("c++23")
.shared_flag(true)
.compile("nix-eval-logging");
cxx_build::bridge("src/lib.rs")
.file("src/lib.cc")
- .std("c++20")
+ .std("c++23")
.shared_flag(true)
.compile("nix-eval");
crates/nix-eval/src/lib.rsdiffbeforeafterboth--- a/crates/nix-eval/src/lib.rs
+++ b/crates/nix-eval/src/lib.rs
@@ -13,7 +13,7 @@
pub use anyhow::Result;
use tracing::instrument;
-use self::logging::nix_logging_cxx;
+use self::logging::{ErrorInfoBuilder, nix_logging_cxx};
use self::nix_cxx::set_fetcher_setting;
use self::nix_raw::{
BindingsBuilder as c_bindings_builder, EvalState as c_eval_state, GC_SUCCESS,
@@ -179,8 +179,9 @@
let code = unsafe { err_code(self.0) };
NixErrorKind::from_int(code)
}
- fn error<'t>(&self) -> Option<Cow<'t, str>> {
+ fn error<'t>(&self) -> Option<(Cow<'t, str>, Option<Box<ErrorInfoBuilder>>)> {
if let NixErrorKind::Generic = self.error_kind()? {
+ let ei = unsafe { logging::nix_logging_cxx::extract_error_info(self.0) };
let mut err_out = String::new();
unsafe {
err_info_msg(
@@ -190,13 +191,13 @@
(&raw mut err_out).cast(),
)
};
- return Some(Cow::Owned(err_out));
+ return Some((Cow::Owned(err_out), Some(ei)));
};
// TODO: Can throw error (resulting in panic) if unable to retrieve error. Should be able to resolve by passing context as a first argument,
// but it looks ugly
let str = unsafe { err_msg(null_mut(), self.0, null_mut()) };
- Some(unsafe { CStr::from_ptr(str) }.to_string_lossy())
+ Some((unsafe { CStr::from_ptr(str) }.to_string_lossy(), None))
}
fn clean_err(&mut self) {
unsafe {
@@ -205,8 +206,20 @@
}
fn bail_if_error(&self) -> Result<()> {
- if let Some(err) = self.error() {
- bail!("{err}");
+ if let Some((err, stack)) = self.error() {
+ let mut e = Err(anyhow!("{err}"));
+ if let Some(stack) = stack {
+ for ele in stack.stack_frames {
+ e = e.with_context(|| {
+ if ele.pos.is_empty() {
+ ele.msg
+ } else {
+ format!("{} at {}", ele.msg, ele.pos)
+ }
+ })
+ }
+ }
+ return e.context("<nix frames>");
};
Ok(())
}
crates/nix-eval/src/logging.ccdiffbeforeafterboth--- a/crates/nix-eval/src/logging.cc
+++ b/crates/nix-eval/src/logging.cc
@@ -1,9 +1,36 @@
-#include "nix-eval/src/logging.rs"
#include "logging.hh"
#include <nix/util/logging.hh>
+#include <nix/util/position.hh>
using namespace nix;
+rust::Box<ErrorInfoBuilder> copy_error_info(const ErrorInfo &ei) {
+ auto s = ei.msg.str();
+ rust::Slice<const unsigned char> str(
+ reinterpret_cast<const unsigned char *>(s.data()), s.size());
+ auto b = new_error_info(ei.level, str);
+ if (!ei.traces.empty()) {
+ for (auto iter = ei.traces.rbegin(); iter != ei.traces.rend(); ++iter) {
+ auto msg = iter->hint.str();
+
+ rust::Slice<const unsigned char> msgv(
+ reinterpret_cast<const unsigned char *>(msg.data()), msg.size());
+
+ std::ostringstream oss;
+ if (iter->pos) {
+ iter->pos->print(oss, true);
+ }
+ std::string pos = oss.str();
+
+ rust::Slice<const unsigned char> posv(
+ reinterpret_cast<const unsigned char *>(pos.data()), pos.size());
+
+ b->push_stack_frame(msgv, posv);
+ }
+ }
+ return b;
+}
+
struct TracingLogger : Logger {
TracingLogger() {}
@@ -14,10 +41,8 @@
emit_log(lvl, str);
}
void logEI(const ErrorInfo &ei) override {
- auto s = ei.msg.str();
- rust::Slice<const unsigned char> str(
- reinterpret_cast<const unsigned char *>(s.data()), s.size());
- emit_log(ei.level, str);
+ auto b = copy_error_info(ei);
+ b->emit_error_info();
}
void startActivity(ActivityId act, Verbosity lvl, ActivityType type,
@@ -74,4 +99,8 @@
logger = std::make_unique<TracingLogger>();
// verbosity = lvlVomit;
}
+rust::Box<ErrorInfoBuilder>
+extract_error_info(const nix_c_context *read_context) {
+ return copy_error_info(read_context->info.value());
+}
}
crates/nix-eval/src/logging.hhdiffbeforeafterboth--- a/crates/nix-eval/src/logging.hh
+++ b/crates/nix-eval/src/logging.hh
@@ -1,5 +1,12 @@
#pragma once
+#include "nix-eval/src/logging.rs"
+#include "rust/cxx.h"
+#include <nix_api_util.h>
+#include <nix_api_util_internal.h>
+
+struct ErrorInfoBuilder;
extern "C" {
void apply_tracing_logger();
+rust::Box<ErrorInfoBuilder> extract_error_info(const nix_c_context *ctx);
}
crates/nix-eval/src/logging.rsdiffbeforeafterboth2use std::fmt::Arguments;2use std::fmt::Arguments;3use std::sync::{LazyLock, Mutex};3use std::sync::{LazyLock, Mutex};445use cxx::ExternType;5use tracing::{6use tracing::{6 Level, Span, debug, debug_span, error, error_span, info, info_span, trace, trace_span, warn,7 Level, Span, debug, debug_span, error, error_span, info, info_span, trace, trace_span, warn,7 warn_span,8 warn_span,535 out.output536 out.output536}537}538539#[derive(Debug)]540pub struct StackFrame {541 pub msg: String,542 pub pos: String,543}544545#[derive(Debug)]546pub struct ErrorInfoBuilder {547 level: Level,548 msg: String,549 pub stack_frames: Vec<StackFrame>,550}551fn new_error_info(lvl: u32, v: &[u8]) -> Box<ErrorInfoBuilder> {552 let verbosity = Verbosity::from_int(lvl);553 let level: Level = verbosity.into();554 let v = String::from_utf8_lossy(v);555 Box::new(ErrorInfoBuilder {556 level,557 msg: v.to_string(),558 stack_frames: Vec::new(),559 })560}561impl ErrorInfoBuilder {562 fn push_stack_frame(&mut self, v: &[u8], pos: &[u8]) {563 let v = String::from_utf8_lossy(v);564 let pos = String::from_utf8_lossy(pos);565 self.stack_frames.push(StackFrame {566 msg: v.to_string(),567 pos: pos.to_string(),568 });569 }570 fn emit_error_info(&mut self) {571 error!("{}", self.msg);572 for frame in &self.stack_frames {573 error!(" {} at {}", frame.msg, frame.pos)574 }575 }576}537577538#[cxx::bridge]578#[cxx::bridge]539pub mod nix_logging_cxx {579pub mod nix_logging_cxx {540 extern "Rust" {580 extern "Rust" {541 type StartActivityBuilder;581 type StartActivityBuilder;542 fn new_start_activity(activity_id: u64, lvl: u32, typ: u32) -> Box<StartActivityBuilder>;582 fn new_start_activity(activity_id: u64, lvl: u32, typ: u32) -> Box<StartActivityBuilder>;543 fn add_int_field(&mut self, i: i32);583 fn add_int_field(&mut self, i: i32);544 fn add_string_field(&mut self, v: &[u8]);584 fn add_string_field(&mut self, v: &[u8]);545 fn emit(&mut self, parent: u64, s: &str);585 fn emit(&mut self, parent: u64, s: &str);546 fn emit_result(&mut self, ty: u32);586 fn emit_result(&mut self, ty: u32);547587 }588 extern "Rust" {589 type ErrorInfoBuilder;590 fn new_error_info(lvl: u32, v: &[u8]) -> Box<ErrorInfoBuilder>;591 fn push_stack_frame(&mut self, v: &[u8], pos: &[u8]);592 fn emit_error_info(&mut self);593 }594 extern "Rust" {548 fn emit_warn(v: &str);595 fn emit_warn(v: &str);549 fn emit_stop(id: u64);596 fn emit_stop(id: u64);550 fn emit_log(lvl: u32, v: &[u8]);597 fn emit_log(lvl: u32, v: &[u8]);551 }598 }552 unsafe extern "C++" {599 unsafe extern "C++" {553 include!("nix-eval/src/logging.hh");600 include!("nix-eval/src/logging.hh");601602 type nix_c_context = crate::nix_raw::c_context;554603555 fn apply_tracing_logger();604 fn apply_tracing_logger();605 unsafe fn extract_error_info(ctx: *const nix_c_context) -> Box<ErrorInfoBuilder>;556 }606 }557}607}608609unsafe impl ExternType for crate::nix_raw::c_context {610 type Id = cxx::type_id!("nix_c_context");611612 type Kind = cxx::kind::Opaque;613}558614