Skip to content

Use TempDir for copied lockfiles #20290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ smallvec = { version = "1.15.1", features = [
"const_generics",
] }
smol_str = "0.3.2"
temp-dir = "0.1.16"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose this crate because it uses short, safe Rust codes and has no deps

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL temp-dir (besides tempdir, which is very popular, and tempfile, which is unmaintained).

text-size = "1.1.1"
tracing = "0.1.41"
tracing-tree = "0.4.0"
Expand Down
1 change: 1 addition & 0 deletions crates/project-model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ semver.workspace = true
serde_json.workspace = true
serde.workspace = true
serde_derive.workspace = true
temp-dir.workspace = true
tracing.workspace = true
triomphe.workspace = true
la-arena.workspace = true
Expand Down
48 changes: 43 additions & 5 deletions crates/project-model/src/cargo_workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ impl CargoWorkspace {

pub(crate) struct FetchMetadata {
command: cargo_metadata::MetadataCommand,
manifest_path: ManifestPath,
lockfile_path: Option<Utf8PathBuf>,
kind: &'static str,
no_deps: bool,
Expand Down Expand Up @@ -655,7 +656,15 @@ impl FetchMetadata {
}
.with_context(|| format!("Failed to run `{cargo_command:?}`"));

Self { command, lockfile_path, kind: config.kind, no_deps, no_deps_result, other_options }
Self {
manifest_path: cargo_toml.clone(),
command,
lockfile_path,
kind: config.kind,
no_deps,
no_deps_result,
other_options,
}
}

pub(crate) fn no_deps_metadata(&self) -> Option<&cargo_metadata::Metadata> {
Expand All @@ -672,18 +681,47 @@ impl FetchMetadata {
locked: bool,
progress: &dyn Fn(String),
) -> anyhow::Result<(cargo_metadata::Metadata, Option<anyhow::Error>)> {
let Self { mut command, lockfile_path, kind, no_deps, no_deps_result, mut other_options } =
self;
let Self {
mut command,
manifest_path,
lockfile_path,
kind,
no_deps,
no_deps_result,
mut other_options,
} = self;

if no_deps {
return no_deps_result.map(|m| (m, None));
}

let mut using_lockfile_copy = false;
let mut _temp_dir_guard = None;
// The manifest is a rust file, so this means its a script manifest
if let Some(lockfile) = lockfile_path {
let target_lockfile =
target_dir.join("rust-analyzer").join("metadata").join(kind).join("Cargo.lock");
_temp_dir_guard = temp_dir::TempDir::with_prefix("rust-analyzer").ok();
let target_lockfile = _temp_dir_guard
.and_then(|tmp| tmp.path().join("Cargo.lock").try_into().ok())
.unwrap_or_else(|| {
Copy link
Member Author

@ShoyuVanilla ShoyuVanilla Jul 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think failures on creating TempDir or converting it to Utf8PathBuf would be quite rare, but copied disambiguation from #20244 into this fallback anyway

// When multiple workspaces share the same target dir, they might overwrite into a
// single lockfile path.
// See https://github.com/rust-lang/rust-analyzer/issues/20189#issuecomment-3073520255
let manifest_path_hash = std::hash::BuildHasher::hash_one(
&std::hash::BuildHasherDefault::<rustc_hash::FxHasher>::default(),
&manifest_path,
);
let disambiguator = format!(
"{}_{manifest_path_hash}",
manifest_path.components().nth_back(1).map_or("", |c| c.as_str())
);

target_dir
.join("rust-analyzer")
.join("metadata")
.join(kind)
.join(disambiguator)
.join("Cargo.lock")
});
match std::fs::copy(&lockfile, &target_lockfile) {
Ok(_) => {
using_lockfile_copy = true;
Expand Down
3 changes: 2 additions & 1 deletion crates/project-model/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1905,7 +1905,8 @@ fn cargo_target_dir(
meta.manifest_path(manifest);
// `--no-deps` doesn't (over)write lockfiles as it doesn't do any package resolve.
// So we can use it to get `target_directory` before copying lockfiles
let mut other_options = vec!["--no-deps".to_owned()];
meta.no_deps();
let mut other_options = vec![];
if manifest.is_rust_manifest() {
meta.env("RUSTC_BOOTSTRAP", "1");
other_options.push("-Zscript".to_owned());
Expand Down
Loading