Skip to content

Commit 47f38d8

Browse files
committed
Fix fix::fix_in_dependency to not rely on rustc
1 parent 4f70d17 commit 47f38d8

File tree

1 file changed

+151
-3
lines changed

1 file changed

+151
-3
lines changed

tests/testsuite/fix.rs

+151-3
Original file line numberDiff line numberDiff line change
@@ -1858,9 +1858,22 @@ fn non_edition_lint_migration() {
18581858
assert!(contents.contains("from_utf8(crate::foo::FOO)"));
18591859
}
18601860

1861-
// For rust-lang/cargo#9857
18621861
#[cargo_test]
18631862
fn fix_in_dependency() {
1863+
// Tests what happens if rustc emits a suggestion to modify a file from a
1864+
// dependency in cargo's home directory. This should never happen, and
1865+
// indicates a bug in rustc. However, there are several known bugs in
1866+
// rustc where it does this (often involving macros), so `cargo fix` has a
1867+
// guard that says if the suggestion points to some location in CARGO_HOME
1868+
// to not apply it.
1869+
//
1870+
// See https://github.com/rust-lang/cargo/issues/9857 for some other
1871+
// examples.
1872+
//
1873+
// This test uses a simulated rustc which replays a suggestion via a JSON
1874+
// message that points into CARGO_HOME. This does not use the real rustc
1875+
// because as the bugs are fixed in the real rustc, that would cause this
1876+
// test to stop working.
18641877
Package::new("bar", "1.0.0")
18651878
.file(
18661879
"src/lib.rs",
@@ -1896,8 +1909,143 @@ fn fix_in_dependency() {
18961909
"#,
18971910
)
18981911
.build();
1912+
p.cargo("fetch").run();
1913+
1914+
// The path in CARGO_HOME.
1915+
let bar_path = std::fs::read_dir(paths::home().join(".cargo/registry/src"))
1916+
.unwrap()
1917+
.next()
1918+
.unwrap()
1919+
.unwrap()
1920+
.path();
1921+
1922+
// This is a fake rustc that will emit a JSON message when the `foo` crate
1923+
// builds that tells cargo to modify a file it shouldn't.
1924+
let rustc = project()
1925+
.at("rustc-replay")
1926+
.file("Cargo.toml", &basic_manifest("rustc-replay", "1.0.0"))
1927+
.file("src/main.rs",
1928+
&r##"
1929+
fn main() {
1930+
let pkg_name = match std::env::var("CARGO_PKG_NAME") {
1931+
Ok(pkg_name) => pkg_name,
1932+
Err(_) => {
1933+
let r = std::process::Command::new("rustc")
1934+
.args(std::env::args_os().skip(1))
1935+
.status();
1936+
std::process::exit(r.unwrap().code().unwrap_or(2));
1937+
}
1938+
};
1939+
if pkg_name == "foo" {
1940+
eprintln!("{}", r#"{
1941+
"$message_type": "diagnostic",
1942+
"message": "unused variable: `abc`",
1943+
"code":
1944+
{
1945+
"code": "unused_variables",
1946+
"explanation": null
1947+
},
1948+
"level": "warning",
1949+
"spans":
1950+
[
1951+
{
1952+
"file_name": "__BAR_PATH__/bar-1.0.0/src/lib.rs",
1953+
"byte_start": 127,
1954+
"byte_end": 129,
1955+
"line_start": 5,
1956+
"line_end": 5,
1957+
"column_start": 29,
1958+
"column_end": 31,
1959+
"is_primary": true,
1960+
"text":
1961+
[
1962+
{
1963+
"text": " let $i = 1;",
1964+
"highlight_start": 29,
1965+
"highlight_end": 31
1966+
}
1967+
],
1968+
"label": null,
1969+
"suggested_replacement": null,
1970+
"suggestion_applicability": null,
1971+
"expansion": null
1972+
}
1973+
],
1974+
"children":
1975+
[
1976+
{
1977+
"message": "`#[warn(unused_variables)]` on by default",
1978+
"code": null,
1979+
"level": "note",
1980+
"spans":
1981+
[],
1982+
"children":
1983+
[],
1984+
"rendered": null
1985+
},
1986+
{
1987+
"message": "if this is intentional, prefix it with an underscore",
1988+
"code": null,
1989+
"level": "help",
1990+
"spans":
1991+
[
1992+
{
1993+
"file_name": "__BAR_PATH__/bar-1.0.0/src/lib.rs",
1994+
"byte_start": 127,
1995+
"byte_end": 129,
1996+
"line_start": 5,
1997+
"line_end": 5,
1998+
"column_start": 29,
1999+
"column_end": 31,
2000+
"is_primary": true,
2001+
"text":
2002+
[
2003+
{
2004+
"text": " let $i = 1;",
2005+
"highlight_start": 29,
2006+
"highlight_end": 31
2007+
}
2008+
],
2009+
"label": null,
2010+
"suggested_replacement": "_abc",
2011+
"suggestion_applicability": "MachineApplicable",
2012+
"expansion": null
2013+
}
2014+
],
2015+
"children":
2016+
[],
2017+
"rendered": null
2018+
}
2019+
],
2020+
"rendered": "warning: unused variable: `abc`\n --> __BAR_PATH__/bar-1.0.0/src/lib.rs:5:29\n |\n5 | let $i = 1;\n | ^^ help: if this is intentional, prefix it with an underscore: `_abc`\n |\n = note: `#[warn(unused_variables)]` on by default\n\n"
2021+
}"#.replace("\n", ""));
2022+
}
2023+
}
2024+
"##.replace("__BAR_PATH__", bar_path.to_str().unwrap()))
2025+
.build();
2026+
rustc.cargo("build").run();
2027+
let rustc_bin = rustc.bin("rustc-replay");
18992028

1900-
p.cargo("fix --allow-no-vcs")
1901-
.with_stderr_does_not_contain("[FIXED] [..]")
2029+
// The output here should not say `Fixed`.
2030+
//
2031+
// It is OK to compare the full diagnostic output here because the text is
2032+
// hard-coded in rustc-replay. Normally tests should not be checking the
2033+
// compiler output.
2034+
p.cargo("fix --lib --allow-no-vcs")
2035+
.env("RUSTC", &rustc_bin)
2036+
.with_stderr("\
2037+
[CHECKING] bar v1.0.0
2038+
[CHECKING] foo v0.1.0 [..]
2039+
warning: unused variable: `abc`
2040+
--> [ROOT]/home/.cargo/registry/src/[..]/bar-1.0.0/src/lib.rs:5:29
2041+
|
2042+
5 | let $i = 1;
2043+
| ^^ help: if this is intentional, prefix it with an underscore: `_abc`
2044+
|
2045+
= note: `#[warn(unused_variables)]` on by default
2046+
2047+
warning: `foo` (lib) generated 1 warning (run `cargo fix --lib -p foo` to apply 1 suggestion)
2048+
[FINISHED] [..]
2049+
")
19022050
.run();
19032051
}

0 commit comments

Comments
 (0)