Skip to content

Add Spring.after_environment_load hook for post-Rails preloading#754

Merged
rafaelfranca merged 3 commits into
rails:mainfrom
Shopify:add-after-environment-load-hook
Apr 30, 2026
Merged

Add Spring.after_environment_load hook for post-Rails preloading#754
rafaelfranca merged 3 commits into
rails:mainfrom
Shopify:add-after-environment-load-hook

Conversation

@jahfer
Copy link
Copy Markdown
Contributor

@jahfer jahfer commented Apr 30, 2026

Summary

Adds a Spring.after_environment_load hook that runs immediately after the Rails application environment is loaded in the Spring server process, but before GC.compact/Process.warmup and before the server enters its wait loop.

This hook enables applications to preload expensive test infrastructure (e.g. test helpers, fixture caches, datastore connections) into the Spring server so that forked test workers inherit that work via copy-on-write instead of paying it on every invocation.

API

# config/spring.rb
Spring.after_environment_load do
  require "test_helper"
end

The block receives no arguments and runs once per server boot. It is intentionally not called when Spring is disabled or when the environment is reloaded.

Motivation

In Shopify's monolith, bin/test pays ~2.4s of require cost for its test_helper on every test run even with Spring hot. By preloading this file into the Spring server, forked workers inherit the already-required code via CoW, reducing time-to-first-test significantly for inner-loop development.

Implementation

  • Spring::Configuration gains an @after_environment_load array and after_environment_load registration method (same pattern as after_fork).
  • Spring::Application#load_application calls the callbacks after require app_env returns.

@jahfer jahfer force-pushed the add-after-environment-load-hook branch 4 times, most recently from 0c515af to 7b9ce61 Compare April 30, 2026 17:21
Co-authored-by: AI (Pi/Claude Sonnet 4.6) <noreply@pi.dev>
@jahfer jahfer force-pushed the add-after-environment-load-hook branch from 7b9ce61 to dabbb56 Compare April 30, 2026 17:22
@jahfer jahfer marked this pull request as ready for review April 30, 2026 17:31
@rafaelfranca rafaelfranca enabled auto-merge April 30, 2026 22:21
@rafaelfranca rafaelfranca merged commit 9db0173 into rails:main Apr 30, 2026
28 checks passed
@rafaelfranca rafaelfranca deleted the add-after-environment-load-hook branch May 1, 2026 00:59
@rafaelfranca rafaelfranca restored the add-after-environment-load-hook branch May 1, 2026 00:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants