Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Cooperate with Cargo regarding target/ backup exclusion #1687

Merged
merged 2 commits into from
Nov 18, 2020
Merged
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
11 changes: 11 additions & 0 deletions rls/src/build/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,17 @@ fn run_cargo_ws(
Arc::clone(&reached_primary),
);

// Cargo excludes target/ from backups since rust-lang/cargo@cf3bfc9/rust-lang/cargo#8378 but
// it does so if and only if the directory does not exist and it's about to create it.
// rls runs cargo internally with target directory set to target/rls/ so, if target/ doesn't
// exist yet and rls runs, target/ and target/rls/ will be created. While target/rls/ will be
// excluded from backups target/ won't be (as from our perspective it's not the target
// directory but its parent) and, when user runs "cargo build" themselves cargo will see
// target/ existing already and won't exclude it from backups. We can work around that by
// attempting to create a backup-excluded target/ ourelves using cargo paths:: machinery.
cargo::util::paths::create_dir_all_excluded_from_backups_atomic(
config.target_dir().unwrap().unwrap().as_path_unlocked().parent().unwrap(),
)?;
let exec = Arc::new(exec) as Arc<dyn Executor>;
match compile_with_exec(&ws, &compile_opts, &exec) {
Ok(_) => {
Expand Down
28 changes: 28 additions & 0 deletions tests/client.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fs;
use std::path::Path;
use std::time::{Duration, Instant};

Expand Down Expand Up @@ -2227,3 +2228,30 @@ fn client_parse_error_on_malformed_input() {
// to provide better fault tolerance.
cmd.wait().unwrap();
}

#[test]
fn client_cargo_target_directory_is_excluded_from_backups() {
// This is to make sure that if it's rls that crates target/ directory the directory is
// excluded from backups just as if it was created by cargo itself. See a comment in
// run_cargo_ws() or rust-lang/cargo@cf3bfc9/rust-lang/cargo#8378 for more information.
let p = project("backup_exclusion_workspace")
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file(
"src/main.rs",
r#"
fn main() {
println!("Hello world!");
}
"#,
)
.build();
let root_path = p.root();
let mut rls = p.spawn_rls_async();
rls.request::<Initialize>(0, initialize_params(root_path));
let _ = rls.wait_for_indexing();
let cachedir_tag = p.root().join("target").join("CACHEDIR.TAG");
assert!(cachedir_tag.is_file());
assert!(fs::read_to_string(&cachedir_tag)
.unwrap()
.starts_with("Signature: 8a477f597d28d172789f06886806bc55"));
}