Skip to content

Commit 4a0c883

Browse files
committed
Improve error for missing crate in --offline mode for sparse index
This changes sparse registries to instead return not found when a non-cached crate is requested in offline mode. The resolver can then suggest removing the --offline flag if resolution fails.
1 parent e7b11e7 commit 4a0c883

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-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: 37 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,39 @@ 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+
372+
let p = project()
373+
.file(
374+
"Cargo.toml",
375+
r#"
376+
[package]
377+
name = "foo"
378+
version = "0.0.1"
379+
authors = []
380+
381+
[dependencies]
382+
bar = "*"
383+
"#,
384+
)
385+
.file("src/main.rs", "fn main() {}")
386+
.build();
387+
p.cargo("update --offline")
388+
.with_status(101)
389+
.with_stderr(
390+
"\
391+
[ERROR] no matching package named `bar` found
392+
location searched: registry `[..]`
393+
required by package `foo v0.0.1 ([..]/foo)`
394+
As a reminder, you're using offline mode (--offline) which can sometimes cause \
395+
surprising resolution failures, if this error is too confusing you may wish to \
396+
retry without the offline flag.",
397+
)
398+
.run();
399+
}
400+
365401
#[cargo_test]
366402
fn cargo_compile_offline_with_cached_git_dep() {
367403
let git_project = git::new("dep1", |project| {

0 commit comments

Comments
 (0)