From 06eed4afe4961e201001b5bef4b466935cc557e7 Mon Sep 17 00:00:00 2001 From: Revantus <33831688+Revantus@users.noreply.github.com> Date: Sun, 14 Mar 2021 06:36:36 -0600 Subject: [PATCH 1/4] Allowing update --offline with precise flags Adding additional test for updating against changed offline dependancies. rust-lang/cargo#9248 --- src/cargo/ops/cargo_generate_lockfile.rs | 7 +- tests/testsuite/offline.rs | 105 +++++++++++++++++++++++ 2 files changed, 111 insertions(+), 1 deletion(-) diff --git a/src/cargo/ops/cargo_generate_lockfile.rs b/src/cargo/ops/cargo_generate_lockfile.rs index 0040bc9b0d6..628d67587a6 100644 --- a/src/cargo/ops/cargo_generate_lockfile.rs +++ b/src/cargo/ops/cargo_generate_lockfile.rs @@ -45,7 +45,12 @@ pub fn update_lockfile(ws: &Workspace<'_>, opts: &UpdateOptions<'_>) -> CargoRes } if opts.config.offline() { - anyhow::bail!("you can't update in the offline mode"); + match opts.precise { + None => { + anyhow::bail!("you can't update in the offline mode without precise packages"); + } + Some(_) => {} + } } // Updates often require a lot of modifications to the registry, so ensure diff --git a/tests/testsuite/offline.rs b/tests/testsuite/offline.rs index a5505cff781..3596e04903e 100644 --- a/tests/testsuite/offline.rs +++ b/tests/testsuite/offline.rs @@ -562,3 +562,108 @@ fn offline_with_all_patched() { p.cargo("check --offline").run(); } + +#[cargo_test] +fn offline_precise_update() { + // Cache a few versions to update against + let p = project().file("src/lib.rs", "").build(); + let versions = ["1.2.3", "1.2.5", "1.2.9"]; + for vers in versions.iter() { + Package::new("present_dep", vers) + .file("Cargo.toml", &basic_manifest("present_dep", vers)) + .file( + "src/lib.rs", + format!(r#"pub fn get_version()->&'static str {{ "{}" }}"#, vers).as_str(), + ) + .publish(); + // make package cached + p.change_file( + "Cargo.toml", + format!( + r#" + [project] + name = "foo" + version = "0.1.0" + + [dependencies] + present_dep = "={}" + "#, + vers + ) + .as_str(), + ); + p.cargo("build").run(); + } + + let p2 = project() + .file( + "Cargo.toml", + r#" + [project] + name = "foo" + version = "0.1.0" + + [dependencies] + present_dep = "1.2" + "#, + ) + .file( + "src/main.rs", + "\ +extern crate present_dep; +fn main(){ + println!(\"{}\", present_dep::get_version()); +}", + ) + .build(); + + p2.cargo("build --offline") + .with_stderr( + "\ +[COMPILING] present_dep v1.2.9 +[COMPILING] foo v0.1.0 ([CWD]) +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +", + ) + .run(); + p2.rename_run("foo", "with_1_2_9") + .with_stdout("1.2.9") + .run(); + // updates happen without updating the index + p2.cargo("update -p present_dep --precise 1.2.3 --offline") + .with_status(0) + .with_stderr( + "\ +[UPDATING] present_dep v1.2.9 -> v1.2.3 +", + ) + .run(); + + p2.cargo("build --offline") + .with_stderr( + "\ +[COMPILING] present_dep v1.2.3 +[COMPILING] foo v0.1.0 ([CWD]) +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +", + ) + .run(); + p2.rename_run("foo", "with_1_2_9") + .with_stdout("1.2.3") + .run(); + + // No v1.2.8 loaded into the cache so expect failure. + p2.cargo("update -p present_dep --precise 1.2.8 --offline") + .with_status(101) + .with_stderr( + "\ +[ERROR] no matching package named `present_dep` found +location searched: registry `[..]` +required by package `foo v0.1.0 ([..]/foo)` +As a reminder, you're using offline mode (--offline) which can sometimes cause \ +surprising resolution failures, if this error is too confusing you may wish to \ +retry without the offline flag. +", + ) + .run(); +} From 2bdac84292bb2f41e919d72bce3d1ca4ae40d8c8 Mon Sep 17 00:00:00 2001 From: Revantus <33831688+Revantus@users.noreply.github.com> Date: Sun, 14 Mar 2021 07:23:52 -0600 Subject: [PATCH 2/4] Switching precise check from match to `is_none()` --- src/cargo/ops/cargo_generate_lockfile.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/cargo/ops/cargo_generate_lockfile.rs b/src/cargo/ops/cargo_generate_lockfile.rs index 628d67587a6..03dc4006e3c 100644 --- a/src/cargo/ops/cargo_generate_lockfile.rs +++ b/src/cargo/ops/cargo_generate_lockfile.rs @@ -44,13 +44,8 @@ pub fn update_lockfile(ws: &Workspace<'_>, opts: &UpdateOptions<'_>) -> CargoRes anyhow::bail!("you can't generate a lockfile for an empty workspace.") } - if opts.config.offline() { - match opts.precise { - None => { - anyhow::bail!("you can't update in the offline mode without precise packages"); - } - Some(_) => {} - } + if opts.config.offline() && opts.precise.is_none() { + anyhow::bail!("you can't update in the offline mode without precise packages"); } // Updates often require a lot of modifications to the registry, so ensure From 8ae547233fafc675052df8606b2ba3b0cd7f8344 Mon Sep 17 00:00:00 2001 From: Revantus <33831688+Revantus@users.noreply.github.com> Date: Thu, 18 Mar 2021 08:04:42 -0600 Subject: [PATCH 3/4] Remove eager fail for update --offline --- src/cargo/ops/cargo_generate_lockfile.rs | 4 ---- tests/testsuite/offline.rs | 14 ++++++++++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/cargo/ops/cargo_generate_lockfile.rs b/src/cargo/ops/cargo_generate_lockfile.rs index 03dc4006e3c..6ef544da1d6 100644 --- a/src/cargo/ops/cargo_generate_lockfile.rs +++ b/src/cargo/ops/cargo_generate_lockfile.rs @@ -44,10 +44,6 @@ pub fn update_lockfile(ws: &Workspace<'_>, opts: &UpdateOptions<'_>) -> CargoRes anyhow::bail!("you can't generate a lockfile for an empty workspace.") } - if opts.config.offline() && opts.precise.is_none() { - anyhow::bail!("you can't update in the offline mode without precise packages"); - } - // Updates often require a lot of modifications to the registry, so ensure // that we're synchronized against other Cargos. let _lock = ws.config().acquire_package_cache_lock()?; diff --git a/tests/testsuite/offline.rs b/tests/testsuite/offline.rs index 3596e04903e..ccef042824e 100644 --- a/tests/testsuite/offline.rs +++ b/tests/testsuite/offline.rs @@ -564,7 +564,7 @@ fn offline_with_all_patched() { } #[cargo_test] -fn offline_precise_update() { +fn offline_update() { // Cache a few versions to update against let p = project().file("src/lib.rs", "").build(); let versions = ["1.2.3", "1.2.5", "1.2.9"]; @@ -648,10 +648,20 @@ fn main(){ ", ) .run(); - p2.rename_run("foo", "with_1_2_9") + p2.rename_run("foo", "with_1_2_3") .with_stdout("1.2.3") .run(); + // Offline update should only print package details and not index updating + p2.cargo("update --offline") + .with_status(0) + .with_stderr( + "\ +[UPDATING] present_dep v1.2.3 -> v1.2.9 +", + ) + .run(); + // No v1.2.8 loaded into the cache so expect failure. p2.cargo("update -p present_dep --precise 1.2.8 --offline") .with_status(101) From 61a8f711df5d9af631bbf6c4ac0450b37e1f5981 Mon Sep 17 00:00:00 2001 From: Revantus <33831688+Revantus@users.noreply.github.com> Date: Thu, 18 Mar 2021 08:33:55 -0600 Subject: [PATCH 4/4] Fix offline tests. Rename to cached/not_cached. --- tests/testsuite/offline.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/tests/testsuite/offline.rs b/tests/testsuite/offline.rs index ccef042824e..37fd873005d 100644 --- a/tests/testsuite/offline.rs +++ b/tests/testsuite/offline.rs @@ -332,7 +332,7 @@ Caused by: } #[cargo_test] -fn update_offline() { +fn update_offline_not_cached() { let p = project() .file( "Cargo.toml", @@ -350,7 +350,15 @@ fn update_offline() { .build(); p.cargo("update --offline") .with_status(101) - .with_stderr("error: you can't update in the offline mode[..]") + .with_stderr( + "\ +[ERROR] no matching package named `bar` found +location searched: registry `[..]` +required by package `foo v0.0.1 ([..]/foo)` +As a reminder, you're using offline mode (--offline) which can sometimes cause \ +surprising resolution failures, if this error is too confusing you may wish to \ +retry without the offline flag.", + ) .run(); } @@ -564,7 +572,7 @@ fn offline_with_all_patched() { } #[cargo_test] -fn offline_update() { +fn update_offline_cached() { // Cache a few versions to update against let p = project().file("src/lib.rs", "").build(); let versions = ["1.2.3", "1.2.5", "1.2.9"];