Skip to content

Commit 5553284

Browse files
committed
Avoid multiple index updates.
This also restores the Cargo.lock update during packaging.
1 parent 753b03f commit 5553284

File tree

4 files changed

+18
-50
lines changed

4 files changed

+18
-50
lines changed

src/cargo/ops/cargo_package.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ pub struct PackageOpts<'cfg> {
4141
static VCS_INFO_FILE: &'static str = ".cargo_vcs_info.json";
4242

4343
pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult<Option<FileLock>> {
44+
// Make sure the Cargo.lock is up-to-date and valid.
45+
ops::resolve_ws(ws)?;
4446
let pkg = ws.current()?;
4547
let config = ws.config();
4648

src/cargo/sources/registry/remote.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
181181
if self.config.cli_unstable().no_index_update {
182182
return Ok(());
183183
}
184+
// Make sure the index is only updated once per session since it is an
185+
// expensive operation. This generally only happens when the resolver
186+
// is run multiple times, such as during `cargo publish`.
187+
if self.config.updated_sources().contains(&self.source_id) {
188+
return Ok(());
189+
}
184190

185191
debug!("updating the index");
186192

@@ -208,6 +214,7 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
208214
let repo = self.repo.borrow_mut().unwrap();
209215
git::fetch(repo, url, refspec, self.config)
210216
.chain_err(|| format!("failed to fetch `{}`", url))?;
217+
self.config.updated_sources().insert(self.source_id);
211218
Ok(())
212219
}
213220

src/cargo/util/config.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ pub struct Config {
7474
env: HashMap<String, String>,
7575
/// Profiles loaded from config.
7676
profiles: LazyCell<ConfigProfiles>,
77+
/// Tracks which sources have been updated to avoid multiple updates.
78+
updated_sources: LazyCell<RefCell<HashSet<SourceId>>>,
7779
}
7880

7981
impl Config {
@@ -129,6 +131,7 @@ impl Config {
129131
target_dir: None,
130132
env,
131133
profiles: LazyCell::new(),
134+
updated_sources: LazyCell::new(),
132135
}
133136
}
134137

@@ -271,6 +274,12 @@ impl Config {
271274
})
272275
}
273276

277+
pub fn updated_sources(&self) -> RefMut<'_, HashSet<SourceId>> {
278+
self.updated_sources.borrow_with(|| {
279+
RefCell::new(HashSet::new())
280+
}).borrow_mut()
281+
}
282+
274283
pub fn values(&self) -> CargoResult<&HashMap<String, ConfigValue>> {
275284
self.values.try_borrow_with(|| self.load_values())
276285
}

tests/testsuite/publish_lockfile.rs

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -268,54 +268,6 @@ fn no_warn_workspace_extras() {
268268
.run();
269269
}
270270

271-
#[test]
272-
fn out_of_date_lock_note() {
273-
// Dependency is force-changed from an out-of-date Cargo.lock.
274-
Package::new("dep", "1.0.0").publish();
275-
Package::new("dep", "2.0.0").publish();
276-
277-
let p = project()
278-
.file(
279-
"Cargo.toml",
280-
&pl_manifest(
281-
"foo",
282-
"0.0.1",
283-
r#"
284-
[dependencies]
285-
dep = "1.0"
286-
"#,
287-
),
288-
)
289-
.file("src/main.rs", "fn main() {}")
290-
.build();
291-
p.cargo("generate-lockfile")
292-
.masquerade_as_nightly_cargo()
293-
.run();
294-
p.change_file(
295-
"Cargo.toml",
296-
&pl_manifest(
297-
"foo",
298-
"0.0.1",
299-
r#"
300-
[dependencies]
301-
dep = "2.0"
302-
"#,
303-
),
304-
);
305-
p.cargo("package --no-verify -v --allow-dirty")
306-
.masquerade_as_nightly_cargo()
307-
.with_stderr(
308-
"\
309-
[PACKAGING] foo v0.0.1 ([..])
310-
[ARCHIVING] Cargo.toml
311-
[ARCHIVING] src/main.rs
312-
[UPDATING] `[..]` index
313-
[NOTE] package `dep v2.0.0` added to the packaged Cargo.lock file, previous version was `1.0.0`
314-
",
315-
)
316-
.run();
317-
}
318-
319271
#[test]
320272
fn warn_package_with_yanked() {
321273
Package::new("bar", "0.1.0").publish();
@@ -377,15 +329,13 @@ dependencies = [
377329
)
378330
.publish();
379331

380-
// It is unfortunate that this displays UPDATING twice.
381332
cargo_process("install --locked foo")
382333
.with_stderr(
383334
"\
384335
[UPDATING] `[..]` index
385336
[DOWNLOADING] crates ...
386337
[DOWNLOADED] foo v0.1.0 (registry `[..]`)
387338
[INSTALLING] foo v0.1.0
388-
[UPDATING] `[..]` index
389339
[WARNING] package `bar v0.1.0` in Cargo.lock is yanked in registry \
390340
`crates.io`, consider running without --locked
391341
[DOWNLOADING] crates ...

0 commit comments

Comments
 (0)