Skip to content

Commit 0d8be35

Browse files
committed
feat(build-rs): Add the 'error' directive
1 parent f9ef2c5 commit 0d8be35

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

crates/build-rs/src/output.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,26 @@ pub fn warning(message: &str) {
411411
emit("warning", message);
412412
}
413413

414+
/// The `error` instruction tells Cargo to display an error after the build script has finished
415+
/// running, and then fail the build.
416+
///
417+
/// <div class="warning">
418+
///
419+
/// Build script libraries should carefully consider if they want to use [`error`] versus
420+
/// returning a `Result`. It may be better to return a `Result`, and allow the caller to decide if the
421+
/// error is fatal or not. The caller can then decide whether or not to display the `Err` variant
422+
/// using [`error`].
423+
///
424+
/// </div>
425+
#[doc = respected_msrv!("1.84")]
426+
#[track_caller]
427+
pub fn error(message: &str) {
428+
if message.contains('\n') {
429+
panic!("cannot emit warning: message contains newline");
430+
}
431+
emit("error", message);
432+
}
433+
414434
/// Metadata, used by `links` scripts.
415435
#[track_caller]
416436
pub fn metadata(key: &str, val: &str) {

0 commit comments

Comments
 (0)