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

Commit bf2e4dc

Browse files
authored
Merge pull request #1687 from jstasiak/target-backup-exclusion
Cooperate with Cargo regarding target/ backup exclusion
2 parents dd96b56 + 65e83bb commit bf2e4dc

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

rls/src/build/cargo.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,17 @@ fn run_cargo_ws(
260260
Arc::clone(&reached_primary),
261261
);
262262

263+
// Cargo excludes target/ from backups since rust-lang/cargo@cf3bfc9/rust-lang/cargo#8378 but
264+
// it does so if and only if the directory does not exist and it's about to create it.
265+
// rls runs cargo internally with target directory set to target/rls/ so, if target/ doesn't
266+
// exist yet and rls runs, target/ and target/rls/ will be created. While target/rls/ will be
267+
// excluded from backups target/ won't be (as from our perspective it's not the target
268+
// directory but its parent) and, when user runs "cargo build" themselves cargo will see
269+
// target/ existing already and won't exclude it from backups. We can work around that by
270+
// attempting to create a backup-excluded target/ ourelves using cargo paths:: machinery.
271+
cargo::util::paths::create_dir_all_excluded_from_backups_atomic(
272+
config.target_dir().unwrap().unwrap().as_path_unlocked().parent().unwrap(),
273+
)?;
263274
let exec = Arc::new(exec) as Arc<dyn Executor>;
264275
match compile_with_exec(&ws, &compile_opts, &exec) {
265276
Ok(_) => {

tests/client.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::fs;
12
use std::path::Path;
23
use std::time::{Duration, Instant};
34

@@ -2227,3 +2228,30 @@ fn client_parse_error_on_malformed_input() {
22272228
// to provide better fault tolerance.
22282229
cmd.wait().unwrap();
22292230
}
2231+
2232+
#[test]
2233+
fn client_cargo_target_directory_is_excluded_from_backups() {
2234+
// This is to make sure that if it's rls that crates target/ directory the directory is
2235+
// excluded from backups just as if it was created by cargo itself. See a comment in
2236+
// run_cargo_ws() or rust-lang/cargo@cf3bfc9/rust-lang/cargo#8378 for more information.
2237+
let p = project("backup_exclusion_workspace")
2238+
.file("Cargo.toml", &basic_bin_manifest("foo"))
2239+
.file(
2240+
"src/main.rs",
2241+
r#"
2242+
fn main() {
2243+
println!("Hello world!");
2244+
}
2245+
"#,
2246+
)
2247+
.build();
2248+
let root_path = p.root();
2249+
let mut rls = p.spawn_rls_async();
2250+
rls.request::<Initialize>(0, initialize_params(root_path));
2251+
let _ = rls.wait_for_indexing();
2252+
let cachedir_tag = p.root().join("target").join("CACHEDIR.TAG");
2253+
assert!(cachedir_tag.is_file());
2254+
assert!(fs::read_to_string(&cachedir_tag)
2255+
.unwrap()
2256+
.starts_with("Signature: 8a477f597d28d172789f06886806bc55"));
2257+
}

0 commit comments

Comments
 (0)