Skip to content

rails tailwindcss:watch issue on Windows 10 / Sprockets::Rails::Helper::AssetNotPrecompiledError #118

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
allanclloyds opened this issue Dec 29, 2021 · 3 comments · Fixed by #156

Comments

@allanclloyds
Copy link

I don't know if this is a tailwindcss-rails issue or something else - I'm a bit rusty as a Rails dev now, but hopefully this issue helps someone else as a workaround.

A fresh install of Rails 7 on Ruby 2.7.5 x64-mingw32 (Windows 10) with --css=tailwind and slim templates gives Sprockets::Rails::Helper::AssetNotPrecompiledError in development.

Asset `tailwind.css` was not declared to be precompiled in production.
Declare links to your assets in `app/assets/config/manifest.js`.

  //= link tailwind.css

and restart your server

application.html.slim:

html
  head
    = csrf_meta_tags
    = csp_meta_tag
    meta name="viewport" content="width=device-width, initial-scale=1.0"
    = stylesheet_link_tag "tailwind", "inter-font", "data-turbo-track": "reload"
    = stylesheet_link_tag "application", "data-turbo-track": "reload"
    = javascript_importmap_tags

bin/dev is a bash script and doesn't run on Windows, but it just installs and runs foreman.

foreman start -f Procfile.dev throws an error

C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/foreman-0.87.2/lib/foreman/process.rb:54:in `spawn': Exec format error - bin/rails server -p 3000 (Errno::ENOEXEC)

rails tailwindcss:watch runs and quits without an error and without building tailwind.css

The workaround is to manually run tailwindcss -i app/assets/stylesheets/application.tailwind.css -o app/assets/builds/tailwind.css -c config/tailwind.config.js -w which works okay, and builds tailwind.css in app/assets/builds so the app doesn't throw an exception.

@jsvcycling
Copy link

Running foreman manually fails for the same reason you can't run bin/dev (Windows doesn't understand how to run bin/rails) so that's not surprising.

It should also be noted that the suggested action by Rails (adding //= link tailwind.css) get the project running but using the vanilla TailwindCSS configuration; anything setup in config/tailwind.config.js isn't used. So it's definitely not the "correct" resolution.

@pietmichal
Copy link
Contributor

I'm experiencing the same problem with Windows 10.

The workaround removed the error.

@pietmichal
Copy link
Contributor

pietmichal commented Mar 10, 2022

I've modified the gem on my machine so the rake build task (lib/tasks/build.rake) prepends ruby to tailwind compile command.

TAILWIND_COMPILE_COMMAND = "#{Pathname.new(__dir__).to_s}/../../exe/tailwindcss -i #{Rails.root.join("app/assets/stylesheets/application.tailwind.css")} -o #{Rails.root.join("app/assets/builds/tailwind.css")} -c #{Rails.root.join("config/tailwind.config.js")} --minify"

namespace :tailwindcss do
  desc "Build your Tailwind CSS"
  task :build do
    system "ruby " + TAILWIND_COMPILE_COMMAND
  end

  desc "Watch and build your Tailwind CSS on file changes"
  task :watch do
    system "ruby #{TAILWIND_COMPILE_COMMAND} -w"
  end
end

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

Now, rails tailwindcss:build works as expected.

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 a pull request may close this issue.

3 participants