Skip to content

Commit 365fa96

Browse files
committed
feat(build-rs): Add the 'error' directive
1 parent 5e833bf commit 365fa96

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
@@ -400,6 +400,26 @@ pub fn warning(message: &str) {
400400
emit("warning", message);
401401
}
402402

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

0 commit comments

Comments
 (0)