@@ -50,6 +50,11 @@ use std::path::{Path, PathBuf};
50
50
use std:: str:: { self , FromStr } ;
51
51
use std:: sync:: { Arc , Mutex } ;
52
52
53
+ /// A build script instruction that tells Cargo to display an error after the
54
+ /// build script has finished running. Read [the doc] for more.
55
+ ///
56
+ /// [the doc]: https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#cargo-error
57
+ const CARGO_ERROR_SYNTAX : & str = "cargo::error=" ;
53
58
/// Deprecated: A build script instruction that tells Cargo to display a warning after the
54
59
/// build script has finished running. Read [the doc] for more.
55
60
///
@@ -82,10 +87,11 @@ pub struct BuildOutput {
82
87
pub rerun_if_changed : Vec < PathBuf > ,
83
88
/// Environment variables which, when changed, will cause a rebuild.
84
89
pub rerun_if_env_changed : Vec < String > ,
85
- /// Warnings generated by this build.
90
+ /// Errors and warnings generated by this build.
86
91
///
87
92
/// These are only displayed if this is a "local" package, `-vv` is used,
88
93
/// or there is a build error for any target in this package.
94
+ pub errors : Vec < String > ,
89
95
pub warnings : Vec < String > ,
90
96
}
91
97
@@ -473,10 +479,14 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul
473
479
state. running ( & cmd) ;
474
480
let timestamp = paths:: set_invocation_time ( & script_run_dir) ?;
475
481
let prefix = format ! ( "[{} {}] " , id. name( ) , id. version( ) ) ;
482
+ let mut errors_in_case_of_panic = Vec :: new ( ) ;
476
483
let mut warnings_in_case_of_panic = Vec :: new ( ) ;
477
484
let output = cmd
478
485
. exec_with_streaming (
479
486
& mut |stdout| {
487
+ if let Some ( error) = stdout. strip_prefix ( CARGO_ERROR_SYNTAX ) {
488
+ errors_in_case_of_panic. push ( error. to_owned ( ) ) ;
489
+ }
480
490
if let Some ( warning) = stdout
481
491
. strip_prefix ( OLD_CARGO_WARNING_SYNTAX )
482
492
. or ( stdout. strip_prefix ( NEW_CARGO_WARNING_SYNTAX ) )
@@ -522,10 +532,11 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul
522
532
} ) ;
523
533
524
534
if let Err ( error) = output {
525
- insert_warnings_in_build_outputs (
535
+ insert_errors_and_warnings_in_build_outputs (
526
536
build_script_outputs,
527
537
id,
528
538
metadata_hash,
539
+ errors_in_case_of_panic,
529
540
warnings_in_case_of_panic,
530
541
) ;
531
542
return Err ( error) ;
@@ -610,22 +621,25 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul
610
621
Ok ( job)
611
622
}
612
623
613
- /// When a build script run fails, store only warnings and nuke other outputs,
614
- /// as they are likely broken.
615
- fn insert_warnings_in_build_outputs (
624
+ /// When a build script run fails, store only errors and warnings, and nuke
625
+ /// other outputs, as they are likely broken.
626
+ fn insert_errors_and_warnings_in_build_outputs (
616
627
build_script_outputs : Arc < Mutex < BuildScriptOutputs > > ,
617
628
id : PackageId ,
618
629
metadata_hash : Metadata ,
630
+ errors : Vec < String > ,
619
631
warnings : Vec < String > ,
620
632
) {
621
- let build_output_with_only_warnings = BuildOutput {
633
+ let build_output_with_only_errors_and_warnings = BuildOutput {
634
+ errors,
622
635
warnings,
623
636
..BuildOutput :: default ( )
624
637
} ;
625
- build_script_outputs
626
- . lock ( )
627
- . unwrap ( )
628
- . insert ( id, metadata_hash, build_output_with_only_warnings) ;
638
+ build_script_outputs. lock ( ) . unwrap ( ) . insert (
639
+ id,
640
+ metadata_hash,
641
+ build_output_with_only_errors_and_warnings,
642
+ ) ;
629
643
}
630
644
631
645
impl BuildOutput {
@@ -677,6 +691,7 @@ impl BuildOutput {
677
691
let mut metadata = Vec :: new ( ) ;
678
692
let mut rerun_if_changed = Vec :: new ( ) ;
679
693
let mut rerun_if_env_changed = Vec :: new ( ) ;
694
+ let mut errors = Vec :: new ( ) ;
680
695
let mut warnings = Vec :: new ( ) ;
681
696
let whence = format ! ( "build script of `{}`" , pkg_descr) ;
682
697
// Old syntax:
@@ -962,6 +977,7 @@ impl BuildOutput {
962
977
env. push ( ( key, val) ) ;
963
978
}
964
979
}
980
+ "error" => errors. push ( value. to_string ( ) ) ,
965
981
"warning" => warnings. push ( value. to_string ( ) ) ,
966
982
"rerun-if-changed" => rerun_if_changed. push ( PathBuf :: from ( value) ) ,
967
983
"rerun-if-env-changed" => rerun_if_env_changed. push ( value. to_string ( ) ) ,
@@ -987,6 +1003,7 @@ impl BuildOutput {
987
1003
metadata,
988
1004
rerun_if_changed,
989
1005
rerun_if_env_changed,
1006
+ errors,
990
1007
warnings,
991
1008
} )
992
1009
}
0 commit comments