@@ -446,3 +446,138 @@ fn cfg_looks_at_rustflags_for_target() {
446446 . env ( "RUSTFLAGS" , "--cfg with_b" )
447447 . run ( ) ;
448448}
449+
450+ #[ cargo_test]
451+ fn bad_cfg_discovery ( ) {
452+ // Check error messages when `rustc -v` and `rustc --print=*` parsing fails.
453+ //
454+ // This is a `rustc` replacement which behaves differently based on an
455+ // environment variable.
456+ let p = project ( )
457+ . at ( "compiler" )
458+ . file ( "Cargo.toml" , & basic_manifest ( "compiler" , "0.1.0" ) )
459+ . file (
460+ "src/main.rs" ,
461+ r#"
462+ fn run_rustc() -> String {
463+ let mut cmd = std::process::Command::new("rustc");
464+ for arg in std::env::args_os().skip(1) {
465+ cmd.arg(arg);
466+ }
467+ String::from_utf8(cmd.output().unwrap().stdout).unwrap()
468+ }
469+
470+ fn main() {
471+ let mode = std::env::var("FUNKY_MODE").unwrap();
472+ if mode == "bad-version" {
473+ println!("foo");
474+ return;
475+ }
476+ if std::env::args_os().any(|a| a == "-vV") {
477+ print!("{}", run_rustc());
478+ return;
479+ }
480+ if mode == "no-crate-types" {
481+ return;
482+ }
483+ if mode == "bad-crate-type" {
484+ println!("foo");
485+ return;
486+ }
487+ let output = run_rustc();
488+ let mut lines = output.lines();
489+ let sysroot = loop {
490+ let line = lines.next().unwrap();
491+ if line.contains("___") {
492+ println!("{}", line);
493+ } else {
494+ break line;
495+ }
496+ };
497+ if mode == "no-sysroot" {
498+ return;
499+ }
500+ println!("{}", sysroot);
501+ if mode != "bad-cfg" {
502+ panic!("unexpected");
503+ }
504+ println!("123");
505+ }
506+ "# ,
507+ )
508+ . build ( ) ;
509+ p. cargo ( "build" ) . run ( ) ;
510+ let funky_rustc = p. bin ( "compiler" ) ;
511+
512+ let p = project ( ) . file ( "src/lib.rs" , "" ) . build ( ) ;
513+
514+ p. cargo ( "build" )
515+ . env ( "RUSTC" , & funky_rustc)
516+ . env ( "FUNKY_MODE" , "bad-version" )
517+ . with_status ( 101 )
518+ . with_stderr (
519+ "\
520+ [ERROR] `rustc -vV` didn't have a line for `host:`, got:
521+ foo
522+
523+ " ,
524+ )
525+ . run ( ) ;
526+
527+ p. cargo ( "build" )
528+ . env ( "RUSTC" , & funky_rustc)
529+ . env ( "FUNKY_MODE" , "no-crate-types" )
530+ . with_status ( 101 )
531+ . with_stderr (
532+ "\
533+ [ERROR] malformed output when learning about crate-type bin information
534+ command was: `[..]compiler[..] --crate-name ___ [..]`
535+ (no output received)
536+ " ,
537+ )
538+ . run ( ) ;
539+
540+ p. cargo ( "build" )
541+ . env ( "RUSTC" , & funky_rustc)
542+ . env ( "FUNKY_MODE" , "no-sysroot" )
543+ . with_status ( 101 )
544+ . with_stderr (
545+ "\
546+ [ERROR] output of --print=sysroot missing when learning about target-specific information from rustc
547+ command was: `[..]compiler[..]--crate-type [..]`
548+
549+ --- stdout
550+ [..]___[..]
551+ [..]___[..]
552+ [..]___[..]
553+ [..]___[..]
554+ [..]___[..]
555+ [..]___[..]
556+
557+ " ,
558+ )
559+ . run ( ) ;
560+
561+ p. cargo ( "build" )
562+ . env ( "RUSTC" , & funky_rustc)
563+ . env ( "FUNKY_MODE" , "bad-cfg" )
564+ . with_status ( 101 )
565+ . with_stderr (
566+ "\
567+ [ERROR] failed to parse the cfg from `rustc --print=cfg`, got:
568+ [..]___[..]
569+ [..]___[..]
570+ [..]___[..]
571+ [..]___[..]
572+ [..]___[..]
573+ [..]___[..]
574+ [..]
575+ 123
576+
577+
578+ Caused by:
579+ unexpected character in cfg `1`, expected parens, a comma, an identifier, or a string
580+ " ,
581+ )
582+ . run ( ) ;
583+ }
0 commit comments