Skip to content

Commit 5fa8a68

Browse files
committed
feat(build-rs): Add the 'error' directive
1 parent 260fcab commit 5fa8a68

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

crates/build-rs/src/output.rs

+20
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,26 @@ pub fn warning(message: &str) {
403403
emit("warning", message);
404404
}
405405

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

0 commit comments

Comments
 (0)