Skip to content

Commit 5ae8d74

Browse files
committed
Auto merge of #9611 - ehuss:beta-1.54-backports, r=alexcrichton
Beta 1.54 backports This backports the following changes to 1.54 beta: * #9595 — Relax doc collision error. This was an unintended regression where a warning was switched to an error in 1.54. * #9563 — Change rustc-cdylib-link-arg error to a warning. Validation was added in #9523 (1.54) that generated an error. However, it was unknown at the time that use of link args via dependencies was being used. Rather than regress those projects (which has been on stable since 1.50), this switches the validation to a warning to indicate this was not intended. * #9604 — Disambiguate is_symlink. Required to get CI to pass.
2 parents aa8b092 + 09ed3d2 commit 5ae8d74

File tree

8 files changed

+193
-40
lines changed

8 files changed

+193
-40
lines changed

crates/cargo-test-support/src/paths.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,6 @@ pub trait CargoPathExt {
125125
fn move_in_time<F>(&self, travel_amount: F)
126126
where
127127
F: Fn(i64, u32) -> (i64, u32);
128-
129-
fn is_symlink(&self) -> bool;
130128
}
131129

132130
impl CargoPathExt for Path {
@@ -199,12 +197,14 @@ impl CargoPathExt for Path {
199197
});
200198
}
201199
}
200+
}
202201

203-
fn is_symlink(&self) -> bool {
204-
fs::symlink_metadata(self)
205-
.map(|m| m.file_type().is_symlink())
206-
.unwrap_or(false)
207-
}
202+
// Replace with std implementation when stabilized, see
203+
// https://github.com/rust-lang/rust/issues/85748
204+
pub fn is_symlink(path: &Path) -> bool {
205+
fs::symlink_metadata(path)
206+
.map(|m| m.file_type().is_symlink())
207+
.unwrap_or(false)
208208
}
209209

210210
fn do_op<F>(path: &Path, desc: &str, mut f: F)

src/cargo/core/compiler/context/mod.rs

+23-5
Original file line numberDiff line numberDiff line change
@@ -507,17 +507,35 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
507507
.collect::<Vec<_>>();
508508
// Sort for consistent error messages.
509509
keys.sort_unstable();
510+
// These are kept separate to retain compatibility with older
511+
// versions, which generated an error when there was a duplicate lib
512+
// or bin (but the old code did not check bin<->lib collisions). To
513+
// retain backwards compatibility, this only generates an error for
514+
// duplicate libs or duplicate bins (but not both). Ideally this
515+
// shouldn't be here, but since there isn't a complete workaround,
516+
// yet, this retains the old behavior.
517+
let mut doc_libs = HashMap::new();
518+
let mut doc_bins = HashMap::new();
510519
for unit in keys {
520+
if unit.mode.is_doc() && self.is_primary_package(unit) {
521+
// These situations have been an error since before 1.0, so it
522+
// is not a warning like the other situations.
523+
if unit.target.is_lib() {
524+
if let Some(prev) = doc_libs.insert((unit.target.crate_name(), unit.kind), unit)
525+
{
526+
doc_collision_error(unit, prev)?;
527+
}
528+
} else if let Some(prev) =
529+
doc_bins.insert((unit.target.crate_name(), unit.kind), unit)
530+
{
531+
doc_collision_error(unit, prev)?;
532+
}
533+
}
511534
for output in self.outputs(unit)?.iter() {
512535
if let Some(other_unit) = output_collisions.insert(output.path.clone(), unit) {
513536
if unit.mode.is_doc() {
514537
// See https://github.com/rust-lang/rust/issues/56169
515538
// and https://github.com/rust-lang/rust/issues/61378
516-
if self.is_primary_package(unit) {
517-
// This has been an error since before 1.0, so it
518-
// is not a warning like the other situations.
519-
doc_collision_error(unit, other_unit)?;
520-
}
521539
report_collision(unit, other_unit, &output.path, rustdoc_suggestion)?;
522540
} else {
523541
report_collision(unit, other_unit, &output.path, suggestion)?;

src/cargo/core/compiler/custom_build.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ pub struct BuildOutput {
3838
/// Environment variables which, when changed, will cause a rebuild.
3939
pub rerun_if_env_changed: Vec<String>,
4040
/// Warnings generated by this build.
41+
///
42+
/// These are only displayed if this is a "local" package, `-vv` is used,
43+
/// or there is a build error for any target in this package.
4144
pub warnings: Vec<String>,
4245
}
4346

@@ -571,13 +574,16 @@ impl BuildOutput {
571574
"rustc-link-search" => library_paths.push(PathBuf::from(value)),
572575
"rustc-link-arg-cdylib" | "rustc-cdylib-link-arg" => {
573576
if !targets.iter().any(|target| target.is_cdylib()) {
574-
bail!(
575-
"invalid instruction `cargo:{}` from {}\n\
576-
The package {} does not have a cdylib target.",
577-
key,
578-
whence,
579-
pkg_descr
580-
);
577+
warnings.push(format!(
578+
"cargo:{} was specified in the build script of {}, \
579+
but that package does not contain a cdylib target\n\
580+
\n\
581+
Allowing this was an unintended change in the 1.50 \
582+
release, and may become an error in the future. \
583+
For more information, see \
584+
<https://github.com/rust-lang/cargo/issues/9562>.",
585+
key, pkg_descr
586+
));
581587
}
582588
linker_args.push((LinkType::Cdylib, value))
583589
}

src/cargo/core/compiler/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,12 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
394394
}
395395

396396
for (lt, arg) in &output.linker_args {
397-
if lt.applies_to(target) {
397+
// There was an unintentional change where cdylibs were
398+
// allowed to be passed via transitive dependencies. This
399+
// clause should have been kept in the `if` block above. For
400+
// now, continue allowing it for cdylib only.
401+
// See https://github.com/rust-lang/cargo/issues/9562
402+
if lt.applies_to(target) && (key.0 == current_id || *lt == LinkType::Cdylib) {
398403
rustc.arg("-C").arg(format!("link-arg={}", arg));
399404
}
400405
}

tests/testsuite/build.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -4505,6 +4505,7 @@ fn building_a_dependent_crate_witout_bin_should_fail() {
45054505
#[cargo_test]
45064506
#[cfg(any(target_os = "macos", target_os = "ios"))]
45074507
fn uplift_dsym_of_bin_on_mac() {
4508+
use cargo_test_support::paths::is_symlink;
45084509
let p = project()
45094510
.file("src/main.rs", "fn main() { panic!(); }")
45104511
.file("src/bin/b.rs", "fn main() { panic!(); }")
@@ -4517,7 +4518,7 @@ fn uplift_dsym_of_bin_on_mac() {
45174518
.run();
45184519
assert!(p.target_debug_dir().join("foo.dSYM").is_dir());
45194520
assert!(p.target_debug_dir().join("b.dSYM").is_dir());
4520-
assert!(p.target_debug_dir().join("b.dSYM").is_symlink());
4521+
assert!(is_symlink(&p.target_debug_dir().join("b.dSYM")));
45214522
assert!(p.target_debug_dir().join("examples/c.dSYM").is_dir());
45224523
assert!(!p.target_debug_dir().join("c.dSYM").exists());
45234524
assert!(!p.target_debug_dir().join("d.dSYM").exists());
@@ -4526,6 +4527,7 @@ fn uplift_dsym_of_bin_on_mac() {
45264527
#[cargo_test]
45274528
#[cfg(any(target_os = "macos", target_os = "ios"))]
45284529
fn uplift_dsym_of_bin_on_mac_when_broken_link_exists() {
4530+
use cargo_test_support::paths::is_symlink;
45294531
let p = project()
45304532
.file("src/main.rs", "fn main() { panic!(); }")
45314533
.build();
@@ -4544,7 +4546,7 @@ fn uplift_dsym_of_bin_on_mac_when_broken_link_exists() {
45444546
.join("foo-baaaaaadbaaaaaad.dSYM"),
45454547
&dsym,
45464548
);
4547-
assert!(dsym.is_symlink());
4549+
assert!(is_symlink(&dsym));
45484550
assert!(!dsym.exists());
45494551

45504552
p.cargo("build").enable_mac_dsym().run();

tests/testsuite/build_script_extra_link_arg.rs

+126-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
//! Tests for -Zextra-link-arg.
22
3-
use cargo_test_support::{basic_bin_manifest, project};
3+
// NOTE: Many of these tests use `without_status()` when passing bogus flags
4+
// because MSVC link.exe just gives a warning on unknown flags (how helpful!),
5+
// and other linkers will return an error.
6+
7+
use cargo_test_support::registry::Package;
8+
use cargo_test_support::{basic_bin_manifest, basic_manifest, project};
49

510
#[cargo_test]
611
fn build_script_extra_link_arg_bin() {
@@ -125,14 +130,16 @@ fn link_arg_missing_target() {
125130
)
126131
.build();
127132

128-
p.cargo("check")
129-
.with_status(101)
130-
.with_stderr("\
131-
[COMPILING] foo [..]
132-
error: invalid instruction `cargo:rustc-link-arg-cdylib` from build script of `foo v0.0.1 ([ROOT]/foo)`
133-
The package foo v0.0.1 ([ROOT]/foo) does not have a cdylib target.
134-
")
135-
.run();
133+
// TODO: Uncomment this if cdylib restriction is re-added (see
134+
// cdylib_link_arg_transitive below).
135+
// p.cargo("check")
136+
// .with_status(101)
137+
// .with_stderr("\
138+
// [COMPILING] foo [..]
139+
// error: invalid instruction `cargo:rustc-link-arg-cdylib` from build script of `foo v0.0.1 ([ROOT]/foo)`
140+
// The package foo v0.0.1 ([ROOT]/foo) does not have a cdylib target.
141+
// ")
142+
// .run();
136143

137144
p.change_file(
138145
"build.rs",
@@ -183,3 +190,113 @@ The instruction should have the form cargo:rustc-link-arg-bin=BIN=ARG
183190
)
184191
.run();
185192
}
193+
194+
#[cargo_test]
195+
fn cdylib_link_arg_transitive() {
196+
// There was an unintended regression in 1.50 where rustc-link-arg-cdylib
197+
// arguments from dependencies were being applied in the parent package.
198+
// Previously it was silently ignored.
199+
// See https://github.com/rust-lang/cargo/issues/9562
200+
let p = project()
201+
.file(
202+
"Cargo.toml",
203+
r#"
204+
[package]
205+
name = "foo"
206+
version = "0.1.0"
207+
208+
[lib]
209+
crate-type = ["cdylib"]
210+
211+
[dependencies]
212+
bar = {path="bar"}
213+
"#,
214+
)
215+
.file("src/lib.rs", "")
216+
.file("bar/Cargo.toml", &basic_manifest("bar", "1.0.0"))
217+
.file("bar/src/lib.rs", "")
218+
.file(
219+
"bar/build.rs",
220+
r#"
221+
fn main() {
222+
println!("cargo:rustc-link-arg-cdylib=--bogus");
223+
}
224+
"#,
225+
)
226+
.build();
227+
p.cargo("build -v")
228+
.without_status()
229+
.with_stderr_contains(
230+
"\
231+
[COMPILING] bar v1.0.0 [..]
232+
[RUNNING] `rustc --crate-name build_script_build bar/build.rs [..]
233+
[RUNNING] `[..]build-script-build[..]
234+
warning: cargo:rustc-link-arg-cdylib was specified in the build script of bar v1.0.0 \
235+
([ROOT]/foo/bar), but that package does not contain a cdylib target
236+
237+
Allowing this was an unintended change in the 1.50 release, and may become an error in \
238+
the future. For more information, see <https://github.com/rust-lang/cargo/issues/9562>.
239+
[RUNNING] `rustc --crate-name bar bar/src/lib.rs [..]
240+
[COMPILING] foo v0.1.0 [..]
241+
[RUNNING] `rustc --crate-name foo src/lib.rs [..]-C link-arg=--bogus[..]`
242+
",
243+
)
244+
.run();
245+
}
246+
247+
#[cargo_test]
248+
fn link_arg_transitive_not_allowed() {
249+
// Verify that transitive dependencies don't pass link args.
250+
//
251+
// Note that rustc-link-arg doesn't have any errors or warnings when it is
252+
// unused. Perhaps that could be more aggressive, but it is difficult
253+
// since it could be used for test binaries.
254+
Package::new("bar", "1.0.0")
255+
.file("src/lib.rs", "")
256+
.file(
257+
"build.rs",
258+
r#"
259+
fn main() {
260+
println!("cargo:rustc-link-arg=--bogus");
261+
}
262+
"#,
263+
)
264+
.publish();
265+
266+
let p = project()
267+
.file(
268+
"Cargo.toml",
269+
r#"
270+
[package]
271+
name = "foo"
272+
version = "0.1.0"
273+
274+
[lib]
275+
crate-type = ["cdylib"]
276+
277+
[dependencies]
278+
bar = "1.0"
279+
"#,
280+
)
281+
.file("src/lib.rs", "")
282+
.build();
283+
284+
p.cargo("build -v -Zextra-link-arg")
285+
.masquerade_as_nightly_cargo()
286+
.with_stderr(
287+
"\
288+
[UPDATING] [..]
289+
[DOWNLOADING] [..]
290+
[DOWNLOADED] [..]
291+
[COMPILING] bar v1.0.0
292+
[RUNNING] `rustc --crate-name build_script_build [..]
293+
[RUNNING] `[..]/build-script-build[..]
294+
[RUNNING] `rustc --crate-name bar [..]
295+
[COMPILING] foo v0.1.0 [..]
296+
[RUNNING] `rustc --crate-name foo src/lib.rs [..]
297+
[FINISHED] dev [..]
298+
",
299+
)
300+
.with_stderr_does_not_contain("--bogus")
301+
.run();
302+
}

tests/testsuite/clean.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Tests for the `cargo clean` command.
22
3-
use cargo_test_support::paths::CargoPathExt;
3+
use cargo_test_support::paths::is_symlink;
44
use cargo_test_support::registry::Package;
55
use cargo_test_support::{basic_bin_manifest, basic_manifest, git, main_file, project, rustc_host};
66
use std::env;
@@ -438,7 +438,7 @@ fn assert_all_clean(build_dir: &Path) {
438438
{
439439
continue;
440440
}
441-
if path.is_symlink() || path.is_file() {
441+
if is_symlink(path) || path.is_file() {
442442
panic!("{:?} was not cleaned", path);
443443
}
444444
}

tests/testsuite/doc.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,19 @@ fn doc_multiple_targets_same_name() {
271271
.build();
272272

273273
p.cargo("doc --workspace")
274-
.with_status(101)
275-
.with_stderr(
274+
.with_stderr_unordered(
276275
"\
277-
error: document output filename collision
278-
The bin `foo_lib` in package `foo v0.1.0 ([ROOT]/foo/foo)` has the same name as \
279-
the lib `foo_lib` in package `bar v0.1.0 ([ROOT]/foo/bar)`.
280-
Only one may be documented at once since they output to the same path.
281-
Consider documenting only one, renaming one, or marking one with `doc = false` in Cargo.toml.
276+
warning: output filename collision.
277+
The bin target `foo_lib` in package `foo v0.1.0 ([ROOT]/foo/foo)` \
278+
has the same output filename as the lib target `foo_lib` in package \
279+
`bar v0.1.0 ([ROOT]/foo/bar)`.
280+
Colliding filename is: [ROOT]/foo/target/doc/foo_lib/index.html
281+
The targets should have unique names.
282+
This is a known bug where multiple crates with the same name use
283+
the same path; see <https://github.com/rust-lang/cargo/issues/6313>.
284+
[DOCUMENTING] bar v0.1.0 ([ROOT]/foo/bar)
285+
[DOCUMENTING] foo v0.1.0 ([ROOT]/foo/foo)
286+
[FINISHED] [..]
282287
",
283288
)
284289
.run();

0 commit comments

Comments
 (0)