Skip to content

Commit 06eed4a

Browse files
committed
Allowing update --offline with precise flags
Adding additional test for updating against changed offline dependancies. #9248
1 parent ca476ab commit 06eed4a

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed

src/cargo/ops/cargo_generate_lockfile.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ pub fn update_lockfile(ws: &Workspace<'_>, opts: &UpdateOptions<'_>) -> CargoRes
4545
}
4646

4747
if opts.config.offline() {
48-
anyhow::bail!("you can't update in the offline mode");
48+
match opts.precise {
49+
None => {
50+
anyhow::bail!("you can't update in the offline mode without precise packages");
51+
}
52+
Some(_) => {}
53+
}
4954
}
5055

5156
// Updates often require a lot of modifications to the registry, so ensure

tests/testsuite/offline.rs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,3 +562,108 @@ fn offline_with_all_patched() {
562562

563563
p.cargo("check --offline").run();
564564
}
565+
566+
#[cargo_test]
567+
fn offline_precise_update() {
568+
// Cache a few versions to update against
569+
let p = project().file("src/lib.rs", "").build();
570+
let versions = ["1.2.3", "1.2.5", "1.2.9"];
571+
for vers in versions.iter() {
572+
Package::new("present_dep", vers)
573+
.file("Cargo.toml", &basic_manifest("present_dep", vers))
574+
.file(
575+
"src/lib.rs",
576+
format!(r#"pub fn get_version()->&'static str {{ "{}" }}"#, vers).as_str(),
577+
)
578+
.publish();
579+
// make package cached
580+
p.change_file(
581+
"Cargo.toml",
582+
format!(
583+
r#"
584+
[project]
585+
name = "foo"
586+
version = "0.1.0"
587+
588+
[dependencies]
589+
present_dep = "={}"
590+
"#,
591+
vers
592+
)
593+
.as_str(),
594+
);
595+
p.cargo("build").run();
596+
}
597+
598+
let p2 = project()
599+
.file(
600+
"Cargo.toml",
601+
r#"
602+
[project]
603+
name = "foo"
604+
version = "0.1.0"
605+
606+
[dependencies]
607+
present_dep = "1.2"
608+
"#,
609+
)
610+
.file(
611+
"src/main.rs",
612+
"\
613+
extern crate present_dep;
614+
fn main(){
615+
println!(\"{}\", present_dep::get_version());
616+
}",
617+
)
618+
.build();
619+
620+
p2.cargo("build --offline")
621+
.with_stderr(
622+
"\
623+
[COMPILING] present_dep v1.2.9
624+
[COMPILING] foo v0.1.0 ([CWD])
625+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
626+
",
627+
)
628+
.run();
629+
p2.rename_run("foo", "with_1_2_9")
630+
.with_stdout("1.2.9")
631+
.run();
632+
// updates happen without updating the index
633+
p2.cargo("update -p present_dep --precise 1.2.3 --offline")
634+
.with_status(0)
635+
.with_stderr(
636+
"\
637+
[UPDATING] present_dep v1.2.9 -> v1.2.3
638+
",
639+
)
640+
.run();
641+
642+
p2.cargo("build --offline")
643+
.with_stderr(
644+
"\
645+
[COMPILING] present_dep v1.2.3
646+
[COMPILING] foo v0.1.0 ([CWD])
647+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
648+
",
649+
)
650+
.run();
651+
p2.rename_run("foo", "with_1_2_9")
652+
.with_stdout("1.2.3")
653+
.run();
654+
655+
// No v1.2.8 loaded into the cache so expect failure.
656+
p2.cargo("update -p present_dep --precise 1.2.8 --offline")
657+
.with_status(101)
658+
.with_stderr(
659+
"\
660+
[ERROR] no matching package named `present_dep` found
661+
location searched: registry `[..]`
662+
required by package `foo v0.1.0 ([..]/foo)`
663+
As a reminder, you're using offline mode (--offline) which can sometimes cause \
664+
surprising resolution failures, if this error is too confusing you may wish to \
665+
retry without the offline flag.
666+
",
667+
)
668+
.run();
669+
}

0 commit comments

Comments
 (0)