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.ccdiffbeforeafterboth1#include "nix-eval/src/logging.rs"2#include "logging.hh"3#include <nix/util/logging.hh>45using namespace nix;67struct TracingLogger : Logger {8 TracingLogger() {}910 bool isVerbose() override { return true; }11 void log(Verbosity lvl, std::string_view s) override {12 rust::Slice<const unsigned char> str(13 reinterpret_cast<const unsigned char *>(s.data()), s.size());14 emit_log(lvl, str);15 }16 void logEI(const ErrorInfo &ei) override {17 auto s = ei.msg.str();18 rust::Slice<const unsigned char> str(19 reinterpret_cast<const unsigned char *>(s.data()), s.size());20 emit_log(ei.level, str);21 }2223 void startActivity(ActivityId act, Verbosity lvl, ActivityType type,24 const std::string &s, const Fields &fields,25 ActivityId parent) override {26 auto b = new_start_activity(act, lvl, type);27 for (auto &f : fields) {28 if (f.type == Logger::Field::tInt) {29 b->add_int_field(f.i);30 } else if (f.type == Logger::Field::tString) {31 auto s = &f.s;32 rust::Slice<const unsigned char> str(33 reinterpret_cast<const unsigned char *>(s->data()), s->size());34 b->add_string_field(str);35 } else {36 unreachable();37 }38 }39 b->emit(parent, s);40 };4142 void stopActivity(ActivityId act) override { emit_stop(act); };4344 void result(ActivityId act, ResultType type, const Fields &fields) override {45 auto b = new_start_activity(act, 0, type);46 for (auto &f : fields) {47 if (f.type == Logger::Field::tInt) {48 b->add_int_field(f.i);49 } else if (f.type == Logger::Field::tString) {50 auto s = &f.s;51 rust::Slice<const unsigned char> str(52 reinterpret_cast<const unsigned char *>(s->data()), s->size());53 b->add_string_field(str);54 } else {55 unreachable();56 }57 }58 b->emit_result(type);59 };6061 void writeToStdout(std::string_view s) override {62 emit_warn("writeToStdout() called, but unsupported");63 }64 void warn(const std::string &msg) override { emit_warn(msg); }6566 virtual std::optional<char> ask(std::string_view s) {67 emit_warn("ask() called, but unsupported");68 return {};69 }70};7172extern "C" {73void apply_tracing_logger() {74 logger = std::make_unique<TracingLogger>();75 // verbosity = lvlVomit;76}77}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.rsdiffbeforeafterboth--- a/crates/nix-eval/src/logging.rs
+++ b/crates/nix-eval/src/logging.rs
@@ -2,6 +2,7 @@
use std::fmt::Arguments;
use std::sync::{LazyLock, Mutex};
+use cxx::ExternType;
use tracing::{
Level, Span, debug, debug_span, error, error_span, info, info_span, trace, trace_span, warn,
warn_span,
@@ -535,6 +536,45 @@
out.output
}
+#[derive(Debug)]
+pub struct StackFrame {
+ pub msg: String,
+ pub pos: String,
+}
+
+#[derive(Debug)]
+pub struct ErrorInfoBuilder {
+ level: Level,
+ msg: String,
+ pub stack_frames: Vec<StackFrame>,
+}
+fn new_error_info(lvl: u32, v: &[u8]) -> Box<ErrorInfoBuilder> {
+ let verbosity = Verbosity::from_int(lvl);
+ let level: Level = verbosity.into();
+ let v = String::from_utf8_lossy(v);
+ Box::new(ErrorInfoBuilder {
+ level,
+ msg: v.to_string(),
+ stack_frames: Vec::new(),
+ })
+}
+impl ErrorInfoBuilder {
+ fn push_stack_frame(&mut self, v: &[u8], pos: &[u8]) {
+ let v = String::from_utf8_lossy(v);
+ let pos = String::from_utf8_lossy(pos);
+ self.stack_frames.push(StackFrame {
+ msg: v.to_string(),
+ pos: pos.to_string(),
+ });
+ }
+ fn emit_error_info(&mut self) {
+ error!("{}", self.msg);
+ for frame in &self.stack_frames {
+ error!(" {} at {}", frame.msg, frame.pos)
+ }
+ }
+}
+
#[cxx::bridge]
pub mod nix_logging_cxx {
extern "Rust" {
@@ -544,7 +584,14 @@
fn add_string_field(&mut self, v: &[u8]);
fn emit(&mut self, parent: u64, s: &str);
fn emit_result(&mut self, ty: u32);
-
+ }
+ extern "Rust" {
+ type ErrorInfoBuilder;
+ fn new_error_info(lvl: u32, v: &[u8]) -> Box<ErrorInfoBuilder>;
+ fn push_stack_frame(&mut self, v: &[u8], pos: &[u8]);
+ fn emit_error_info(&mut self);
+ }
+ extern "Rust" {
fn emit_warn(v: &str);
fn emit_stop(id: u64);
fn emit_log(lvl: u32, v: &[u8]);
@@ -552,6 +599,15 @@
unsafe extern "C++" {
include!("nix-eval/src/logging.hh");
+ type nix_c_context = crate::nix_raw::c_context;
+
fn apply_tracing_logger();
+ unsafe fn extract_error_info(ctx: *const nix_c_context) -> Box<ErrorInfoBuilder>;
}
}
+
+unsafe impl ExternType for crate::nix_raw::c_context {
+ type Id = cxx::type_id!("nix_c_context");
+
+ type Kind = cxx::kind::Opaque;
+}