diff --git a/src/cargo/ops/resolve.rs b/src/cargo/ops/resolve.rs
index d528e8a04417..473ec6d40530 100644
--- a/src/cargo/ops/resolve.rs
+++ b/src/cargo/ops/resolve.rs
@@ -642,15 +642,19 @@ fn register_previous_locks(
     // if they changed.
     let mut avoid_locking = HashSet::new();
     registry.add_to_yanked_whitelist(resolve.iter().filter(keep));
+    // We must check `path_pkg` first so we don't recursively walk them via `add_deps`
     for node in resolve.iter() {
-        if !keep(&node) {
-            add_deps(resolve, node, &mut avoid_locking);
-        } else if let Some(pkg) = path_pkg(node.source_id()) {
+        if let Some(pkg) = path_pkg(node.source_id()) {
             if pkg.package_id() != node {
                 avoid_locking.insert(node);
             }
         }
     }
+    for node in resolve.iter() {
+        if !keep(&node) {
+            add_deps(resolve, node, &mut avoid_locking);
+        }
+    }
 
     // Ok, but the above loop isn't the entire story! Updates to the dependency
     // graph can come from two locations, the `cargo update` command or
diff --git a/tests/testsuite/update.rs b/tests/testsuite/update.rs
index ab178a41f323..8f9981335003 100644
--- a/tests/testsuite/update.rs
+++ b/tests/testsuite/update.rs
@@ -991,14 +991,11 @@ rustdns.workspace = true
     p.change_file("Cargo.toml", &workspace_toml.replace("2.29.8", "2.29.81"));
 
     p.cargo("update -p rootcrate")
-        .with_stderr(&format!(
+        .with_stderr(
             "\
-[UPDATING] git repository `{}`
 [UPDATING] rootcrate v2.29.8 ([CWD]/rootcrate) -> v2.29.81
-[UPDATING] rustdns v0.5.0 ([..]) -> [..]
 [UPDATING] subcrate v2.29.8 ([CWD]/subcrate) -> v2.29.81",
-            git_project.url(),
-        ))
+        )
         .run();
 }