Skip to content

Commit 38983ce

Browse files
committed
Better error messages when rustc discovery parsing fails.
1 parent e853aa9 commit 38983ce

File tree

3 files changed

+150
-2
lines changed

3 files changed

+150
-2
lines changed

src/cargo/core/compiler/build_context/target_info.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,15 @@ impl TargetInfo {
170170
}
171171
};
172172

173-
let cfg = lines.map(Cfg::from_str).collect::<CargoResult<Vec<_>>>()?;
173+
let cfg = lines
174+
.map(Cfg::from_str)
175+
.collect::<CargoResult<Vec<_>>>()
176+
.chain_err(|| {
177+
format!(
178+
"failed to parse the cfg from `rustc --print=cfg`, got:\n{}",
179+
output
180+
)
181+
})?;
174182

175183
Ok(TargetInfo {
176184
crate_type_process,

src/cargo/util/rustc.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ impl Rustc {
5252
.lines()
5353
.find(|l| l.starts_with("host: "))
5454
.map(|l| &l[6..])
55-
.ok_or_else(|| internal("rustc -v didn't have a line for `host:`"))?;
55+
.ok_or_else(|| {
56+
failure::format_err!(
57+
"`rustc -vV` didn't have a line for `host:`, got:\n{}",
58+
verbose_version
59+
)
60+
})?;
5661
triple.to_string()
5762
};
5863

tests/testsuite/cfg.rs

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)