Skip to content

Commit 52db0e1

Browse files
committed
Keep hatch unit tests off the host's uv path
utils::hatch and vendor::pypi_hatch read HATCH_ENV_TYPE_VIRTUAL_UV_PATH from the process: a non-empty value switches every Hatch env to uv, so the vendored-wheel and [tool.uv.sources] refusals fire with no file setting. A developer who points Hatch at uv that way sees 18 socket-patch-core unit tests fail on a clean origin/main checkout (`cargo test -p socket-patch-core --lib`); CI passes only because no job sets the var. Both reads now go through hatch::uv_path_override(). Production builds read the process env as before; unit-test builds read only a value a test holds with an EnvGuard (test_env::guarded_var), as go_env does since #2057. A new test holds the var to cover the uv branch that the unit tests never reached before, and checks a host value stays hidden. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011f9aeR2MHrmJ9Fh9wmm3X1
1 parent de81f1b commit 52db0e1

2 files changed

Lines changed: 54 additions & 5 deletions

File tree

‎crates/socket-patch-core/src/utils/hatch.rs‎

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,25 @@ fn walk(
601601
Ok(())
602602
}
603603

604+
/// Whether a non-empty `HATCH_ENV_TYPE_VIRTUAL_UV_PATH` switches every
605+
/// Hatch virtual environment to the uv installer, whatever the files say.
606+
pub(crate) fn uv_path_override() -> bool {
607+
uv_path_env().is_some_and(|path| !path.is_empty())
608+
}
609+
610+
#[cfg(not(test))]
611+
fn uv_path_env() -> Option<String> {
612+
std::env::var("HATCH_ENV_TYPE_VIRTUAL_UV_PATH").ok()
613+
}
614+
615+
/// In the unit tests, only a value a test holds with an
616+
/// [`EnvGuard`](crate::test_env::EnvGuard): a developer who points Hatch at
617+
/// uv would otherwise see every pip-installer fixture refused.
618+
#[cfg(test)]
619+
fn uv_path_env() -> Option<String> {
620+
crate::test_env::guarded_var("HATCH_ENV_TYPE_VIRTUAL_UV_PATH")
621+
}
622+
604623
/// Whether hatch.toml carries `metadata`, which then holds the
605624
/// direct-reference permission instead of pyproject.
606625
fn external_metadata(documents: &BTreeMap<&'static str, DocumentMut>) -> bool {
@@ -620,10 +639,7 @@ fn run_round(documents: &mut BTreeMap<&'static str, DocumentMut>, round: &mut Ro
620639
}
621640
}
622641
}
623-
round.uv_sources = uv_source_names(
624-
documents,
625-
std::env::var("HATCH_ENV_TYPE_VIRTUAL_UV_PATH").is_ok_and(|path| !path.is_empty()),
626-
);
642+
round.uv_sources = uv_source_names(documents, uv_path_override());
627643
round.apply = false;
628644
if let Err(error) = walk(documents, round) {
629645
round.fail_rest(&error);
@@ -1722,4 +1738,37 @@ mod tests {
17221738
.unwrap_err()
17231739
.contains("pip installer"));
17241740
}
1741+
1742+
/// `HATCH_ENV_TYPE_VIRTUAL_UV_PATH` turns on the uv refusals with no file
1743+
/// setting, and in the unit tests only a guarded value counts: a
1744+
/// developer's own uv path must not refuse the pip-installer fixtures.
1745+
#[test]
1746+
#[serial_test::serial]
1747+
fn uv_path_env_counts_only_when_a_guard_holds_it() {
1748+
use crate::test_env::EnvGuard;
1749+
const KEY: &str = "HATCH_ENV_TYPE_VIRTUAL_UV_PATH";
1750+
let url = "https://patch.test/six-1.16.0-py2.py3-none-any.whl";
1751+
let inputs = files(
1752+
"[project]\ndependencies = [\"six==1.16.0\"]\n[tool.uv.sources]\nsix = { path = \"/abs/six.whl\" }\n",
1753+
);
1754+
let host = std::env::var_os(KEY);
1755+
std::env::set_var(KEY, "/host/uv");
1756+
assert!(!uv_path_override(), "host value hidden");
1757+
assert!(rewrite(&inputs, "six", "1.16.0", url).is_ok());
1758+
{
1759+
let _uv = EnvGuard::set(KEY, "/usr/bin/uv");
1760+
assert!(uv_path_override());
1761+
assert!(rewrite(&inputs, "six", "1.16.0", url)
1762+
.unwrap_err()
1763+
.contains("[tool.uv.sources]"));
1764+
}
1765+
{
1766+
let _empty = EnvGuard::set(KEY, "");
1767+
assert!(!uv_path_override(), "an empty value is unset");
1768+
}
1769+
match host {
1770+
Some(value) => std::env::set_var(KEY, value),
1771+
None => std::env::remove_var(KEY),
1772+
}
1773+
}
17251774
}

‎crates/socket-patch-core/src/vendor/pypi_hatch.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ async fn require_no_raw_workspace(
177177
}
178178

179179
fn require_pip_installer() -> Result<(), Failure> {
180-
if std::env::var("HATCH_ENV_TYPE_VIRTUAL_UV_PATH").is_ok_and(|path| !path.is_empty()) {
180+
if crate::utils::hatch::uv_path_override() {
181181
return Err(("pypi_hatch_unsupported", "vendored Hatch wheels require the pip installer: uv does not enforce local wheel fragment hashes".into()));
182182
}
183183
Ok(())

0 commit comments

Comments
 (0)