Skip to content

Heroku Issue: ActionView::Template::Error (The asset "tailwind.css" is not present in the asset pipeline.") #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Dnncha opened this issue Feb 27, 2022 · 28 comments

Comments

@Dnncha
Copy link

Dnncha commented Feb 27, 2022

Hi

Thanks for your work.

I'm running a very simple Rails 7.0.2.2 which I've just set up.

Everything is working swimmingly locally, but when I deploy to heroku I'm getting the following error when I try to view a page:

ActionView::Template::Error (The asset "tailwind.css" is not present in the asset pipeline.)

I have checked to ensure that sassc-rails is disabled in the Gemfile.

I have also tried adding

config.assets.css_compressor = nil

to production.rb, but the error remains.

Any ideas where I am going wrong?

Thanks

@joao99c
Copy link

joao99c commented Mar 3, 2022

I fixed this problem running:
gem install bundler
bundle update --bundler
bundle lock --add-platform x86_64-linux

Commit and push to heroku.
Hope it helps!
JC

@peteoliveira
Copy link

peteoliveira commented Mar 5, 2022

I am seeing the same issue when running tests on github. My configuration is using importmap
config.assets.css_compressor = nil did not work and x86_64-linux is already present in Gemfile.lock

  1. Recordings GET /new returns http success
    Failure/Error: <%= stylesheet_link_tag "tailwind", "inter-font", "data-turbo-track": "reload" %>
    ActionView::Template::Error:
    The asset "tailwind.css" is not present in the asset pipeline.
    ./app/views/layouts/application.html.erb:12:in _app_views_layouts_application_html_erb__1997779[47](https://github.com/nuance-internal/sphere-enterprise/runs/5434230804?check_suite_focus=true#step:6:47)84[49](https://github.com/nuance-internal/sphere-enterprise/runs/5434230804?check_suite_focus=true#step:6:49)135196_69[50](https://github.com/nuance-internal/sphere-enterprise/runs/5434230804?check_suite_focus=true#step:6:50)0' ./spec/requests/recordings_spec.rb:6:in block (3 levels) in '
    --- Caused by: ---
    Sprockets::Rails::Helper::AssetNotFound:
    The asset "tailwind.css" is not present in the asset pipeline.
    ./app/views/layouts/application.html.erb:12:in `_app_views_layouts_application_html_erb__1997779478449135196_69500'

@dmarcoux
Copy link

dmarcoux commented Mar 5, 2022

@peteoliveira: If you have this issue while running tests in your continuous integration, you need to precompile assets with:

bundle exec rake assets:precompile

I had the same issue and this fixed it.

@peteoliveira
Copy link

@dmarcoux That worked, thanks!

@ur5us
Copy link

ur5us commented Mar 15, 2022

@dmarcoux Shouldn’t CI be handled by the Rake tasks?

Rake::Task["assets:precompile"].enhance(["tailwindcss:build"])

if Rake::Task.task_defined?("test:prepare")
  Rake::Task["test:prepare"].enhance(["tailwindcss:build"])
elsif Rake::Task.task_defined?("db:test:prepare")
  Rake::Task["db:test:prepare"].enhance(["tailwindcss:build"])
end

Having said that, I’m having the same issue in CircleCI. I call bin/rails db:test:prepare explicitly before running RSpec but that tailwindcss:build task is never executed. Am I misunderstanding those *test:prepare enhancements?

@dmarcoux
Copy link

dmarcoux commented Mar 15, 2022

I didn't know about those enhance calls. I prepare my test database with bundle exec rake db:reset. I'll give bundle exec rake db:test:prepare a try. It should build Tailwind CSS assets before preparing the test database. This sounds exactly like what we need.

Edit: After giving the rake task db:test:prepare a try, it's not calling the rake task tailwindcss:build. I don't know why, because this should happen.

@ur5us
Copy link

ur5us commented Mar 15, 2022

@dmarcoux

Edit: After giving the rake task db:test:prepare a try, it's not calling the rake task tailwindcss:build. I don't know why, because this should happen.

Yeah, this is exactly what I’m seeing also as mentioned:

I call bin/rails db:test:prepare explicitly before running RSpec but that tailwindcss:build task is never executed.

Maybe @flavorjones or @dhh can shine some light?

@elgrillo83
Copy link

Similar things are happening to me after updating the tailwindcss-rails gem from 2.0.5 to 2.0.8:

# Excerpt from my `Gemfile.lock`:

rails (7.0.2.3)

- tailwindcss-rails (2.0.5-arm64-darwin)
+ tailwindcss-rails (2.0.8-arm64-darwin)

RUBY VERSION
   ruby 3.0.3p157

BUNDLED WITH
   2.2.32

The tests run by GitHub CI were then failing with:

ActionView::Template::Error: couldn't find file 'tailwind.css'

After re-reading https://github.com/rails/tailwindcss-rails#building-for-testing, I was able to fix that by adapting the run script:

# Excerpt from my `.github/workflows/ci.yml`:

- run: bin/rake test
+ run: bin/rake test:all

Not running rake db:test:prepare though, just rake db:setup.

@Timmitry
Copy link

Timmitry commented Jun 28, 2022

I also suddenly had this problem after upgrading tailwindcss-rails from 2.0.5 to 2.0.10.

The problem is present both locally (after removing old assets with bin/rails assets:clobber) and on the continuous integration.

I solved it by adding the following to my system test configuration:

# spec/support/system_test_configuration.rb
# Could alternatively be added to `rails_helper.rb` or `spec_helper.rb`
RSpec.configure do |config|
  ...

  config.before(:suite) do
    if config.files_to_run.any? { |path| path.start_with?(Rails.root.join("spec/system").to_s) }
      Rails.application.load_tasks
      Rake::Task["tailwindcss:build"].invoke
    end
  end
end

Works like a charm. Still seems like a breaking change in a patch version, though 😉

It is not perfect, as it will always compile tailwind before a test run, even if no system tests are involved (e.g. when running bin/rspec spec/models). Edit: Updated the code snippet - it now only compiles if any system tests are run.

@ionmx
Copy link

ionmx commented Jul 21, 2022

@dmarcoux Great! it works like a charm... I'm using the Ruby On Rails CI provided by GitHub Actions, I added the assets precompile before running the tests.

...
# Precompile assets
- name: Assets Precompile
  run: bundle exec rake assets:precompile
# Add or replace test runners here
- name: Run tests
  run: bin/rake
...

@gabrielboeker
Copy link

gabrielboeker commented Aug 18, 2022

none of the suggested solutions work for me which is really frustrating since my productive env is broken right now... staging works fine though.

Edit: Turns out in my gitignore i had /app/assets/builds/* which caused this issue... commenting out the line did the trick. weird.

@seanbjornsson
Copy link

seanbjornsson commented Aug 24, 2022

tailwindcss:build was not sufficient for me to fix this issue. I have to run precompile:assets to resolve this.
Edit: I spoke too soon. This resolves the issue locally but not on Gitlab CI

What ended up working:

# config/application.rb
config.assets.css_compressor = nil

@hendricius
Copy link

hendricius commented Aug 24, 2022

none of the suggested solutions work for me which is really frustrating since my productive env is broken right now... staging works fine though.

Edit: Turns out in my gitignore i had /app/assets/builds/* which caused this issue... commenting out the line did the trick. weird.

/app/assets/builds/*

@gabrielboeker Wouldn't that mean that you have to commit your assets all the time?

@noahd1
Copy link

noahd1 commented Aug 30, 2022

For me, it was helpful to see which tasks tailwindcss-rails hooks into. You can see that here:

https://github.com/rails/tailwindcss-rails/blob/main/lib/tasks/build.rake#L15-L21

If you're using RSpec, you need to figure out a way to build tailwind's CSS or you'll receive this error.

@Timmitry 's solution worked for me in this case. I also put the whole block in a unless ENV["CI"] because CI precompiles assets, which in turn, as per the task definition, builds tailwind as well. Otherwise, tailwind would be built more than once (not the end of the world but simple to avoid).

@flavorjones
Copy link
Member

If someone on this thread would be willing to submit a documentation PR to update the README, I would be very grateful!

@Zuhaib042
Copy link

none of the suggested solutions work for me which is really frustrating since my productive env is broken right now... staging works fine though.

Edit: Turns out in my gitignore i had /app/assets/builds/* which caused this issue... commenting out the line did the trick. weird.
Thank you so much man, I was in deep trouble and spent hours fixing this tailwind asset pipeline issue while deploying rails app on render.
I actually noticed that .gitignore has this /app/assets/builds/* and then when I found out your solution then I went away with it and solved it.

@olivierlacan
Copy link
Contributor

olivierlacan commented Apr 21, 2023

For the goofballs like me who don't notice a compilation error and only notice the failure to precompile tailwind.css I strongly recommend checking our CI output for a CssSyntaxError caused by (for example) usage of a non-existent CSS class with @apply.

Since this happened in my Setup test database stage and (annoyingly) didn't result in a 1 exit code (which would be best IMO, but may not be straightforward to achieve), I assumed wrongly that something had indeed failed to precompile tailwind.css despite my correct usage of db:test:prepare in my setup.

Nope, turned out I derped and Tailwind errored when trying to compile my typo, but then it failed to compile — as you'd expect.

Output from bin/rails db:create db:schema:load test:prepare step in GitHub Actions:

Run bin/rails db:create db:schema:load test:prepare
Database 'maison_test' already exists
["/home/runner/work/maison/maison/vendor/bundle/ruby/3.2.0/gems/tailwindcss-rails-2.0.21-x86_64-linux/exe/x86_64-linux/tailwindcss", "-i", "/home/runner/work/maison/maison/app/assets/stylesheets/application.tailwind.css", "-o", "/home/runner/work/maison/maison/app/assets/builds/tailwind.css", "-c", "/home/runner/work/maison/maison/config/tailwind.config.js", "--minify"]

Rebuilding...
CssSyntaxError: tailwindcss: /home/runner/work/maison/maison/app/assets/stylesheets/application.tailwind.css:86:5: The `grid-flow-cols` class does not exist. If `grid-flow-cols` is a custom class, make sure it is defined within a `@layer` directive.
    at Input.error (/snapshot/tailwindcss/node_modules/postcss/lib/input.js:148:16)
    at AtRule.error (/snapshot/tailwindcss/node_modules/postcss/lib/node.js:60:32)
    at processApply (/snapshot/tailwindcss/lib/lib/expandApplyAtRules.js:376:29)
    at /snapshot/tailwindcss/lib/lib/expandApplyAtRules.js:524:9
    at /snapshot/tailwindcss/lib/processTailwindFeatures.js:55:50
    at Object.Once (/snapshot/tailwindcss/lib/cli/build/plugin.js:251:19)
    at LazyResult.runOnRoot (/snapshot/tailwindcss/node_modules/postcss/lib/lazy-result.js:337:23)
    at LazyResult.runAsync (/snapshot/tailwindcss/node_modules/postcss/lib/lazy-result.js:393:26)
    at async build (/snapshot/tailwindcss/lib/cli/build/index.js:42:9) {
  reason: 'The `grid-flow-cols` class does not exist. If `grid-flow-cols` is a custom class, make sure it is defined within a `@layer` directive.',
  file: '/home/runner/work/maison/maison/app/assets/stylesheets/application.tailwind.css',
  source: '@tailwind base;\n' +
    '\n' +
    '@layer base {\n' +
    '  body {\n' +
    '    @apply text-slate-300;\n' +
    '  }\n' +
    '\n' +
    '  header h1 {\n' +
    '    @apply text-2xl font-bold tracking-wide;\n' +
    '  }\n' +
    '\n' +
    '  header nav ul li {\n' +
    '    @apply font-extrabold tracking-wide;\n' +
    '  }\n' +
    '\n' +
    '  h1, h2, h3, h4, h5 {\n' +
    '    @apply text-slate-600 font-bold;\n' +
    '  }\n' +
    '\n' +
    '  h1 {\n' +
    '    @apply text-2xl my-5;\n' +
    '  }\n' +
    '  h2 {\n' +
      '    text-sm\n' +
      '    px-8\n' +
      '    py-6\n' +
      '    space-y-3\n' +
      '    shrink;\n' +
      '}\n',
    url: 'file:///home/runner/work/maison/maison/app/assets/stylesheets/application.tailwind.css',
    file: '/home/runner/work/maison/maison/app/assets/stylesheets/application.tailwind.css'
  },
  plugin: 'tailwindcss'
}

@fwuensche
Copy link

fwuensche commented Jul 21, 2023

I can confirm that two of the previously suggested solutions work for me.

Run all tests at once with bundle exec rails test:all
Source: cherrypush/cherrypush.com@8088175
image

Explicitly precompile assets before running your tests
Source: cherrypush/cherrypush.com@3967c6e
image

I'm sticking to the second solution, this way I can keep unit tests separated from system tests.

You can also check the builds on the two last commits of this pull request: cherrypush/cherrypush.com#80

Hope that helps ❤️

@kalashnikovisme
Copy link

Running assets:precompile in CI for this purpose looks like an overhead.

Enhancing prepare commands with tailwindcss:build is the good idea and it's already implemented here

if Rake::Task.task_defined?("test:prepare")
  Rake::Task["test:prepare"].enhance(["tailwindcss:build"])
elsif Rake::Task.task_defined?("spec:prepare")
  Rake::Task["spec:prepare"].enhance(["tailwindcss:build"])
elsif Rake::Task.task_defined?("db:test:prepare")
  Rake::Task["db:test:prepare"].enhance(["tailwindcss:build"])
end

So, you can just add bin/rake spec:prepare for RSpec tests, for example 🙂

@pas256
Copy link

pas256 commented May 2, 2024

What does rake spec:prepare do? It isn't listed when I run rake -T but somehow it still works and fixed my tests in CI (GitHub Actions). Thanks @kalashnikovisme

@kalashnikovisme
Copy link

@pas256 yeah, as I wrote above, this should have been fixed in the latest versions of the gem.

Happy that everything is ok now 🙂

@fernandokosh
Copy link

I only makethis working after run:

rake tailwindcss:build

@andrew
Copy link

andrew commented Oct 2, 2024

My fix for this error was to make sure that the app/assets/builds directory is in git:

git add app/assets/builds.keep -f

@sebastialonso
Copy link

sebastialonso commented Dec 3, 2024

I'm experiencing this issue in production, even though I've already run or have these:

  • rake assets:precompile already runs tailwindcss:build
  • apps/assets/builds already contains a .keep and also a tailwind.css

Always getting

Sprockets::Rails::Helper::AssetNotFound (The asset "tailwind.css" is not present in the asset pipeline.

Here's a bare bones repo to reproduce: https://github.com/sebastialonso/tailwindcss-rails-dokku

Anybody managed to solve this?

@aksafan
Copy link

aksafan commented Dec 30, 2024

tailwindcss:build was not sufficient for me to fix this issue. I have to run precompile:assets to resolve this. Edit: I spoke too soon. This resolves the issue locally but not on Gitlab CI

What ended up working:

# config/application.rb
config.assets.css_compressor = nil

The only thing that worked for me is adding Rails.application.config.assets.css_compressor = nil to initializers/assets.rb and I've tried everything from this thread and even more from the Internet.

Thanks for the help @seanbjornsson!

@aksafan
Copy link

aksafan commented Dec 30, 2024

none of the suggested solutions work for me which is really frustrating since my productive env is broken right now... staging works fine though.
Edit: Turns out in my gitignore i had /app/assets/builds/* which caused this issue... commenting out the line did the trick. weird.

/app/assets/builds/*

@gabrielboeker Wouldn't that mean that you have to commit your assets all the time?

Oh, I thought I got some kind of glitch or smth, cause I've just baked a sourdough bread by your recipe @hendricius and now I see you here answering about the Ruby. What a great coincidence!

@jeffdill2
Copy link

For those who have mentioned removing /app/assets/builds/* from their .gitignore file, I'd recommend doing this instead of removing that line entirely:

/app/assets/builds/*
!/app/assets/builds/.keep

This will ignore ignore anything in the /builds directory except for the .keep file.

@bilalhussain-dev
Copy link

@peteoliveira: If you have this issue while running tests in your continuous integration, you need to precompile assets with:

bundle exec rake assets:precompile
I had the same issue and this fixed it.

Love it! Problem solved!

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

No branches or pull requests