Skip to content

Commit 5191f15

Browse files
committed
fix(embedded): Don't pollute script dir with lockfile
This puts the lockfile back into a target directory in the users home, like before #12268. Another idea that came up was to move the workspace root to be in the target directory (which would effectively be like pre-#12268) but I think that is a bit hacky / misleading. This does mean that the lockfile is buried away from the user and they can't pass it along with their script. In most cases I've dealt with, this would be fine. When the lockfile is needed, they will also most likely have a workspace, so it shoud have a local lockfile in that case. The inbetween case is something that needs further evaluation for whether we should handle it and how.
1 parent c919fe3 commit 5191f15

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

src/cargo/core/workspace.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1599,7 +1599,8 @@ impl MaybePackage {
15991599
}
16001600
}
16011601

1602-
fn is_embedded(&self) -> bool {
1602+
/// Has an embedded manifest (single-file package)
1603+
pub fn is_embedded(&self) -> bool {
16031604
match self {
16041605
MaybePackage::Package(p) => p.manifest().is_embedded(),
16051606
MaybePackage::Virtual(_) => false,

src/cargo/ops/lockfile.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ use crate::util::Filesystem;
88
use anyhow::Context as _;
99

1010
pub fn load_pkg_lockfile(ws: &Workspace<'_>) -> CargoResult<Option<Resolve>> {
11-
if !ws.root().join("Cargo.lock").exists() {
11+
let lock_root = lock_root(ws);
12+
if !lock_root.as_path_unlocked().join("Cargo.lock").exists() {
1213
return Ok(None);
1314
}
1415

15-
let root = Filesystem::new(ws.root().to_path_buf());
16-
let mut f = root.open_ro("Cargo.lock", ws.config(), "Cargo.lock file")?;
16+
let mut f = lock_root.open_ro("Cargo.lock", ws.config(), "Cargo.lock file")?;
1717

1818
let mut s = String::new();
1919
f.read_to_string(&mut s)
@@ -30,12 +30,12 @@ pub fn load_pkg_lockfile(ws: &Workspace<'_>) -> CargoResult<Option<Resolve>> {
3030

3131
/// Generate a toml String of Cargo.lock from a Resolve.
3232
pub fn resolve_to_string(ws: &Workspace<'_>, resolve: &mut Resolve) -> CargoResult<String> {
33-
let (_orig, out, _ws_root) = resolve_to_string_orig(ws, resolve);
33+
let (_orig, out, _lock_root) = resolve_to_string_orig(ws, resolve);
3434
Ok(out)
3535
}
3636

3737
pub fn write_pkg_lockfile(ws: &Workspace<'_>, resolve: &mut Resolve) -> CargoResult<()> {
38-
let (orig, mut out, ws_root) = resolve_to_string_orig(ws, resolve);
38+
let (orig, mut out, lock_root) = resolve_to_string_orig(ws, resolve);
3939

4040
// If the lock file contents haven't changed so don't rewrite it. This is
4141
// helpful on read-only filesystems.
@@ -55,7 +55,7 @@ pub fn write_pkg_lockfile(ws: &Workspace<'_>, resolve: &mut Resolve) -> CargoRes
5555
"the lock file {} needs to be updated but {} was passed to prevent this\n\
5656
If you want to try to generate the lock file without accessing the network, \
5757
remove the {} flag and use --offline instead.",
58-
ws.root().to_path_buf().join("Cargo.lock").display(),
58+
lock_root.as_path_unlocked().join("Cargo.lock").display(),
5959
flag,
6060
flag
6161
);
@@ -80,7 +80,7 @@ pub fn write_pkg_lockfile(ws: &Workspace<'_>, resolve: &mut Resolve) -> CargoRes
8080
}
8181

8282
// Ok, if that didn't work just write it out
83-
ws_root
83+
lock_root
8484
.open_rw("Cargo.lock", ws.config(), "Cargo.lock file")
8585
.and_then(|mut f| {
8686
f.file().set_len(0)?;
@@ -96,15 +96,15 @@ fn resolve_to_string_orig(
9696
resolve: &mut Resolve,
9797
) -> (Option<String>, String, Filesystem) {
9898
// Load the original lock file if it exists.
99-
let ws_root = Filesystem::new(ws.root().to_path_buf());
100-
let orig = ws_root.open_ro("Cargo.lock", ws.config(), "Cargo.lock file");
99+
let lock_root = lock_root(ws);
100+
let orig = lock_root.open_ro("Cargo.lock", ws.config(), "Cargo.lock file");
101101
let orig = orig.and_then(|mut f| {
102102
let mut s = String::new();
103103
f.read_to_string(&mut s)?;
104104
Ok(s)
105105
});
106106
let out = serialize_resolve(resolve, orig.as_deref().ok());
107-
(orig.ok(), out, ws_root)
107+
(orig.ok(), out, lock_root)
108108
}
109109

110110
fn serialize_resolve(resolve: &Resolve, orig: Option<&str>) -> String {
@@ -235,3 +235,11 @@ fn emit_package(dep: &toml::Table, out: &mut String) {
235235
out.push_str(&format!("replace = {}\n\n", &dep["replace"]));
236236
}
237237
}
238+
239+
fn lock_root(ws: &Workspace<'_>) -> Filesystem {
240+
if ws.root_maybe().is_embedded() {
241+
ws.target_dir()
242+
} else {
243+
Filesystem::new(ws.root().to_owned())
244+
}
245+
}

tests/testsuite/script.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -619,5 +619,5 @@ args: []
619619
)
620620
.run();
621621

622-
assert!(local_lockfile_path.exists());
622+
assert!(!local_lockfile_path.exists());
623623
}

0 commit comments

Comments
 (0)