sprockets-rails icon indicating copy to clipboard operation
sprockets-rails copied to clipboard

Intermittent `AssetNotPrecompiledError`

Open EmilioCristalli opened this issue 2 years ago • 4 comments

We are seeing the Sprockets::Rails::Helper::AssetNotPrecompiledError error, but it doesn't happen all the time, and haven't been able to reproduce it. Sorry this issue is going to sound weird :(, but we are not sure what's causing it, or if it's a problem with our config or something in sprockets/sprockets-rails, so would appreciate any help.

We have a Rails application which includes an engine (named admin). The engine has a Sprockets manifest (admin_manifest, which is appended to the main app's app.config.assets.precompile), and the manifest links a couple css and js directories and trees (which resolve, among other things, to the engine's admin/application.css). Then in the engine's layouts/application.html.erb we do <%= stylesheet_link_tag "admin/application", media: "all" %>, which works fine most of the time (both in development and production), but sometimes it raises a Sprockets::Rails::Helper::AssetNotPrecompiledError error. In production the error persists until we rotate the server (using the same code), but in staging it works correctly after we reload the page.

We are not precompiling assets in production, but we have config.assets.compile enabled, so I wouldn't expect it to cause this error (and if we did precompile, i guess this same problem could happen when running rake assets:precompile so the precompilation would miss some files).

It's like for some reason the app.config.assets.precompile is missing the admin_manifest sometimes, or the admin_manifest is missing parts of its content (we have seen this error both with the admin/application.css, and with other js files, which are included in the application layout after the css, so we assume the css was correctly precompiled in those cases)

Relevant configs:

# main app
config.cache_classes = true
config.assets.compile = true
# engine
initializer "admin.assets.precompile" do |app|
  app.config.assets.precompile += %w[admin_manifest]
end

Gem versions: rails: 7.0.3.1 sprockets-rails: 3.4.2 sprockets: 4.1.1

Stacktrace:

[GEM_ROOT]/gems/sprockets-rails-3.4.2/lib/sprockets/rails/helper.rb:372 :in `raise_unless_precompiled_asset`
[GEM_ROOT]/gems/sprockets-rails-3.4.2/lib/sprockets/rails/helper.rb:338 :in `digest_path`
[GEM_ROOT]/gems/sprockets-rails-3.4.2/lib/sprockets/rails/helper.rb:326 :in `asset_path`
[GEM_ROOT]/gems/sprockets-rails-3.4.2/lib/sprockets/rails/helper.rb:103 :in `block in resolve_asset_path`
[GEM_ROOT]/gems/sprockets-rails-3.4.2/lib/sprockets/rails/helper.rb:243 :in `block in resolve_asset`
[GEM_ROOT]/gems/sprockets-rails-3.4.2/lib/sprockets/rails/helper.rb:242 :in `each`
[GEM_ROOT]/gems/sprockets-rails-3.4.2/lib/sprockets/rails/helper.rb:242 :in `detect`
[GEM_ROOT]/gems/sprockets-rails-3.4.2/lib/sprockets/rails/helper.rb:242 :in `resolve_asset`
[GEM_ROOT]/gems/sprockets-rails-3.4.2/lib/sprockets/rails/helper.rb:102 :in `resolve_asset_path`
[GEM_ROOT]/gems/sprockets-rails-3.4.2/lib/sprockets/rails/helper.rb:80 :in `compute_asset_path`
[GEM_ROOT]/gems/actionview-7.0.3.1/lib/action_view/helpers/asset_url_helper.rb:203 :in `asset_path`
[GEM_ROOT]/gems/actionview-7.0.3.1/lib/action_view/helpers/asset_url_helper.rb:348 :in `stylesheet_path`
[GEM_ROOT]/gems/actionview-7.0.3.1/lib/action_view/helpers/asset_tag_helper.rb:180 :in `block in stylesheet_link_tag`
[GEM_ROOT]/gems/actionview-7.0.3.1/lib/action_view/helpers/asset_tag_helper.rb:179 :in `map`
[GEM_ROOT]/gems/actionview-7.0.3.1/lib/action_view/helpers/asset_tag_helper.rb:179 :in `stylesheet_link_tag`
[GEM_ROOT]/gems/sprockets-rails-3.4.2/lib/sprockets/rails/helper.rb:186 :in `block in stylesheet_link_tag`
[GEM_ROOT]/gems/sprockets-rails-3.4.2/lib/sprockets/rails/helper.rb:184 :in `map`
[GEM_ROOT]/gems/sprockets-rails-3.4.2/lib/sprockets/rails/helper.rb:184 :in `stylesheet_link_tag`

EmilioCristalli avatar Dec 12 '22 19:12 EmilioCristalli

@EmilioCristalli Did you end up finding a solution to this problem? This all sounds extremely similar to a problem I'm having.

derekharmel avatar Oct 16 '24 16:10 derekharmel

nope 😞 we just disabled this check as a workaround app.config.assets.check_precompiled_asset = false

EmilioCristalli avatar Oct 16 '24 17:10 EmilioCristalli

@EmilioCristalli Ah, bummer, but good to know. Thanks for the response!

derekharmel avatar Oct 16 '24 22:10 derekharmel

We started having this issue after upgrading to rails 7. We have assets.compile disabled and yet it appears at random

solerman avatar Dec 05 '24 11:12 solerman

We've been hitting htis for a while too, and I finally dug into it. It looks like its likely a race condition with the use of a concurrent map. Looks like an asset can get loaded asyncronously more than once and if something happens in just the wrong enough order, the cache can be written weird and then this happens. I'm not gonna try to debug it further than this, so hopefully someone braver than i can pick up the torch. I write Ruby because I don't want to deal with these kind of concurrency bugs!

botandrose avatar Jun 20 '25 16:06 botandrose