Skip to content

Commit a500bb2

Browse files
committed
Auto merge of #11783 - arlosi:sparse-offline, r=ehuss
Improve error for missing crate in --offline mode for sparse index This changes sparse registries to instead return `NotFound` when a non-cached crate is requested in `--offline` mode. The resolver can then suggest removing the `--offline` flag if resolution fails, which is a more helpful error than the one currently issued: `attempting to make an HTTP request, but --offline was specified`. With this change, the behavior matches what is already done for git-based registries. Closes #11276
2 parents 80f1a5d + c0d84d6 commit a500bb2

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/cargo/sources/registry/http_remote.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,13 @@ impl<'cfg> RegistryData for HttpRegistry<'cfg> {
438438
return Poll::Ready(Ok(LoadResponse::NotFound));
439439
}
440440

441+
if self.config.offline() || self.config.cli_unstable().no_index_update {
442+
// Return NotFound in offline mode when the file doesn't exist in the cache.
443+
// If this results in resolution failure, the resolver will suggest
444+
// removing the --offline flag.
445+
return Poll::Ready(Ok(LoadResponse::NotFound));
446+
}
447+
441448
if let Some(result) = self.downloads.results.remove(path) {
442449
let result =
443450
result.with_context(|| format!("download of {} failed", path.display()))?;

tests/testsuite/generate_lockfile.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Tests for the `cargo generate-lockfile` command.
22
3-
use cargo_test_support::registry::Package;
3+
use cargo_test_support::registry::{Package, RegistryBuilder};
44
use cargo_test_support::{basic_manifest, paths, project, ProjectBuilder};
55
use std::fs;
66

@@ -56,6 +56,12 @@ fn adding_and_removing_packages() {
5656
assert_eq!(lock1, lock4);
5757
}
5858

59+
#[cargo_test]
60+
fn no_index_update_sparse() {
61+
let _registry = RegistryBuilder::new().http_index().build();
62+
no_index_update();
63+
}
64+
5965
#[cargo_test]
6066
fn no_index_update() {
6167
Package::new("serde", "1.0.0").publish();

tests/testsuite/offline.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! Tests for --offline flag.
22
3-
use cargo_test_support::{basic_manifest, git, main_file, path2url, project, registry::Package};
3+
use cargo_test_support::{
4+
basic_manifest, git, main_file, path2url, project,
5+
registry::{Package, RegistryBuilder},
6+
};
47
use std::fs;
58

69
#[cargo_test]
@@ -362,6 +365,12 @@ retry without the offline flag.",
362365
.run();
363366
}
364367

368+
#[cargo_test]
369+
fn update_offline_not_cached_sparse() {
370+
let _registry = RegistryBuilder::new().http_index().build();
371+
update_offline_not_cached()
372+
}
373+
365374
#[cargo_test]
366375
fn cargo_compile_offline_with_cached_git_dep() {
367376
let git_project = git::new("dep1", |project| {

0 commit comments

Comments
 (0)