From 42acdd88410b69e87a32c14cf649224453a97f4f Mon Sep 17 00:00:00 2001 From: Agochar Bhatia Date: Sat, 14 Nov 2026 17:59:37 -0500 Subject: [PATCH 1/2] Preserve local flake-compat wrapper changes during conversion --- flake.nix | 2 + src/cli/cmd/convert.rs | 304 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 290 insertions(+), 16 deletions(-) diff --git a/flake.nix b/flake.nix index a4ddc84..b8193c1 100644 --- a/flake.nix +++ b/flake.nix @@ -57,6 +57,8 @@ doCheck = true; + nativeCheckInputs = [ final.gitMinimal ]; + nativeBuildInputs = with final; [ pkg-config final.buildPackages.rustPlatform.bindgenHook diff --git a/src/cli/cmd/convert.rs b/src/cli/cmd/convert.rs index 0797e89..8d6e2dd 100644 --- a/src/cli/cmd/convert.rs +++ b/src/cli/cmd/convert.rs @@ -337,22 +337,32 @@ impl ConvertSubcommand { let is_a_git_repo = git_toplevel.success(); if is_a_git_repo { - let files = tokio::process::Command::new("git") - .args(["ls-files ", "--modified ", "--full-name"]) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .stdin(Stdio::null()) - .output() - .await?; - let output = std::str::from_utf8(&files.stdout)?; - - for line in output.lines() { - if line.contains("shell.nix") { - shell_nix_clean = false; - } - if line.contains("default.nix") { - default_nix_clean = false; - } + // Check both files before writing either one. A failed inspection must not + // look like a clean file, and staged or untracked content also belongs to the user. + for (path, clean) in [ + (SHELL_NIX, &mut shell_nix_clean), + (DEFAULT_NIX, &mut default_nix_clean), + ] { + let status = tokio::process::Command::new("git") + .args([ + "status", + "--porcelain=v1", + "--untracked-files=all", + "--ignored", + "--", + path, + ]) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .stdin(Stdio::null()) + .output() + .await?; + color_eyre::eyre::ensure!( + status.status.success(), + "Failed to inspect Git state for {path}: {}", + String::from_utf8_lossy(&status.stderr).trim() + ); + *clean = status.stdout.is_empty(); } } @@ -835,3 +845,265 @@ mod test { ); } } + +#[cfg(all(test, unix))] +mod local_edit_regression { + use std::path::Path; + use std::process::{Command, Output}; + + const CHILD: &str = "cli::cmd::convert::local_edit_regression::fixture_child"; + const CHILD_DONE: &str = "fh conversion fixture completed"; + const WRAPPERS: [&str; 2] = ["shell.nix", "default.nix"]; + + // --- Fixture helpers: isolate real Git and file writes from the caller's repository. --- + fn isolated(command: &mut Command) -> &mut Command { + for (key, _) in std::env::vars_os() { + if key.to_string_lossy().starts_with("GIT_") { + command.env_remove(key); + } + } + command + .env("GIT_CONFIG_NOSYSTEM", "1") + .env("GIT_CONFIG_GLOBAL", "/dev/null") + .env("GIT_CONFIG_SYSTEM", "/dev/null") + } + + fn git(root: &Path, args: &[&str]) -> Output { + let output = isolated(&mut Command::new("git")) + .args([ + "-c", + "user.name=Test", + "-c", + "user.email=test@fixture.invalid", + "-c", + "commit.gpgsign=false", + "-c", + "core.hooksPath=/dev/null", + ]) + .args(args) + .current_dir(root) + .output() + .expect("run fixture Git"); + assert!( + output.status.success(), + "git {args:?}: {}", + String::from_utf8_lossy(&output.stderr) + ); + output + } + + fn legacy_wrapper(name: &str) -> String { + format!( + "(import (fetchTarball \"{}/old.tar.gz\") {{ src = ./.; }}).{}\n", + super::FLAKE_COMPAT_MARKER, + if name == "shell.nix" { + "shellNix" + } else { + "defaultNix" + } + ) + } + + fn repository(track_wrappers: bool) -> tempfile::TempDir { + let root = tempfile::tempdir().unwrap(); + git(root.path(), &["init", "--quiet"]); + for name in WRAPPERS { + std::fs::write(root.path().join(name), legacy_wrapper(name)).unwrap(); + } + if track_wrappers { + git(root.path(), &["add", "--", "shell.nix", "default.nix"]); + } + git( + root.path(), + &["commit", "--quiet", "--allow-empty", "-m", "fixture"], + ); + root + } + + /// Run the actual private helper without changing the test process's global directory. + fn convert(root: &Path, expect_error: bool) { + let output = isolated(&mut Command::new(std::env::current_exe().unwrap())) + .args(["--exact", CHILD, "--ignored", "--nocapture"]) + .env("FH_CONVERT_FIXTURE", "1") + .env( + "FH_CONVERT_EXPECT_ERROR", + if expect_error { "1" } else { "0" }, + ) + .current_dir(root) + .output() + .unwrap(); + let stdout = String::from_utf8_lossy(&output.stdout); + assert!( + output.status.success(), + "{stdout}\n{}", + String::from_utf8_lossy(&output.stderr) + ); + assert!( + stdout.contains(CHILD_DONE), + "fixture child did not run: {stdout}" + ); + } + + fn assert_converted(root: &Path, name: &str) { + let contents = std::fs::read_to_string(root.join(name)).unwrap(); + assert!(contents.contains("lock.nodes.flake-compat.locked")); + assert!(contents.ends_with(if name == "shell.nix" { + ".shellNix\n" + } else { + ".defaultNix\n" + })); + assert_ne!(contents, legacy_wrapper(name)); + } + // --- End fixture helpers. --- + + #[tokio::test] + #[ignore = "invoked only by isolated fixture subprocesses"] + async fn fixture_child() { + assert_eq!(std::env::var("FH_CONVERT_FIXTURE").as_deref(), Ok("1")); + let command = super::ConvertSubcommand { + flake_path: "flake.nix".into(), + dry_run: false, + api_addr: url::Url::parse("http://127.0.0.1:1").unwrap(), + }; + let result = command.fixup_flake_compat_nix_files().await; + if std::env::var("FH_CONVERT_EXPECT_ERROR").as_deref() == Ok("1") { + assert!( + result.is_err(), + "Git inspection failure must not authorize writes" + ); + } else { + result.unwrap(); + } + println!("{CHILD_DONE}"); + } + + #[test] + fn unstaged_changes_are_preserved() { + for name in WRAPPERS { + let root = repository(true); + let edited = format!("{}# local customization\n", legacy_wrapper(name)); + std::fs::write(root.path().join(name), &edited).unwrap(); + convert(root.path(), false); + assert_eq!( + std::fs::read_to_string(root.path().join(name)).unwrap(), + edited + ); + } + } + + #[test] + fn staged_and_mixed_changes_are_preserved() { + for name in WRAPPERS { + for mixed in [false, true] { + let root = repository(true); + let staged = format!("{}# staged customization\n", legacy_wrapper(name)); + std::fs::write(root.path().join(name), &staged).unwrap(); + git(root.path(), &["add", "--", name]); + let edited = if mixed { + format!("{staged}# unstaged customization\n") + } else { + staged.clone() + }; + std::fs::write(root.path().join(name), &edited).unwrap(); + convert(root.path(), false); + assert_eq!( + std::fs::read_to_string(root.path().join(name)).unwrap(), + edited + ); + assert_eq!( + git(root.path(), &["show", &format!(":{name}")]).stdout, + staged.as_bytes() + ); + } + } + } + + #[test] + fn untracked_and_ignored_wrappers_are_preserved() { + for ignored in [false, true] { + let root = repository(false); + if ignored { + std::fs::write(root.path().join(".gitignore"), "shell.nix\ndefault.nix\n").unwrap(); + } + convert(root.path(), false); + for name in WRAPPERS { + assert_eq!( + std::fs::read_to_string(root.path().join(name)).unwrap(), + legacy_wrapper(name) + ); + } + } + } + + #[test] + fn clean_wrappers_still_convert() { + let root = repository(true); + convert(root.path(), false); + for name in WRAPPERS { + assert_converted(root.path(), name); + } + } + + #[test] + fn a_dirty_wrapper_does_not_block_its_clean_sibling() { + for name in WRAPPERS { + let root = repository(true); + let edited = format!("{}# local customization\n", legacy_wrapper(name)); + std::fs::write(root.path().join(name), &edited).unwrap(); + convert(root.path(), false); + assert_eq!( + std::fs::read_to_string(root.path().join(name)).unwrap(), + edited + ); + assert_converted( + root.path(), + if name == "shell.nix" { + "default.nix" + } else { + "shell.nix" + }, + ); + } + } + + #[test] + fn missing_and_non_marker_files_are_untouched() { + let root = repository(true); + std::fs::remove_file(root.path().join("shell.nix")).unwrap(); + std::fs::write(root.path().join("default.nix"), "# unrelated contents\n").unwrap(); + convert(root.path(), false); + assert!(!root.path().join("shell.nix").exists()); + assert_eq!( + std::fs::read_to_string(root.path().join("default.nix")).unwrap(), + "# unrelated contents\n" + ); + } + + #[test] + fn outside_git_is_recommendation_only() { + let root = tempfile::tempdir().unwrap(); + for name in WRAPPERS { + std::fs::write(root.path().join(name), legacy_wrapper(name)).unwrap(); + } + convert(root.path(), false); + for name in WRAPPERS { + assert_eq!( + std::fs::read_to_string(root.path().join(name)).unwrap(), + legacy_wrapper(name) + ); + } + } + + #[test] + fn a_failed_git_inspection_preserves_both_files() { + let root = repository(true); + std::fs::write(root.path().join(".git/index"), b"corrupt index").unwrap(); + convert(root.path(), true); + for name in WRAPPERS { + assert_eq!( + std::fs::read_to_string(root.path().join(name)).unwrap(), + legacy_wrapper(name) + ); + } + } +} From 18e2d5b61e1db1a37a1bae9444cb428562998d95 Mon Sep 17 00:00:00 2001 From: Agochar Bhatia Date: Sat, 14 Nov 2026 18:29:36 -0500 Subject: [PATCH 2/2] Ignore inherited Git locations when checking wrappers --- src/cli/cmd/convert.rs | 86 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 85 insertions(+), 1 deletion(-) diff --git a/src/cli/cmd/convert.rs b/src/cli/cmd/convert.rs index 8d6e2dd..9d27f3b 100644 --- a/src/cli/cmd/convert.rs +++ b/src/cli/cmd/convert.rs @@ -328,6 +328,11 @@ impl ConvertSubcommand { let mut default_nix_clean = true; let git_toplevel = tokio::process::Command::new("git") + // Inspect the directory whose wrappers we will write, even inside a Git hook. + .env_remove("GIT_DIR") + .env_remove("GIT_WORK_TREE") + .env_remove("GIT_COMMON_DIR") + .env_remove("GIT_INDEX_FILE") .args(["rev-parse", "--show-toplevel"]) .stdout(Stdio::null()) .stderr(Stdio::null()) @@ -344,6 +349,10 @@ impl ConvertSubcommand { (DEFAULT_NIX, &mut default_nix_clean), ] { let status = tokio::process::Command::new("git") + .env_remove("GIT_DIR") + .env_remove("GIT_WORK_TREE") + .env_remove("GIT_COMMON_DIR") + .env_remove("GIT_INDEX_FILE") .args([ "status", "--porcelain=v1", @@ -922,7 +931,16 @@ mod local_edit_regression { /// Run the actual private helper without changing the test process's global directory. fn convert(root: &Path, expect_error: bool) { - let output = isolated(&mut Command::new(std::env::current_exe().unwrap())) + convert_with_git_environment(root, expect_error, &[]); + } + + fn convert_with_git_environment(root: &Path, expect_error: bool, env: &[(&str, &Path)]) { + let mut child = Command::new(std::env::current_exe().unwrap()); + isolated(&mut child); + for (key, value) in env { + child.env(key, value); + } + let output = child .args(["--exact", CHILD, "--ignored", "--nocapture"]) .env("FH_CONVERT_FIXTURE", "1") .env( @@ -1094,6 +1112,72 @@ mod local_edit_regression { } } + #[test] + fn inherited_git_locations_do_not_hide_local_edits() { + for name in WRAPPERS { + let root = repository(true); + let other = repository(true); + let edited = format!("{}# local customization\n", legacy_wrapper(name)); + std::fs::write(root.path().join(name), &edited).unwrap(); + let git_dir = other.path().join(".git"); + let index = git_dir.join("index"); + convert_with_git_environment( + root.path(), + false, + &[ + ("GIT_DIR", &git_dir), + ("GIT_WORK_TREE", other.path()), + ("GIT_COMMON_DIR", &git_dir), + ("GIT_INDEX_FILE", &index), + ], + ); + assert_eq!( + std::fs::read_to_string(root.path().join(name)).unwrap(), + edited + ); + assert_converted( + root.path(), + if name == "shell.nix" { + "default.nix" + } else { + "shell.nix" + }, + ); + for wrapper in WRAPPERS { + assert_eq!( + std::fs::read_to_string(other.path().join(wrapper)).unwrap(), + legacy_wrapper(wrapper) + ); + } + assert!( + git(other.path(), &["status", "--porcelain"]) + .stdout + .is_empty() + ); + } + } + + #[test] + fn inherited_git_locations_do_not_make_an_outside_directory_a_repository() { + let root = tempfile::tempdir().unwrap(); + let other = repository(true); + for name in WRAPPERS { + std::fs::write(root.path().join(name), legacy_wrapper(name)).unwrap(); + } + let git_dir = other.path().join(".git"); + convert_with_git_environment( + root.path(), + false, + &[("GIT_DIR", &git_dir), ("GIT_WORK_TREE", other.path())], + ); + for name in WRAPPERS { + assert_eq!( + std::fs::read_to_string(root.path().join(name)).unwrap(), + legacy_wrapper(name) + ); + } + } + #[test] fn a_failed_git_inspection_preserves_both_files() { let root = repository(true);