Skip to content

Commit 623b7ff

Browse files
committed
fix(publish): Block until it is in index
Originally, crates.io would block on publish requests until the publish was complete, giving `cargo publish` this behavior by extension. When crates.io switched to asynchronous publishing, this intermittently broke people's workflows when publishing multiple crates. I say interittent because it usually works until it doesn't and it is unclear why to the end user because it will be published by the time they check. In the end, callers tend to either put in timeouts (and pray), poll the server's API, or use `crates-index` crate to poll the index. This isn't sufficient because - For any new interested party, this is a pit of failure they'll fall into - crates-index has re-implemented index support incorrectly in the past, currently doesn't handle auth, doesn't support `git-cli`, etc. - None of these previous options work if we were to implement workspace-publish support (#1169) - The new sparse registry might increase the publish times, making the delay easier to hit manually - The new sparse registry goes through CDNs so checking the server's API might not be sufficient - Once the sparse registry is available, crates-index users will find out when the package is ready in git but it might not be ready through the sparse registry because of CDNs So now `cargo` will block until it sees the package in the index. - This is checking via the index instead of server APIs in case there are propagation delays. This has the side effect of being noisy because of all of the "Updating index" messages. - This is done unconditionally because cargo used to block and that didn't seem to be a problem, blocking by default is the less error prone case, and there doesn't seem to be enough justification for a "don't block" flag. Fixes #9507
1 parent 566a3fa commit 623b7ff

File tree

3 files changed

+72
-41
lines changed

3 files changed

+72
-41
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ fn substitute_macros(input: &str) -> String {
197197
("[MIGRATING]", " Migrating"),
198198
("[EXECUTABLE]", " Executable"),
199199
("[SKIPPING]", " Skipping"),
200+
("[WAITING]", " Waiting"),
200201
];
201202
let mut result = input.to_owned();
202203
for &(pat, subst) in &macros {

src/cargo/ops/registry.rs

+60
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ use termcolor::Color::Green;
1818
use termcolor::ColorSpec;
1919

2020
use crate::core::dependency::DepKind;
21+
use crate::core::dependency::Dependency;
2122
use crate::core::manifest::ManifestMetadata;
2223
use crate::core::resolver::CliFeatures;
2324
use crate::core::source::Source;
25+
use crate::core::QueryKind;
2426
use crate::core::{Package, SourceId, Workspace};
2527
use crate::ops;
2628
use crate::ops::Packages;
@@ -183,6 +185,10 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
183185
reg_id,
184186
opts.dry_run,
185187
)?;
188+
if !opts.dry_run {
189+
let timeout = std::time::Duration::from_secs(300);
190+
wait_for_publish(opts.config, reg_id, pkg, timeout)?;
191+
}
186192

187193
Ok(())
188194
}
@@ -374,6 +380,60 @@ fn transmit(
374380
Ok(())
375381
}
376382

383+
fn wait_for_publish(
384+
config: &Config,
385+
registry_src: SourceId,
386+
pkg: &Package,
387+
timeout: std::time::Duration,
388+
) -> CargoResult<()> {
389+
let version_req = format!("={}", pkg.version());
390+
let mut source = registry_src.load(config, &HashSet::new())?;
391+
let source_description = source.describe();
392+
let query = Dependency::parse(pkg.name(), Some(&version_req), registry_src)?;
393+
394+
let now = std::time::Instant::now();
395+
let sleep_time = std::time::Duration::from_secs(1);
396+
let mut logged = false;
397+
loop {
398+
{
399+
let _lock = config.acquire_package_cache_lock()?;
400+
source.invalidate_cache();
401+
let summaries = loop {
402+
// Exact to avoid returning all for path/git
403+
match source.query_vec(&query, QueryKind::Exact) {
404+
std::task::Poll::Ready(res) => {
405+
break res?;
406+
}
407+
std::task::Poll::Pending => source.block_until_ready()?,
408+
}
409+
};
410+
if !summaries.is_empty() {
411+
break;
412+
}
413+
}
414+
415+
if timeout < now.elapsed() {
416+
config.shell().warn(format!(
417+
"timed out waiting for `{}` to be in {}",
418+
pkg.name(),
419+
source_description
420+
))?;
421+
break;
422+
}
423+
424+
if !logged {
425+
config.shell().status(
426+
"Waiting",
427+
format!("on `{}` to be in {}", pkg.name(), source_description),
428+
)?;
429+
logged = true;
430+
}
431+
std::thread::sleep(sleep_time);
432+
}
433+
434+
Ok(())
435+
}
436+
377437
/// Returns the index and token from the config file for the given registry.
378438
///
379439
/// `registry` is typically the registry specified on the command-line. If

tests/testsuite/publish.rs

+11-41
Original file line numberDiff line numberDiff line change
@@ -2051,7 +2051,7 @@ error: package ID specification `bar` did not match any packages
20512051
}
20522052

20532053
#[cargo_test]
2054-
fn delayed_publish_errors() {
2054+
fn wait_for_publish() {
20552055
// Counter for number of tries before the package is "published"
20562056
let arc: Arc<Mutex<u32>> = Arc::new(Mutex::new(0));
20572057
let arc2 = arc.clone();
@@ -2107,13 +2107,15 @@ Use the --token command-line flag to remove this warning.
21072107
See [..]
21082108
[PACKAGING] delay v0.0.1 ([CWD])
21092109
[UPLOADING] delay v0.0.1 ([CWD])
2110+
[UPDATING] `dummy-registry` index
2111+
[WAITING] on `delay` to be in `dummy-registry` index
21102112
",
21112113
)
21122114
.run();
21132115

2114-
// Check nothing has touched the responder
2116+
// Verify the responder has been pinged
21152117
let lock = arc2.lock().unwrap();
2116-
assert_eq!(*lock, 0);
2118+
assert_eq!(*lock, 2);
21172119
drop(lock);
21182120

21192121
let p = project()
@@ -2131,23 +2133,6 @@ See [..]
21312133
.file("src/main.rs", "fn main() {}")
21322134
.build();
21332135

2134-
p.cargo("build -Z sparse-registry")
2135-
.masquerade_as_nightly_cargo(&["sparse-registry"])
2136-
.with_status(101)
2137-
.with_stderr(
2138-
"\
2139-
[UPDATING] [..]
2140-
[ERROR] no matching package named `delay` found
2141-
location searched: registry `crates-io`
2142-
required by package `foo v0.0.1 ([..]/foo)`
2143-
",
2144-
)
2145-
.run();
2146-
2147-
let lock = arc2.lock().unwrap();
2148-
assert_eq!(*lock, 1);
2149-
drop(lock);
2150-
21512136
p.cargo("build -Z sparse-registry")
21522137
.masquerade_as_nightly_cargo(&["sparse-registry"])
21532138
.with_status(0)
@@ -2158,7 +2143,7 @@ required by package `foo v0.0.1 ([..]/foo)`
21582143
/// the responder twice per cargo invocation. If that ever gets changed
21592144
/// this test will need to be changed accordingly.
21602145
#[cargo_test]
2161-
fn delayed_publish_errors_underscore() {
2146+
fn wait_for_publish_underscore() {
21622147
// Counter for number of tries before the package is "published"
21632148
let arc: Arc<Mutex<u32>> = Arc::new(Mutex::new(0));
21642149
let arc2 = arc.clone();
@@ -2214,13 +2199,16 @@ Use the --token command-line flag to remove this warning.
22142199
See [..]
22152200
[PACKAGING] delay_with_underscore v0.0.1 ([CWD])
22162201
[UPLOADING] delay_with_underscore v0.0.1 ([CWD])
2202+
[UPDATING] `dummy-registry` index
2203+
[WAITING] on `delay_with_underscore` to be in `dummy-registry` index
22172204
",
22182205
)
22192206
.run();
22202207

2221-
// Check nothing has touched the responder
2208+
// Verify the repsponder has been pinged
22222209
let lock = arc2.lock().unwrap();
2223-
assert_eq!(*lock, 0);
2210+
// NOTE: package names with - or _ hit the responder twice per cargo invocation
2211+
assert_eq!(*lock, 3);
22242212
drop(lock);
22252213

22262214
let p = project()
@@ -2238,24 +2226,6 @@ See [..]
22382226
.file("src/main.rs", "fn main() {}")
22392227
.build();
22402228

2241-
p.cargo("build -Z sparse-registry")
2242-
.masquerade_as_nightly_cargo(&["sparse-registry"])
2243-
.with_status(101)
2244-
.with_stderr(
2245-
"\
2246-
[UPDATING] [..]
2247-
[ERROR] no matching package named `delay_with_underscore` found
2248-
location searched: registry `crates-io`
2249-
required by package `foo v0.0.1 ([..]/foo)`
2250-
",
2251-
)
2252-
.run();
2253-
2254-
let lock = arc2.lock().unwrap();
2255-
// package names with - or _ hit the responder twice per cargo invocation
2256-
assert_eq!(*lock, 2);
2257-
drop(lock);
2258-
22592229
p.cargo("build -Z sparse-registry")
22602230
.masquerade_as_nightly_cargo(&["sparse-registry"])
22612231
.with_status(0)

0 commit comments

Comments
 (0)