Skip to content

Commit ddf05ad

Browse files
committed
Auto merge of #11674 - weihanglo:rust-1.68.0, r=ehuss
[beta-1.68] Backport fixes Beta backports: * #11661 — Do not error for `auth-required: true` without `-Z sparse-registry` * #11672 — Verify source before recompile
2 parents 0762e3b + 9faaba0 commit ddf05ad

File tree

5 files changed

+108
-22
lines changed

5 files changed

+108
-22
lines changed

src/cargo/core/compiler/fingerprint.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -379,17 +379,21 @@ pub fn prepare_target(cx: &mut Context<'_, '_>, unit: &Unit, force: bool) -> Car
379379
let compare = compare_old_fingerprint(&loc, &*fingerprint, mtime_on_use);
380380
log_compare(unit, &compare);
381381

382-
// If our comparison failed (e.g., we're going to trigger a rebuild of this
383-
// crate), then we also ensure the source of the crate passes all
384-
// verification checks before we build it.
382+
// If our comparison failed or reported dirty (e.g., we're going to trigger
383+
// a rebuild of this crate), then we also ensure the source of the crate
384+
// passes all verification checks before we build it.
385385
//
386386
// The `Source::verify` method is intended to allow sources to execute
387387
// pre-build checks to ensure that the relevant source code is all
388388
// up-to-date and as expected. This is currently used primarily for
389389
// directory sources which will use this hook to perform an integrity check
390390
// on all files in the source to ensure they haven't changed. If they have
391391
// changed then an error is issued.
392-
if compare.is_err() {
392+
if compare
393+
.as_ref()
394+
.map(|dirty| dirty.is_some())
395+
.unwrap_or(true)
396+
{
393397
let source_id = unit.pkg.package_id().source_id();
394398
let sources = bcx.packages.sources();
395399
let source = sources

src/cargo/sources/registry/http_remote.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,6 @@ impl<'cfg> HttpRegistry<'cfg> {
334334
}
335335
}
336336

337-
fn check_registry_auth_unstable(&self) -> CargoResult<()> {
338-
if self.auth_required && !self.config.cli_unstable().registry_auth {
339-
anyhow::bail!("authenticated registries require `-Z registry-auth`");
340-
}
341-
Ok(())
342-
}
343-
344337
/// Get the cached registry configuration, if it exists.
345338
fn config_cached(&mut self) -> CargoResult<Option<&RegistryConfig>> {
346339
if self.registry_config.is_some() {
@@ -486,7 +479,9 @@ impl<'cfg> RegistryData for HttpRegistry<'cfg> {
486479
return Poll::Ready(Ok(LoadResponse::NotFound));
487480
}
488481
StatusCode::Unauthorized
489-
if !self.auth_required && path == Path::new("config.json") =>
482+
if !self.auth_required
483+
&& path == Path::new("config.json")
484+
&& self.config.cli_unstable().registry_auth =>
490485
{
491486
debug!("re-attempting request for config.json with authorization included.");
492487
self.fresh.remove(path);
@@ -542,6 +537,10 @@ impl<'cfg> RegistryData for HttpRegistry<'cfg> {
542537
}
543538
}
544539

540+
if !self.config.cli_unstable().registry_auth {
541+
self.auth_required = false;
542+
}
543+
545544
// Looks like we're going to have to do a network request.
546545
self.start_fetch()?;
547546

@@ -587,7 +586,6 @@ impl<'cfg> RegistryData for HttpRegistry<'cfg> {
587586
}
588587
}
589588
if self.auth_required {
590-
self.check_registry_auth_unstable()?;
591589
let authorization =
592590
auth::auth_token(self.config, &self.source_id, self.login_url.as_ref(), None)?;
593591
headers.append(&format!("Authorization: {}", authorization))?;
@@ -660,8 +658,10 @@ impl<'cfg> RegistryData for HttpRegistry<'cfg> {
660658
}
661659

662660
fn config(&mut self) -> Poll<CargoResult<Option<RegistryConfig>>> {
663-
let cfg = ready!(self.config()?).clone();
664-
self.check_registry_auth_unstable()?;
661+
let mut cfg = ready!(self.config()?).clone();
662+
if !self.config.cli_unstable().registry_auth {
663+
cfg.auth_required = false;
664+
}
665665
Poll::Ready(Ok(Some(cfg)))
666666
}
667667

src/cargo/sources/registry/remote.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,9 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
245245
match ready!(self.load(Path::new(""), Path::new("config.json"), None)?) {
246246
LoadResponse::Data { raw_data, .. } => {
247247
trace!("config loaded");
248-
let cfg: RegistryConfig = serde_json::from_slice(&raw_data)?;
249-
if cfg.auth_required && !self.config.cli_unstable().registry_auth {
250-
return Poll::Ready(Err(anyhow::anyhow!(
251-
"authenticated registries require `-Z registry-auth`"
252-
)));
248+
let mut cfg: RegistryConfig = serde_json::from_slice(&raw_data)?;
249+
if !self.config.cli_unstable().registry_auth {
250+
cfg.auth_required = false;
253251
}
254252
Poll::Ready(Ok(Some(cfg)))
255253
}

tests/testsuite/freshness.rs

+70
Original file line numberDiff line numberDiff line change
@@ -2744,3 +2744,73 @@ fn changing_linker() {
27442744
)
27452745
.run();
27462746
}
2747+
2748+
#[cargo_test]
2749+
fn verify_source_before_recompile() {
2750+
Package::new("bar", "0.1.0")
2751+
.file("src/lib.rs", "")
2752+
.publish();
2753+
let p = project()
2754+
.file(
2755+
"Cargo.toml",
2756+
r#"
2757+
[package]
2758+
name = "foo"
2759+
version = "0.1.0"
2760+
2761+
[dependencies]
2762+
bar = "0.1.0"
2763+
"#,
2764+
)
2765+
.file("src/lib.rs", "")
2766+
.build();
2767+
2768+
p.cargo("vendor --respect-source-config").run();
2769+
p.change_file(
2770+
".cargo/config.toml",
2771+
r#"
2772+
[source.crates-io]
2773+
replace-with = 'vendor'
2774+
2775+
[source.vendor]
2776+
directory = 'vendor'
2777+
"#,
2778+
);
2779+
// Sanity check: vendoring works correctly.
2780+
p.cargo("check --verbose")
2781+
.with_stderr_contains("[RUNNING] `rustc --crate-name bar [CWD]/vendor/bar/src/lib.rs[..]")
2782+
.run();
2783+
// Now modify vendored crate.
2784+
p.change_file(
2785+
"vendor/bar/src/lib.rs",
2786+
r#"compile_error!("You shall not pass!");"#,
2787+
);
2788+
// Should ignore modifed sources without any recompile.
2789+
p.cargo("check --verbose")
2790+
.with_stderr(
2791+
"\
2792+
[FRESH] bar v0.1.0
2793+
[FRESH] foo v0.1.0 ([CWD])
2794+
[FINISHED] dev [..]
2795+
",
2796+
)
2797+
.run();
2798+
2799+
// Add a `RUSTFLAGS` to trigger a recompile.
2800+
//
2801+
// Cargo should refuse to build because of checksum verfication failure.
2802+
// Cargo shouldn't recompile dependency `bar`.
2803+
p.cargo("check --verbose")
2804+
.env("RUSTFLAGS", "-W warnings")
2805+
.with_status(101)
2806+
.with_stderr(
2807+
"\
2808+
error: the listed checksum of `[CWD]/vendor/bar/src/lib.rs` has changed:
2809+
expected: [..]
2810+
actual: [..]
2811+
2812+
directory sources are not [..]
2813+
",
2814+
)
2815+
.run();
2816+
}

tests/testsuite/registry_auth.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,26 @@ static SUCCESS_OUTPUT: &'static str = "\
4242

4343
#[cargo_test]
4444
fn requires_nightly() {
45-
let _registry = RegistryBuilder::new().alternative().auth_required().build();
45+
let _registry = RegistryBuilder::new()
46+
.alternative()
47+
.auth_required()
48+
.http_api()
49+
.build();
4650

4751
let p = make_project();
4852
p.cargo("build")
4953
.with_status(101)
50-
.with_stderr_contains(" authenticated registries require `-Z registry-auth`")
54+
.with_stderr(
55+
r#"[UPDATING] `alternative` index
56+
[DOWNLOADING] crates ...
57+
error: failed to download from `[..]/dl/bar/0.0.1/download`
58+
59+
Caused by:
60+
failed to get successful HTTP response from `[..]`, got 401
61+
body:
62+
Unauthorized message from server.
63+
"#,
64+
)
5165
.run();
5266
}
5367

0 commit comments

Comments
 (0)