From 8403e442021c2dabe44e5462ee2801c73df476f4 Mon Sep 17 00:00:00 2001 From: Albertus Angga Raharja Date: Sun, 22 Nov 2026 17:00:37 +0800 Subject: [PATCH 1/2] fix(plan): evaluate cache.env and cache.untrackedEnv patterns independently MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before #425, `EnvFingerprints::resolve` ignored `!`-prefixed patterns, so chaining `untracked_env` and `fingerprinted_envs` into one pattern set was equivalent to a union. Once #425 enabled `!` negation in `EnvGlobSet`, a negation in either list began excluding names matched by the other — for example, `env: ["VITE_*", "!VITE_DEBUG"]` with `untrackedEnv: ["VITE_DEBUG"]` dropped VITE_DEBUG, and `untrackedEnv: ["CI_*", "!CI_SECRET"]` with `env: ["CI_SECRET"]` dropped CI_SECRET before fingerprinting. Compile each list separately, pass through inherited names matched by either set, and fingerprint inherited values matched by `env`. A negation in one list can no longer exclude a name matched by the other. In particular, keeping a default pass-through name such as CI out of the spawned process environment now requires excluding it in `untrackedEnv`, and in `env` too if that list matches the name. Co-authored-by: Claude Opus 5.5 Co-authored-by: Gemini 4 Neon --- CHANGELOG.md | 1 + crates/vt_plan/src/envs.rs | 153 ++++++++++++++++++++++++++++++------- 2 files changed, 128 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d9d39790c..542caef9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # Changelog +- **Fixed** A `!` pattern in `cache.env` or `cache.untrackedEnv` now excludes variables only from its own list. To keep a default pass-through variable such as `CI` out of a task's environment, exclude it in `cache.untrackedEnv`, and in `cache.env` if that list also matches it ([#1010](https://github.com/voidzero-dev/vite-task/pull/1010)). - **Changed** When automatic file-access tracking cannot record everything a task did, the "Not cached" summary line now includes the specific reason (for example, that the tracking channel ran out of capacity or that installing the seccomp filter failed for a given executable) ([#1054](https://github.com/voidzero-dev/vite-task/pull/1054), [#1056](https://github.com/voidzero-dev/vite-task/pull/1056)). - **Changed** Invalid remote cache endpoint URLs in `cache.remote.url` or `VP_REMOTE_CACHE_URL` (such as missing schemes, non-`http`/`https` schemes, or missing hosts) now fail upfront during configuration and planning instead of warning per task during execution, and `vp run --help` now documents `VP_REMOTE_CACHE`, `VP_REMOTE_CACHE_URL`, and `VP_REMOTE_CACHE_HEADER_` ([#1048](https://github.com/voidzero-dev/vite-task/pull/1048)). - **Changed** Removed `NEXT_*`, `VERCEL`, `VERCEL_*`, `USE_OUTPUT_FOR_EDGE_FUNCTIONS`, `NOW_BUILDER`, `VC_MICROFRONTENDS_CONFIG_FILE_NAME`, `GITHUB_*`, `ACTIONS_ID_TOKEN_REQUEST_URL`, `DOCKER_*`, `BUILDKIT_*`, `COMPOSE_*`, and `*_TOKEN` from the default untracked environment variables passed to cached tasks. Tasks that need any of these variables should declare them explicitly in `cache.env` or `cache.untrackedEnv` ([#1044](https://github.com/voidzero-dev/vite-task/pull/1044)). diff --git a/crates/vt_plan/src/envs.rs b/crates/vt_plan/src/envs.rs index a7aa9cbc9..70366b40a 100644 --- a/crates/vt_plan/src/envs.rs +++ b/crates/vt_plan/src/envs.rs @@ -108,16 +108,17 @@ impl EnvFingerprints { envs: &mut FxHashMap>, Arc>, env_config: &EnvConfig, ) -> Result { - // Collect all envs matching fingerprinted or untracked envs in env_config - *envs = resolve_envs_with_patterns( - envs.iter(), - &env_config - .untracked_env - .iter() - .map(std::convert::AsRef::as_ref) - .chain(env_config.fingerprinted_envs.iter().map(std::convert::AsRef::as_ref)) - .collect::>(), - )?; + // Match each list on its own, so a `!` pattern only excludes names from + // the list it's in. Compile both before modifying `envs`. + let untracked_patterns = EnvGlobSet::new(env_config.untracked_env.iter())?; + let fingerprinted_patterns = EnvGlobSet::new(env_config.fingerprinted_envs.iter())?; + + // Keep envs matching either list + envs.retain(|name, _| { + name.inner().to_str().is_some_and(|name_str| { + untracked_patterns.is_match(name_str) || fingerprinted_patterns.is_match(name_str) + }) + }); // Ensure cached output is colored by default. Skipped if the user // opted into passing `FORCE_COLOR` through (via `env` / `untrackedEnv`) @@ -129,12 +130,11 @@ impl EnvFingerprints { // Resolve fingerprinted envs let mut fingerprinted_envs = BTreeMap::::new(); if !env_config.fingerprinted_envs.is_empty() { - let fingerprinted_env_patterns = EnvGlobSet::new(env_config.fingerprinted_envs.iter())?; for (name, value) in envs.iter() { let Some(name) = name.inner().to_str() else { continue; }; - if !fingerprinted_env_patterns.is_match(name) { + if !fingerprinted_patterns.is_match(name) { continue; } let Some(value) = value.to_str() else { @@ -159,20 +159,6 @@ impl EnvFingerprints { } } -fn resolve_envs_with_patterns<'a>( - env_vars: impl Iterator>, &'a Arc)>, - patterns: &[&str], -) -> Result>, Arc>, vt_glob::env::EnvGlobError> { - let patterns = EnvGlobSet::new(patterns.iter())?; - let envs: FxHashMap>, Arc> = env_vars - .filter_map(|(name, value)| { - let name_str = name.inner().to_str()?; - if patterns.is_match(name_str) { Some((name.clone(), Arc::clone(value))) } else { None } - }) - .collect(); - Ok(envs) -} - #[cfg(test)] mod tests { use vt_graph::config::DEFAULT_UNTRACKED_ENV; @@ -197,6 +183,12 @@ mod tests { EnvValueHash::new(value) } + fn env_pairs(envs: &FxHashMap>, Arc>) -> BTreeMap<&str, &str> { + envs.iter() + .map(|(name, value)| (name.inner().to_str().unwrap(), value.to_str().unwrap())) + .collect() + } + #[test] fn test_force_color_defaults_to_one_when_user_does_not_opt_in() { // The user did not list `FORCE_COLOR` in `env` or `untrackedEnv`, so @@ -301,6 +293,115 @@ mod tests { assert_eq!(result.fingerprinted_envs.get("FORCE_COLOR").copied(), Some(hash("1"))); } + #[test] + fn test_untracked_negation_does_not_cancel_env() { + let untracked = [DEFAULT_UNTRACKED_ENV, &["!CI"]].concat(); + let env_config = create_env_config(&["CI"], &untracked); + let mut envs = create_test_envs(vec![("CI", "true"), ("PATH", "bin")]); + + let result = EnvFingerprints::resolve(&mut envs, &env_config).unwrap(); + + assert_eq!( + env_pairs(&envs), + BTreeMap::from([("CI", "true"), ("FORCE_COLOR", "1"), ("PATH", "bin")]) + ); + assert_eq!(result.fingerprinted_envs, BTreeMap::from([("CI".into(), hash("true"))])); + } + + #[test] + fn test_env_negation_does_not_cancel_untracked() { + let env_config = create_env_config(&["VITE_*", "!VITE_DEBUG"], &["VITE_DEBUG"]); + let mut envs = create_test_envs(vec![("VITE_API_URL", "api"), ("VITE_DEBUG", "true")]); + + let result = EnvFingerprints::resolve(&mut envs, &env_config).unwrap(); + + assert_eq!( + env_pairs(&envs), + BTreeMap::from([("FORCE_COLOR", "1"), ("VITE_API_URL", "api"), ("VITE_DEBUG", "true")]) + ); + assert_eq!( + result.fingerprinted_envs, + BTreeMap::from([("VITE_API_URL".into(), hash("api"))]) + ); + } + + #[test] + fn test_untracked_exclude_all_keeps_only_explicit_env() { + let untracked = [DEFAULT_UNTRACKED_ENV, &["!*"]].concat(); + let env_config = create_env_config(&["PATH", "CI"], &untracked); + let mut envs = create_test_envs(vec![("CI", "true"), ("HOME", "home"), ("PATH", "bin")]); + + let result = EnvFingerprints::resolve(&mut envs, &env_config).unwrap(); + + assert_eq!( + env_pairs(&envs), + BTreeMap::from([("CI", "true"), ("FORCE_COLOR", "1"), ("PATH", "bin")]) + ); + assert_eq!( + result.fingerprinted_envs, + BTreeMap::from([("CI".into(), hash("true")), ("PATH".into(), hash("bin"))]) + ); + } + + #[test] + fn test_env_negation_does_not_suppress_default_untracked_passthrough() { + // `CI` is in `DEFAULT_UNTRACKED_ENV`, so negating it only in `env` + // still lets it pass through untracked; keeping it out of the spawned + // process environment requires negating it in `untrackedEnv`. + let env_config = create_env_config(&["!CI"], DEFAULT_UNTRACKED_ENV); + let mut envs = create_test_envs(vec![("CI", "true"), ("PATH", "bin")]); + + let result = EnvFingerprints::resolve(&mut envs, &env_config).unwrap(); + + assert_eq!( + env_pairs(&envs), + BTreeMap::from([("CI", "true"), ("FORCE_COLOR", "1"), ("PATH", "bin")]) + ); + assert!(result.fingerprinted_envs.is_empty()); + + let untracked = [DEFAULT_UNTRACKED_ENV, &["!CI"]].concat(); + let env_config = create_env_config(&["!CI"], &untracked); + let mut envs = create_test_envs(vec![("CI", "true"), ("PATH", "bin")]); + + let result = EnvFingerprints::resolve(&mut envs, &env_config).unwrap(); + + assert_eq!(env_pairs(&envs), BTreeMap::from([("FORCE_COLOR", "1"), ("PATH", "bin")])); + assert!(result.fingerprinted_envs.is_empty()); + } + + #[test] + fn test_force_color_opt_in_survives_negation_in_other_list() { + // A negation in the other list must not trigger the `FORCE_COLOR=1` fallback. + let env_config = create_env_config(&["FORCE_COLOR"], &["!FORCE_COLOR"]); + let mut envs = create_test_envs(vec![("FORCE_COLOR", "3")]); + + let result = EnvFingerprints::resolve(&mut envs, &env_config).unwrap(); + + assert_eq!(env_pairs(&envs), BTreeMap::from([("FORCE_COLOR", "3")])); + assert_eq!(result.fingerprinted_envs, BTreeMap::from([("FORCE_COLOR".into(), hash("3"))])); + + let env_config = create_env_config(&["*", "!FORCE_COLOR"], &["FORCE_COLOR"]); + let mut envs = create_test_envs(vec![("FORCE_COLOR", "0")]); + + let result = EnvFingerprints::resolve(&mut envs, &env_config).unwrap(); + + assert_eq!(env_pairs(&envs), BTreeMap::from([("FORCE_COLOR", "0")])); + assert!(result.fingerprinted_envs.is_empty()); + } + + #[test] + fn test_invalid_glob_in_either_list_leaves_envs_unmodified() { + for env_config in [create_env_config(&["["], &[]), create_env_config(&[], &["["])] { + let original = create_test_envs(vec![("UNLISTED", "value")]); + let mut envs = original.clone(); + + let result = EnvFingerprints::resolve(&mut envs, &env_config); + + assert!(matches!(result, Err(ResolveEnvError::GlobError { .. }))); + assert_eq!(envs, original); + } + } + #[test] #[cfg(unix)] fn test_task_envs_stable_ordering() { From 43219a317dc97db5d2214f766cd6dfb9a6dcfd77 Mon Sep 17 00:00:00 2001 From: Albertus Angga Raharja Date: Thu, 26 Nov 2026 22:01:51 +0800 Subject: [PATCH 2/2] fix(cache): bump cache schema version for independent env matching Bump CACHE_SCHEMA_VERSION from 24 to 25 so existing local and remote cache entries recorded under the previous cross-list env matching behavior are not replayed now that cache.env and cache.untrackedEnv are evaluated independently. Co-authored-by: Gemini 4 Neon --- crates/vt/src/session/cache/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/vt/src/session/cache/mod.rs b/crates/vt/src/session/cache/mod.rs index 49908b24f..f1f1f67d6 100644 --- a/crates/vt/src/session/cache/mod.rs +++ b/crates/vt/src/session/cache/mod.rs @@ -470,7 +470,7 @@ pub fn split_path(path: &str) -> (Option<&str>, &str) { /// its own cache warm across branch switches, and a cache from a different /// version is simply ignored (it lives in a directory this build never looks /// at) rather than aborting the run. Bumping the version starts a fresh cache. -const CACHE_SCHEMA_VERSION: u32 = 24; +const CACHE_SCHEMA_VERSION: u32 = 25; /// Name of the per-version subdirectory (e.g. `v14`) under the task-cache /// directory that holds the database and output archives for the current