Skip to content

Commit 8c2cf1d

Browse files
authored
Unrolled build for #154110
Rollup merge of #154110 - lambdageek:fix/incr-compile-note, r=wesleywiser Change "error finalizing incremental compilation" text and emit it as a note, not a warning As mentioned in #151181 (comment) and #151181 (comment) the current message could be improved: 1. Right now it displays as "warning: error ..." which is confusing (is it an error or a warning) 2. It doesn't give the user a clear indication of what the consequences are 3. The _current_ build is successful. The _next_ build might be slower The new message is now ```text note: did not finalize incremental compilation session directory ... | = help: the next build will not be able to reuse work from this compilation ``` I started a zulip thread [#t-compiler/incremental > Ergonomics of "error finalizing incremental session"](https://rust-lang.zulipchat.com/#narrow/channel/241847-t-compiler.2Fincremental/topic/Ergonomics.20of.20.22error.20finalizing.20incremental.20session.22/with/580191447)
2 parents c4db0e1 + 0c05f6c commit 8c2cf1d

File tree

5 files changed

+201
-2
lines changed

5 files changed

+201
-2
lines changed

compiler/rustc_incremental/src/errors.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ pub(crate) struct DeleteFull<'a> {
192192
}
193193

194194
#[derive(Diagnostic)]
195-
#[diag("error finalizing incremental compilation session directory `{$path}`: {$err}")]
195+
#[diag("did not finalize incremental compilation session directory `{$path}`: {$err}")]
196+
#[help("the next build will not be able to reuse work from this compilation")]
196197
pub(crate) struct Finalize<'a> {
197198
pub path: &'a Path,
198199
pub err: std::io::Error,

compiler/rustc_incremental/src/persist/fs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ pub fn finalize_session_directory(sess: &Session, svh: Option<Svh>) {
366366
}
367367
Err(e) => {
368368
// Warn about the error. However, no need to abort compilation now.
369-
sess.dcx().emit_warn(errors::Finalize { path: &incr_comp_session_dir, err: e });
369+
sess.dcx().emit_note(errors::Finalize { path: &incr_comp_session_dir, err: e });
370370

371371
debug!("finalize_session_directory() - error, marking as invalid");
372372
// Drop the file lock, so we can garage collect
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
poison::poison_finalize!();
2+
3+
pub fn hello() -> i32 {
4+
42
5+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//! A proc macro that sabotages the incremental compilation finalize step.
2+
//!
3+
//! When invoked, it locates the `-working` session directory inside the
4+
//! incremental compilation directory (passed via POISON_INCR_DIR) and
5+
//! makes it impossible to rename:
6+
//!
7+
//! - On Unix: removes write permission from the parent (crate) directory.
8+
//! - On Windows: creates a file inside the -working directory and leaks
9+
//! the file handle, preventing the directory from being renamed.
10+
11+
extern crate proc_macro;
12+
13+
use std::fs;
14+
use std::path::PathBuf;
15+
16+
use proc_macro::TokenStream;
17+
18+
#[proc_macro]
19+
pub fn poison_finalize(_input: TokenStream) -> TokenStream {
20+
let incr_dir = std::env::var("POISON_INCR_DIR").expect("POISON_INCR_DIR must be set");
21+
22+
let crate_dir = find_crate_dir(&incr_dir);
23+
let working_dir = find_working_dir(&crate_dir);
24+
25+
#[cfg(unix)]
26+
poison_unix(&crate_dir);
27+
28+
#[cfg(windows)]
29+
poison_windows(&working_dir);
30+
31+
TokenStream::new()
32+
}
33+
34+
/// Remove write permission from the crate directory.
35+
/// This causes rename() to fail with EACCES
36+
#[cfg(unix)]
37+
fn poison_unix(crate_dir: &PathBuf) {
38+
use std::os::unix::fs::PermissionsExt;
39+
let mut perms = fs::metadata(crate_dir).unwrap().permissions();
40+
perms.set_mode(0o555); // r-xr-xr-x
41+
fs::set_permissions(crate_dir, perms).unwrap();
42+
}
43+
44+
/// Create a file inside the -working directory and leak the
45+
/// handle. Windows prevents renaming a directory when any file inside it
46+
/// has an open handle. The handle stays open until the rustc process exits.
47+
#[cfg(windows)]
48+
fn poison_windows(working_dir: &PathBuf) {
49+
let poison_file = working_dir.join("_poison_handle");
50+
let f = fs::File::create(&poison_file).unwrap();
51+
// Leak the handle so it stays open for the lifetime of the rustc process.
52+
std::mem::forget(f);
53+
}
54+
55+
/// Find the crate directory for `foo` inside the incremental compilation dir.
56+
///
57+
/// The incremental directory layout is:
58+
/// {incr_dir}/{crate_name}-{stable_crate_id}/
59+
fn find_crate_dir(incr_dir: &str) -> PathBuf {
60+
let mut dirs = fs::read_dir(incr_dir).unwrap().filter_map(|e| {
61+
let e = e.ok()?;
62+
let name = e.file_name();
63+
let name = name.to_str()?;
64+
if e.file_type().ok()?.is_dir() && name.starts_with("foo-") { Some(e.path()) } else { None }
65+
});
66+
67+
let first =
68+
dirs.next().unwrap_or_else(|| panic!("no foo-* crate directory found in {incr_dir}"));
69+
assert!(
70+
dirs.next().is_none(),
71+
"expected exactly one foo-* crate directory in {incr_dir}, found multiple"
72+
);
73+
first
74+
}
75+
76+
/// Find the session directory ending in "-working" inside the crate directory
77+
fn find_working_dir(crate_dir: &PathBuf) -> PathBuf {
78+
for entry in fs::read_dir(crate_dir).unwrap() {
79+
let entry = entry.unwrap();
80+
let name = entry.file_name();
81+
let name = name.to_str().unwrap().to_string();
82+
if name.starts_with("s-") && name.ends_with("-working") {
83+
return entry.path();
84+
}
85+
}
86+
panic!("no -working session directory found in {}", crate_dir.display());
87+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
//@ ignore-cross-compile
2+
//@ needs-crate-type: proc-macro
3+
4+
//! Test that a failure to finalize the incremental compilation session directory
5+
//! (i.e., the rename from "-working" to the SVH-based name) results in a
6+
//! note, not an ICE, and that the compilation output is still produced.
7+
//!
8+
//! Strategy:
9+
//! 1. Build the `poison` proc-macro crate
10+
//! 2. Compile foo.rs with incremental compilation
11+
//! The proc macro runs mid-compilation (after prepare_session_directory
12+
//! but before finalize_session_directory) and sabotages the rename:
13+
//! - On Unix: removes write permission from the crate directory,
14+
//! so rename() fails with EACCES.
15+
//! - On Windows: creates and leaks an open file handle inside the
16+
//! -working directory, so rename() fails with ERROR_ACCESS_DENIED.
17+
//! 3. Assert that stderr contains the finalize failure messages
18+
19+
use std::fs;
20+
use std::path::{Path, PathBuf};
21+
22+
use run_make_support::rustc;
23+
24+
/// Guard that restores permissions on the incremental directory on drop,
25+
/// to ensure cleanup is possible
26+
struct IncrDirCleanup;
27+
28+
fn main() {
29+
let _cleanup = IncrDirCleanup;
30+
31+
// Build the poison proc-macro crate
32+
rustc().input("poison/lib.rs").crate_name("poison").crate_type("proc-macro").run();
33+
34+
let poison_dylib = find_proc_macro_dylib("poison");
35+
36+
// Incremental compile with the poison macro active
37+
let out = rustc()
38+
.input("foo.rs")
39+
.crate_type("rlib")
40+
.incremental("incr")
41+
.extern_("poison", &poison_dylib)
42+
.env("POISON_INCR_DIR", "incr")
43+
.run();
44+
45+
out.assert_stderr_contains("note: did not finalize incremental compilation session directory");
46+
out.assert_stderr_contains(
47+
"help: the next build will not be able to reuse work from this compilation",
48+
);
49+
out.assert_stderr_not_contains("internal compiler error");
50+
}
51+
52+
impl Drop for IncrDirCleanup {
53+
fn drop(&mut self) {
54+
let incr = Path::new("incr");
55+
if !incr.exists() {
56+
return;
57+
}
58+
59+
#[cfg(unix)]
60+
restore_permissions(incr);
61+
}
62+
}
63+
64+
/// Recursively restore write permissions so rm -rf works after the chmod trick
65+
#[cfg(unix)]
66+
fn restore_permissions(path: &Path) {
67+
use std::os::unix::fs::PermissionsExt;
68+
if let Ok(entries) = fs::read_dir(path) {
69+
for entry in entries.filter_map(|e| e.ok()) {
70+
if entry.file_type().map_or(false, |ft| ft.is_dir()) {
71+
let mut perms = match fs::metadata(entry.path()) {
72+
Ok(m) => m.permissions(),
73+
Err(_) => continue,
74+
};
75+
perms.set_mode(0o755);
76+
let _ = fs::set_permissions(entry.path(), perms);
77+
}
78+
}
79+
}
80+
}
81+
82+
/// Locate the compiled proc-macro dylib by scanning the current directory.
83+
fn find_proc_macro_dylib(name: &str) -> PathBuf {
84+
let prefix = if cfg!(target_os = "windows") { "" } else { "lib" };
85+
86+
let ext: &str = if cfg!(target_os = "macos") {
87+
"dylib"
88+
} else if cfg!(target_os = "windows") {
89+
"dll"
90+
} else {
91+
"so"
92+
};
93+
94+
let lib_name = format!("{prefix}{name}.{ext}");
95+
96+
for entry in fs::read_dir(".").unwrap() {
97+
let entry = entry.unwrap();
98+
let name = entry.file_name();
99+
let name = name.to_str().unwrap();
100+
if name == lib_name {
101+
return entry.path();
102+
}
103+
}
104+
105+
panic!("could not find proc-macro dylib for `{name}`");
106+
}

0 commit comments

Comments
 (0)