Skip to content

A declared input another process edits while a cached task runs is recorded with its pre-run hash, so a later run at the old content replays output built from the new #1001

Open

Description

@jasonkuhrt

Written by an AI agent (Amp) on behalf of @jasonkuhrt.

A cache entry should not record a declared input's pre-run hash when the task read different content that another process wrote. Today a later run at the pre-run content is a cache hit that replays output built from what the task actually read.

docs/inputs.md says: "When any tracked input file changes, the task's cache is invalidated and the task will re-run." For a file matched by cache.input, lookup_cache hashes it once, before spawn (execute/mod.rs). update_cache skips it after the run (cache_update.rs) and stores that pre-run map as the entry's globbed_inputs (cache/mod.rs), which validation compares on the next lookup.

sequenceDiagram
    participant VP as vp run build
    participant F as input.txt
    participant T as node build.mjs
    participant E as another process
    VP->>F: hash v1 (globbed_inputs)
    VP->>T: spawn
    E->>F: write v2
    T->>F: read v2
    T->>T: write out.txt = v2
    VP->>VP: store entry: input.txt = hash(v1), out.txt = v2
    Note over VP,F: later, input.txt is v1 again
    VP->>F: hash v1, matches the entry
    VP->>VP: hit, restores out.txt = v2
Loading

Related: #905 is the same kind of race for inputs that automatic tracking infers, which are fingerprinted after the task reads them. Declared inputs are fingerprinted before the read, so fixing #905's path alone leaves this one.

This asks about changes made by another process. #913, tracked by #929, enabled caching tasks that rewrite automatically inferred inputs. A task that rewrites a declared input can also be cached; refusing every entry whose declared inputs change during the run would stop caching those runs.

Reproduction

Vite+ 1.7.0, Node 24, Linux x86_64. The task signals that it started, then waits for go before it reads input.txt, so the edit lands after the lookup and before the read.

package.json

{ "name": "repro", "private": true, "type": "module" }

vite.config.ts

export default {
  run: {
    tasks: {
      build: {
        command: 'node build.mjs',
        cache: { input: ['input.txt'], output: ['out.txt'] },
      },
    },
  },
};

build.mjs

import { existsSync, readFileSync, writeFileSync } from 'node:fs';
writeFileSync('started', '');
while (!existsSync('go')) await new Promise((r) => setTimeout(r, 50));
const input = readFileSync('input.txt', 'utf8').trim();
console.log(`read: ${input}`);
writeFileSync('out.txt', input);

repro.sh

set -e
export PATH="$PWD/node_modules/.bin:$PATH"
vp cache clean
rm -f out.txt started go
echo v1 > input.txt
vp run build & pid=$!
until [ -e started ]; do kill -0 "$pid" || exit 1; sleep 0.05; done
echo v2 > input.txt
touch go
wait $pid
echo "after run 1: input=$(cat input.txt) out=$(cat out.txt)"
echo v1 > input.txt
rm -f out.txt started go
touch go
vp run build
echo "after run 2: input=$(cat input.txt) out=$(cat out.txt)"

npm i -D vite-plus@1.7.0, then bash repro.sh:

$ node build.mjs
read: v2
after run 1: input=v2 out=v2
$ node build.mjs ◉ cache hit, replaying
read: v2
---
vp run: cache hit, 80ms saved.
after run 2: input=v1 out=v2
Run input.txt Today Expected
1: edited from v1 to v2 by another process before the task reads it v2 entry records hash(v1), output v2 entry not stored
2, out.txt removed first v1 hit, restores out.txt = v2 miss, runs, writes v1

Notes on a fix

Comparing the complete sets of declared input paths and their hashes before and after the run would catch this reproduction and paths newly matching a glob that remain at the second check. It would be a partial mitigation: an input can be edited and restored, or a matching path can appear and disappear, between the two checks.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions