Image without extension works in Dev environment but raises error in Production
This is a nasty bug that has affected us twice last year and took down Production.
I see a PR was submitted but never merged and there was no explanation if the PR was lacking in some way.
https://github.com/rails/sprockets-rails/pull/461/files
This is currently an issue in Rails: https://github.com/rails/rails/issues/42784 Previous issue in Rails: https://github.com/rails/rails/issues/35651
Expected behavior
If an image is missing extension in Development environment, it should raise Sprockets::Rails::Helper::AssetNotFound error.
<%= image_tag 'image' %>
Actual behavior
If the image is missing extension in Development environment, it renders fine.
However, it raises Sprockets::Rails::Helper::AssetNotFound error in Production environment.
System configuration
sprockets (4.0.2) sprockets-rails (3.2.2) Rails 6.0.3.4 ruby 2.7.2p137
Example App (Reproduction) - THIS IS IMPORTANT YOUR ISSUE LIKELY WILL NOT BE RESOLVED WITHOUT THIS
There was an example app provided.
https://github.com/gadikotamohan/image_test
https://github.com/rails/rails/issues/35651#issuecomment-509789076
A PR has already been created.
I tried to play with this a bit. I used your Rails and Ruby versions but didn't get the error that raised for you, i.e. Sprockets::Rails::Helper::AssetNotFound
In a development environment both of these work
<%= image_tag "cat" %>
<%= image_tag "cat.jpeg" %>
In a production environment with rails s -e production, I get ActionView::Template::Error (The asset "cat" is not present in the asset pipeline. For when I use both
<%= image_tag "cat" %>
<%= image_tag "cat.jpeg" %>
If I set config.assets.compile = true in production.rb, in production however, the image shows up for me with no errors raised for both
<%= image_tag "cat" %>
<%= image_tag "cat.jpeg" %>
I've seen others file similar issues; I wasn't able to reproduce any of them. Maybe I'm missing something?
Not sure if it could be because you are using both cat and cat.jpeg, or maybe I because am running
RUN bundle exec rails assets:clobber
RUN bundle exec rails assets:precompile
I did some more research and found this is a known issue.
The example given above may be stumbling over a separate issue, which is that the asset pipeline will change "jpeg" extensions into "jpg" extensions, and this will cause both calls to image_tag to fail.
I whipped up a sample app with [email protected] that should demo the issue.
The root path renders a template with:
<div>image_tag with extension:</div>
<%= image_tag 'golden_cat.png' %>
<div>image_tag without extension:</div>
<%= image_tag 'golden_cat' %>
Run the app and visit the root path to see both images. Then in config/environments/development.rb uncomment config.assets.compile = false and restart the dev server. (And, run rails assets:precompile) You should see a Sprockets::Rails::Helper::AssetNotFound raised by image_tag, but only for the second call with no file extension.
Bitten by this issue recently, would be great if this were consistent in development for everyone.