Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GEN-115: Don't compile backtrace support if feature is disabled #4685

Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
9 changes: 4 additions & 5 deletions libs/error-stack/.justfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,24 @@ set fallback
default:
@just usage

# TODO: add `--ignore-unknown-features` to `cargo hack` and pass `--workspace`
cargo-hack-groups := '--group-features eyre,hooks --group-features anyhow,serde'
profile := env_var_or_default('PROFILE', "dev")
repo := `git rev-parse --show-toplevel`

[private]
clippy *arguments:
@just install-cargo-hack
@just install-rust-script

@CLIPPY_CONF_DIR=../../.config just in-pr cargo clippy --profile {{profile}} --workspace --all-features --all-targets --no-deps {{arguments}}
@CLIPPY_CONF_DIR=../../.config just not-in-pr cargo hack --optional-deps --feature-powerset {{cargo-hack-groups}} --ignore-unknown-features clippy --profile {{profile}} --all-targets --no-deps {{arguments}}
@CLIPPY_CONF_DIR={{repo}} just in-pr cargo clippy --profile {{profile}} --all-features --all-targets --no-deps {{arguments}}
@CLIPPY_CONF_DIR={{repo}} just not-in-pr cargo hack --optional-deps --feature-powerset {{cargo-hack-groups}} --ignore-unknown-features clippy --profile {{profile}} --all-targets --no-deps {{arguments}}

[private]
test *arguments:
@just install-cargo-nextest
@just install-cargo-hack

RUST_BACKTRACE=1 cargo hack --optional-deps --feature-powerset {{cargo-hack-groups}} nextest run --cargo-profile {{profile}} {{arguments}}
RUST_BACKTRACE=1 cargo test --profile {{profile}} --workspace --all-features --doc {{arguments}}
RUST_BACKTRACE=1 cargo test --profile {{profile}} --all-features --doc {{arguments}}

[private]
coverage *arguments:
Expand Down
6 changes: 6 additions & 0 deletions libs/error-stack/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ All notable changes to `error-stack` will be documented in this file.

## 0.5.0 - Unreleased

### Features

- Capture `source()` errors when converting `Error` to `Report` ([#4678](https://github.com/hashintel/hash/pull/4678))

### Breaking Changes

- `Backtrace`s are not included in the `std` feature anymore. Instead, the `backtrace` feature is used which is enabled by default ([#4685](https://github.com/hashintel/hash/pull/4685))

## [0.4.1](https://github.com/hashintel/hash/tree/error-stack%400.4.1/libs/error-stack) - 2023-09-04

### Fixes
Expand Down
16 changes: 10 additions & 6 deletions libs/error-stack/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,17 @@ thiserror = "1.0.59"
rustc_version = "0.4"

[features]
default = ["std"]
default = ["std", "backtrace"]

spantrace = ["dep:tracing-error", "std"]
std = ["anyhow?/std"]
eyre = ["dep:eyre", "std"]
serde = ["dep:serde"]
hooks = ['dep:spin']
std = ["anyhow?/std"] # Enables support for `Error`
backtrace = ["std"] # Enables automatic capturing of `Backtrace`s (requires Rust 1.65+)

spantrace = ["dep:tracing-error", "std"] # Enables automatic capturing of `SpanTrace`s
serde = ["dep:serde"] # Enables serialization support
hooks = ['dep:spin'] # Enables hooks on `no-std` platforms using spin locks

anyhow = ["dep:anyhow"] # Provides `into_report` to convert `anyhow::Error` to `Report`
eyre = ["dep:eyre", "std"] # Provides `into_report` to convert `eyre::Report` to `Report`

[lints]
workspace = true
Expand Down
11 changes: 6 additions & 5 deletions libs/error-stack/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#![allow(clippy::unwrap_used)]
use std::process::exit;

use rustc_version::{version_meta, Channel, Version};

fn main() {
let version_meta = version_meta().unwrap();
let version_meta = version_meta().expect("Could not get Rust version");

println!("cargo:rustc-check-cfg=cfg(nightly)");
if version_meta.channel == Channel::Nightly {
Expand All @@ -16,8 +17,8 @@ fn main() {
rustc_version.patch,
);

println!("cargo:rustc-check-cfg=cfg(rust_1_65)");
if trimmed_rustc_version >= Version::new(1, 65, 0) {
println!("cargo:rustc-cfg=rust_1_65");
if cfg!(feature = "backtrace") && trimmed_rustc_version < Version::new(1, 65, 0) {
println!("cargo:warning=The `backtrace` feature requires Rust 1.65.0 or later.");
exit(1);
}
}
8 changes: 4 additions & 4 deletions libs/error-stack/src/fmt/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,15 +453,15 @@ impl Hooks {
}

mod default {
#[cfg(any(all(feature = "std", rust_1_65), feature = "spantrace"))]
#[cfg(any(feature = "backtrace", feature = "spantrace"))]
use alloc::format;
#[cfg_attr(feature = "std", allow(unused_imports))]
use alloc::string::ToString;
use core::{
panic::Location,
sync::atomic::{AtomicBool, Ordering},
};
#[cfg(all(rust_1_65, feature = "std"))]
#[cfg(feature = "backtrace")]
use std::backtrace::Backtrace;
#[cfg(feature = "std")]
use std::sync::Once;
Expand Down Expand Up @@ -502,7 +502,7 @@ mod default {

Report::install_debug_hook::<Location>(location);

#[cfg(all(feature = "std", rust_1_65))]
#[cfg(feature = "backtrace")]
Report::install_debug_hook::<Backtrace>(backtrace);

#[cfg(feature = "spantrace")]
Expand All @@ -514,7 +514,7 @@ mod default {
context.push_body(LocationAttachment::new(location, context.color_mode()).to_string());
}

#[cfg(all(feature = "std", rust_1_65))]
#[cfg(feature = "backtrace")]
fn backtrace(backtrace: &Backtrace, context: &mut HookContext<Backtrace>) {
let idx = context.increment_counter();

Expand Down
21 changes: 12 additions & 9 deletions libs/error-stack/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,9 @@
//! ### Automatic Backtraces
//!
//! When on a Rust 1.65 or later, [`Report`] will try to capture a [`Backtrace`] if `RUST_BACKTRACE`
//! or `RUST_BACKTRACE_LIB` is set. If on a nightly toolchain, it will use the [`Backtrace`] if
//! provided by the base [`Context`], and will try to capture one otherwise.
//! or `RUST_BACKTRACE_LIB` is set and the `backtrace` feature is enabled (by default this is the
//! case). If on a nightly toolchain, it will use the [`Backtrace`] if provided by the base
//! [`Context`], and will try to capture one otherwise.
//!
//! Unlike some other approaches, this does not require the user modifying their custom error types
//! to be aware of backtraces, and doesn't require manual implementations to forward calls down any
Expand Down Expand Up @@ -452,13 +453,15 @@
//!
//! ### Feature Flags
//!
//! Feature | Description | default
//! ---------------|--------------------------------------------------------------------|----------
//! `std` | Enables support for [`Error`], and, on Rust 1.65+, [`Backtrace`] | enabled
//! `spantrace` | Enables automatic capturing of [`SpanTrace`]s | disabled
//! `hooks` | Enables hooks on `no-std` platforms using spin locks | disabled
//! `anyhow` | Provides `into_report` to convert [`anyhow::Error`] to [`Report`] | disabled
//! `eyre` | Provides `into_report` to convert [`eyre::Report`] to [`Report`] | disabled
//! Feature | Description | default
//! ---------------|---------------------------------------------------------------------|----------
//! `std` | Enables support for [`Error`] | enabled
//! `backtrace` | Enables automatic capturing of [`Backtrace`]s (requires Rust 1.65+) | enabled
//! `spantrace` | Enables automatic capturing of [`SpanTrace`]s | disabled
//! `hooks` | Enables hooks on `no-std` platforms using spin locks | disabled
//! `serde` | Enables serialization support for [`Report`] | disabled
//! `anyhow` | Provides `into_report` to convert [`anyhow::Error`] to [`Report`] | disabled
//! `eyre` | Provides `into_report` to convert [`eyre::Report`] to [`Report`] | disabled
//!
//!
//! [`set_debug_hook`]: Report::set_debug_hook
Expand Down
8 changes: 4 additions & 4 deletions libs/error-stack/src/report.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg_attr(feature = "std", allow(unused_imports))]
use alloc::{boxed::Box, vec, vec::Vec};
use core::{error::Error, fmt, marker::PhantomData, mem, panic::Location};
#[cfg(all(rust_1_65, feature = "std"))]
#[cfg(feature = "backtrace")]
use std::backtrace::{Backtrace, BacktraceStatus};
#[cfg(feature = "std")]
use std::process::ExitCode;
Expand Down Expand Up @@ -298,13 +298,13 @@ impl<C> Report<C> {
#[cfg(not(nightly))]
let location = Some(Location::caller());

#[cfg(all(nightly, feature = "std"))]
#[cfg(all(nightly, feature = "backtrace"))]
let backtrace = core::error::request_ref::<Backtrace>(&frame.as_error())
.filter(|backtrace| backtrace.status() == BacktraceStatus::Captured)
.is_none()
.then(Backtrace::capture);

#[cfg(all(rust_1_65, not(nightly), feature = "std"))]
#[cfg(all(not(nightly), feature = "backtrace"))]
let backtrace = Some(Backtrace::capture());

#[cfg(all(nightly, feature = "spantrace"))]
Expand All @@ -326,7 +326,7 @@ impl<C> Report<C> {
report = report.attach(*location);
}

#[cfg(all(rust_1_65, feature = "std"))]
#[cfg(feature = "backtrace")]
if let Some(backtrace) =
backtrace.filter(|bt| matches!(bt.status(), BacktraceStatus::Captured))
{
Expand Down
20 changes: 10 additions & 10 deletions libs/error-stack/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use core::{
iter,
sync::atomic::{AtomicI8, Ordering},
};
#[cfg(all(rust_1_65, feature = "std"))]
#[cfg(feature = "backtrace")]
use std::backtrace::Backtrace;

use error_stack::{AttachmentKind, Context, Frame, FrameKind, Report, Result};
Expand Down Expand Up @@ -100,31 +100,31 @@ impl Context for ErrorA {
}

#[derive(Debug)]
#[cfg(all(rust_1_65, feature = "std"))]
#[cfg(feature = "backtrace")]
pub struct ErrorB(pub u32, std::backtrace::Backtrace);

#[cfg(all(rust_1_65, feature = "std"))]
#[cfg(feature = "backtrace")]
impl ErrorB {
pub fn new(value: u32) -> Self {
Self(value, Backtrace::force_capture())
}
}

#[cfg(all(rust_1_65, feature = "std"))]
#[cfg(feature = "backtrace")]
impl fmt::Display for ErrorB {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.write_str("error B")
}
}

#[cfg(all(nightly, feature = "std"))]
#[cfg(all(nightly, feature = "backtrace"))]
impl core::error::Error for ErrorB {
fn provide<'a>(&'a self, request: &mut core::error::Request<'a>) {
request.provide_ref(&self.1);
}
}

#[cfg(all(rust_1_65, feature = "std"))]
#[cfg(feature = "backtrace")]
impl ErrorB {
pub const fn backtrace(&self) -> &Backtrace {
&self.1
Expand Down Expand Up @@ -186,7 +186,7 @@ pub fn messages<E>(report: &Report<E>) -> Vec<String> {
FrameKind::Context(context) => context.to_string(),
FrameKind::Attachment(AttachmentKind::Printable(attachment)) => attachment.to_string(),
FrameKind::Attachment(AttachmentKind::Opaque(_)) => {
#[cfg(all(rust_1_65, feature = "std"))]
#[cfg(feature = "backtrace")]
if frame.type_id() == TypeId::of::<Backtrace>() {
return String::from("Backtrace");
}
Expand All @@ -209,7 +209,7 @@ pub fn frame_kinds<E>(report: &Report<E>) -> Vec<FrameKind> {
remove_builtin_frames(report).map(Frame::kind).collect()
}

#[cfg(all(rust_1_65, feature = "std"))]
#[cfg(feature = "backtrace")]
pub fn supports_backtrace() -> bool {
static STATE: Lazy<bool> = Lazy::new(|| {
let bt = std::backtrace::Backtrace::capture();
Expand Down Expand Up @@ -249,7 +249,7 @@ pub fn remove_builtin_messages<S: AsRef<str>>(

pub fn remove_builtin_frames<E>(report: &Report<E>) -> impl Iterator<Item = &Frame> {
report.frames().filter(|frame| {
#[cfg(all(rust_1_65, feature = "std"))]
#[cfg(feature = "backtrace")]
if frame.type_id() == TypeId::of::<Backtrace>() {
return false;
}
Expand All @@ -266,7 +266,7 @@ pub fn remove_builtin_frames<E>(report: &Report<E>) -> impl Iterator<Item = &Fra
#[allow(unused_mut)]
#[allow(clippy::missing_const_for_fn)]
pub fn expect_count(mut count: usize) -> usize {
#[cfg(all(rust_1_65, feature = "std"))]
#[cfg(feature = "backtrace")]
if supports_backtrace() {
count += 1;
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading