Repository navigation
feat(auto-build): add vite:auto_build task to build stale assets up front - #59
Merged
Merged
Conversation
AutoBuild's mutex only covers one process, so parallel test workers, or a server beside a test run, each find stale assets and run `vite build` into the same directory at once. Vite empties that directory before writing, so one process can read a missing or half-written manifest while another builds. The README worked around this by telling parallel runners to turn auto build off. Builds now hold an exclusive lock on tmp/rails-vite-build-<build_dir>.lock (outside the directory Vite empties). A process that waited checks freshness again and reuses the build it waited for.
A failed build leaves the manifest untouched, so the assets stay stale and every following request runs the whole build again, only to log "build failed" and serve the previous assets. With several processes, each one repeats it. The lock file now records the failed sources' timestamp, so no process rebuilds them until a source changes, and the request raises RailsVite::BuildError, as a missing manifest or entry already does, instead of rendering with stale assets. `rake vite:build` still builds them, and a fresh manifest ends the failure.
…tions Emit an event around each auto build, including the time spent waiting for another process's build, so apps can log or subtract build time; for example, a parallel test runner can keep the build out of the runtime it records for the spec file that happened to trigger it.
brodienguyen
force-pushed
the
auto-build-rake-task
branch
from
October 25, 2026 04:57
e4f08e0 to
ea3d9e3
Compare
A process that finds a recorded failure raises BuildError without running Vite, so pointing at the errors in the output above was wrong. It now says the build failed earlier and how to see its errors.
…uest AutoBuild.build_if_stale runs the middleware's staleness check, lock and failure record without a request, under the same conditions the engine uses to add the middleware. AutoBuild.enabled? now holds those conditions for both.
…ront A parallel test runner can build once before it forks its workers, instead of the first worker's request building while the others wait or compete for CPU. The task does nothing when assets are fresh, the dev server runs or auto build is off, and fails with BuildError when the build fails.
brodienguyen
force-pushed
the
auto-build-rake-task
branch
from
October 25, 2026 05:03
ea3d9e3 to
f0ecff1
Compare
stale? checked File.exist? and then read File.mtime outside the build lock. Vite empties the build directory first, so a manifest another process was rebuilding could vanish between the two calls, and the request raised Errno::ENOENT instead of waiting for that build. Read the mtime once and treat a missing manifest as stale; the build lock then waits for the other build and checks again. One stat call instead of two also makes the per-request check faster.
brodienguyen
marked this pull request as ready for review
November 7, 2026 13:10
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
After #58, parallel test workers share a single auto build instead of each running their own. That fixed the clashes, but the build still happens in the middle of the test run: whichever worker gets to a page first starts it, the others wait on the lock, and the build eats CPU the remaining workers would have used. For us that's about 20 seconds of build time landing on whatever spec files happened to run first. It skews the per-file timings our runner uses to split work, and it keeps tripping a spec runtime check we have.
The simple answer is to build once before the workers start.
rake vite:builddoesn't quite fit that job, though. It always does a full build, even when nothing has changed, and it doesn't know about #58's lock or remembered failures. What I wanted was "whatever auto build would do on the first request, but do it now", so that's what this adds:If the assets are fresh, it does nothing and returns straight away. If they're stale, it builds them, or waits if another process is already building. If the build fails, it raises
RailsVite::BuildError, so the command exits non-zero and the test run never starts. It follows the same rules as the middleware, so it also does nothing when auto build is off or a dev server is running.The same thing is available from Ruby as
RailsVite::AutoBuild.build_if_stale, for scripts that start their own workers. Under the hood, the middleware'scallnow goes through that same method, and the "is auto build on?" check lives in one place (AutoBuild.enabled?), shared by the engine and the task. Nothing changes for existing apps.A couple of things I left as they are, but I'm happy to change either:
test:preparestill runsvite:build. Switching it tovite:auto_buildwould skip unnecessary rebuilds forbin/rails test, but it changes current behaviour, so I didn't want to slip it in here.Repro
The script below fakes a parallel run: 4 forked workers, each timing its first request through the middleware, the way a test runner times its first spec file. It runs once with the build left to the first request and once with
vite:auto_buildbefore the workers start. Then it runs the task on fresh assets and with a failing build.vite buildis replaced by a shell script that sleeps for 1 second, so you don't need Node.repro.rbResults against #58 and against this branch:
vite:auto_build, then start workersvite:auto_buildwith fresh assetsvite:auto_buildwith a failing buildBuildError, exits 1The build still takes the same time. It just happens before the workers start, so it isn't charged to any spec.
Also: a manifest that disappears during another process's build
Running the task alongside parallel workers turned up a race in
stale?, so this PR also carries one small fix for it (1fe3c46).stale?calledFile.exist?and thenFile.mtimeoutside the build lock. Vite empties the build directory before it writes, so when another process was rebuilding, the manifest could vanish between those two calls. The request then raisedErrno::ENOENTinstead of waiting for that build. We hit it in CI as a flaky request spec.Now
stale?reads the mtime once and treats a missing manifest as stale. Stale takes the build lock, which waits for the other build and then checks again, so the request serves the fresh build. The new testtest_waits_when_the_manifest_vanishes_under_another_process_buildraisesErrno::ENOENTwithout the fix and passes with it:bundle exec ruby -Ilib -Itest test/auto_build_test.rb -n test_waits_when_the_manifest_vanishes_under_another_process_buildOne stat call instead of two also makes the per-request check on fresh assets a little cheaper:
build_if_staleon fresh assets, 200,000 calls, median of 11I'm happy to split this into its own PR if you'd rather review it separately.