fix(terminal): write shell integration scripts atomically into a content-keyed folder (#6400)

The shell integration scripts went to a temp folder named by version only and were rewritten in place, so a shell starting while another process rewrote them could source a truncated file, and two builds of the same version with different scripts (a dev build next to a release, two checkouts' tests on one runner) overwrote each other; zsh_restores_zdotdir_and_histfile_for_login_shells failed on macOS CI that way. The folder name now carries a hash of the scripts, and each file is written to a unique temporary name and renamed into place.
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-11-27 15:54:12 +08:00
1 джерело 7ecccb3b75
коміт 2ce7621abb
1 file changed
+21 -3
+21 -3
Переглянути файл
@@ -89,11 +89,22 @@ const FISH_INIT: &str = concat!(
const POWERSHELL_INIT: &str = r#"$openideaClearHost = (Get-Command Clear-Host).ScriptBlock; $openideaClearHandler = { & $openideaClearHost @args; [Console]::Write([char]27 + ']133;OpenIDEA;ClearScrollback' + [char]7) }.GetNewClosure(); Set-Item Function:global:Clear-Host -Value $openideaClearHandler"#;
/// Directory holding the generated startup files (written on demand).
/// Directory holding the generated startup files (written on demand). Keyed by the scripts'
/// content as well as the version, so builds with different scripts (a dev build next to a
/// release, two checkouts on one machine) never rewrite each other's files under a starting shell.
fn scripts_dir() -> PathBuf {
std::env::temp_dir().join(format!("openidea-shell-integration-{}", env!("CARGO_PKG_VERSION")))
use std::hash::{Hash, Hasher};
let mut hasher = std::hash::DefaultHasher::new();
(ZSHENV, ZPROFILE, ZSHRC, ZLOGIN, BASHRC).hash(&mut hasher);
std::env::temp_dir().join(format!(
"openidea-shell-integration-{}-{:016x}",
env!("CARGO_PKG_VERSION"),
hasher.finish()
))
}
/// Writes through a temporary file and a rename, so a shell (or another process doing the
/// same) never reads a half-written script.
fn write_if_changed(path: &Path, content: &str) -> std::io::Result<()> {
if std::fs::read_to_string(path).is_ok_and(|c| c == content) {
return Ok(());
@@ -101,7 +112,14 @@ fn write_if_changed(path: &Path, content: &str) -> std::io::Result<()> {
if let Some(dir) = path.parent() {
std::fs::create_dir_all(dir)?;
}
std::fs::write(path, content)
let name = path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
static NEXT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
let n = NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let tmp = path.with_file_name(format!(".{name}.{}.{n}.tmp", std::process::id()));
std::fs::write(&tmp, content)?;
std::fs::rename(&tmp, path).inspect_err(|_| {
let _ = std::fs::remove_file(&tmp);
})
}
/// Adjusts how `shell` is started so it reports its cwd. `args` are the