Skip to content

Commit 3259649

Browse files
authored
feat(cache): name the changed env var in cache-miss message
The inline cache-miss reason now names the env var(s) that changed instead of falling back to a generic envs changed message.
1 parent dfcbfe2 commit 3259649

7 files changed

Lines changed: 60 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Changelog
22

3+
- **Changed** Cache misses caused by a tracked env var now name the env var inline, for example `cache miss: env 'NODE_ENV' changed`, instead of the generic `envs changed` message ([#438](https://github.com/voidzero-dev/vite-task/pull/438))
34
- **Fixed** The task cache is now stored in a per-schema-version subdirectory (e.g. `node_modules/.vite/task-cache/v13/`), so switching between branches that pin different Vite+ versions no longer fails with `Unrecognized database version`. Each version keeps its own cache directory; a cache from a different version is ignored rather than aborting the run ([#433](https://github.com/voidzero-dev/vite-task/pull/433))
45
- **Added** A task's `env` and `untrackedEnv` glob patterns now support `!` negation: a `!`-prefixed pattern excludes matching variables (e.g. `["VITE_*", "!VITE_SECRET"]` tracks every `VITE_*` except `VITE_SECRET`) ([#425](https://github.com/voidzero-dev/vite-task/pull/425))
56
- **Fixed** `package.json` and `pnpm-workspace.yaml` files with a UTF-8 BOM no longer fail to parse ([#424](https://github.com/voidzero-dev/vite-task/pull/424))

crates/vite_task/src/session/cache/display.rs

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,34 @@ pub fn detect_spawn_fingerprint_changes(
119119
changes
120120
}
121121

122+
/// Names of the env vars involved in a set of spawn-fingerprint changes, in the
123+
/// order detected. Only env changes are collected; untracked-env and non-env
124+
/// changes are skipped.
125+
fn env_change_names(changes: &[SpawnFingerprintChange]) -> Vec<&Str> {
126+
changes
127+
.iter()
128+
.filter_map(|change| match change {
129+
SpawnFingerprintChange::Env(mismatch) => Some(mismatch.name()),
130+
_ => None,
131+
})
132+
.collect()
133+
}
134+
135+
/// Inline cache-miss reason naming the env var(s) that changed, e.g.
136+
/// `env 'NODE_ENV' changed` or `envs 'A', 'B' changed`. Falls back to the
137+
/// generic `envs changed` when no names are available.
138+
fn format_env_changed_inline(names: &[&Str]) -> Str {
139+
match names {
140+
[] => Str::from("envs changed"),
141+
[name] => vite_str::format!("env '{name}' changed"),
142+
names => {
143+
let quoted: Vec<Str> = names.iter().map(|name| vite_str::format!("'{name}'")).collect();
144+
let joined = quoted.iter().map(Str::as_str).collect::<Vec<_>>().join(", ");
145+
vite_str::format!("envs {joined} changed")
146+
}
147+
}
148+
}
149+
122150
/// Format cache status for inline display (during Start event).
123151
///
124152
/// Returns `Some(formatted_string)` for Hit, Miss with reason, and Disabled, None for `NotFound`.
@@ -145,22 +173,27 @@ pub fn format_cache_status_inline(cache_status: &CacheStatus) -> Option<Str> {
145173
FingerprintMismatch::SpawnFingerprint { old, new } => {
146174
let changes = detect_spawn_fingerprint_changes(old, new);
147175
match changes.first() {
148-
Some(SpawnFingerprintChange::Env(_)) => "envs changed",
176+
Some(SpawnFingerprintChange::Env(_)) => {
177+
format_env_changed_inline(&env_change_names(&changes))
178+
}
149179
Some(
150180
SpawnFingerprintChange::UntrackedEnvAdded { .. }
151181
| SpawnFingerprintChange::UntrackedEnvRemoved { .. },
152-
) => "untracked env config changed",
153-
Some(SpawnFingerprintChange::ProgramChanged) => "program changed",
154-
Some(SpawnFingerprintChange::ArgsChanged) => "args changed",
155-
Some(SpawnFingerprintChange::CwdChanged) => "working directory changed",
156-
None => "configuration changed",
182+
) => Str::from("untracked env config changed"),
183+
Some(SpawnFingerprintChange::ProgramChanged) => {
184+
Str::from("program changed")
185+
}
186+
Some(SpawnFingerprintChange::ArgsChanged) => Str::from("args changed"),
187+
Some(SpawnFingerprintChange::CwdChanged) => {
188+
Str::from("working directory changed")
189+
}
190+
None => Str::from("configuration changed"),
157191
}
158192
}
159-
FingerprintMismatch::InputConfig => "input configuration changed",
160-
FingerprintMismatch::OutputConfig => "output configuration changed",
193+
FingerprintMismatch::InputConfig => Str::from("input configuration changed"),
194+
FingerprintMismatch::OutputConfig => Str::from("output configuration changed"),
161195
FingerprintMismatch::InputChanged { kind, path } => {
162-
let desc = format_input_change_str(*kind, path.as_str());
163-
return Some(vite_str::format!("○ cache miss: {desc}, executing"));
196+
format_input_change_str(*kind, path.as_str())
164197
}
165198
};
166199
Some(vite_str::format!("○ cache miss: {reason}, executing"))

crates/vite_task/src/session/cache/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,18 @@ pub enum EnvMismatch {
155155
Changed { name: Str, old_value: Str, new_value: Str },
156156
}
157157

158+
impl EnvMismatch {
159+
/// The name of the env var that diverged.
160+
#[must_use]
161+
pub const fn name(&self) -> &Str {
162+
match self {
163+
Self::Added { name, .. } | Self::Removed { name, .. } | Self::Changed { name, .. } => {
164+
name
165+
}
166+
}
167+
}
168+
}
169+
158170
impl Display for EnvMismatch {
159171
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
160172
match self {

crates/vite_task_bin/tests/e2e_snapshots/fixtures/cache_miss_reasons/snapshots/env_added.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ initial content
1616
cache miss: env added
1717

1818
```
19-
$ vtt print-file test.txt ○ cache miss: envs changed, executing
19+
$ vtt print-file test.txt ○ cache miss: env 'MY_ENV' changed, executing
2020
initial content
2121
```

crates/vite_task_bin/tests/e2e_snapshots/fixtures/cache_miss_reasons/snapshots/env_removed.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ initial content
1616
cache miss: env removed
1717

1818
```
19-
$ vtt print-file test.txt ○ cache miss: envs changed, executing
19+
$ vtt print-file test.txt ○ cache miss: env 'MY_ENV' changed, executing
2020
initial content
2121
```

crates/vite_task_bin/tests/e2e_snapshots/fixtures/cache_miss_reasons/snapshots/env_value_changed.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ initial content
1616
cache miss: env value changed
1717

1818
```
19-
$ vtt print-file test.txt ○ cache miss: envs changed, executing
19+
$ vtt print-file test.txt ○ cache miss: env 'MY_ENV' changed, executing
2020
initial content
2121
```

crates/vite_task_bin/tests/e2e_snapshots/fixtures/individual_cache_for_env/snapshots/individual_cache_for_env.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ $ vtt print-env FOO
1616
cache miss, different env
1717

1818
```
19-
$ vtt print-env FOO ○ cache miss: envs changed, executing
19+
$ vtt print-env FOO ○ cache miss: env 'FOO' changed, executing
2020
2
2121
```
2222

0 commit comments

Comments
 (0)