Skip to content
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

Built CSS is only picked up by second precompilation #48

Closed
steerio opened this issue Nov 13, 2023 · 10 comments
Closed

Built CSS is only picked up by second precompilation #48

steerio opened this issue Nov 13, 2023 · 10 comments

Comments

@steerio
Copy link

steerio commented Nov 13, 2023

When one runs rake assets:precompile, the dartsass:build task is indeed run before the precompilation task, but it appears that the latter won't pick up assets written to the app/assets/builds folder by the former, unless the files had been present there before startup in the first place.

To reproduce the error:

  1. Create a brand new Rails app, version 7.1.1.
  2. Add dartsass-rails 0.5.0 to the Gemfile and bundle.
  3. Run rails dartsass:install.
  4. Put some styles in app/assets/stylesheets/application.scss.
  5. Have at least one template or layout that does stylesheet_link_tag "application".
  6. Run rake assets:precompile.
  7. Verify that there is an app/assets/builds/application.css file, so dartsass:build has run successfully.
  8. Observe that neither is application-<hash>.css in the Sprockets manifest (public/assets/.sprockets-manifest-*.json), nor has it been packed up in public/assets.
  9. Run rake assets:precompile again.
  10. Observe that the task gave output corresponding to your SCSS file (and any additional assets that were referenced by it that are not referenced by other assets or your manifest.js), but nothing else.
  11. Verify that application-<hash>.css is now in the Sprockets manifest and appears in public/assets as well.

Subsequent runs of rake assets:precompile will not yield any further changes (provided that none of the assets were modified).

@lbrito1
Copy link

lbrito1 commented Feb 24, 2024

I think I'm running into this too. Did you figure out a solution @steerio ?
https://discuss.rubyonrails.org/t/assets-precompile-skips-application-css-dartsass-rails/85133

@januszm
Copy link

januszm commented Mar 4, 2024

This indeed is a problem, still in March 2024 after 4 months

@januszm
Copy link

januszm commented Mar 4, 2024

@steerio do you use esbuild ?
I noticed that this works correctly with webpack (shakapacker), problems started when I migrated to esbuild

@lbrito1
Copy link

lbrito1 commented Mar 4, 2024

I fixed this by adding back app/assets/builds/.keep, which I had deleted at some point.

I have this on my gitignore and dockerignore, which I believe is the default for Rails these days:

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

Even though that dir gets recreated by dartsass-rails when running dartsass:build and the dir contains the correct compiled files, propshaft "ignored" it when running rails assets:precompile for the first time. This was only happening in production, so there's likely an ordering issue somewhere, perhaps something like:

  1. RUN rails assets:precompile in Dockerfile
  2. propshaft does its thing and outputs the digested files to /public/assets; finds nothing in app/assets/builds
  3. dartsass-rails is called and compiles the css files into app/assets/builds
  4. The image ends up with compiled application.css in app/assets/builds, but no corresponding digested file in public/assets

Something like that. Or maybe I just configured propshaft and/or dartsass-rails wrong. Who knows.

@januszm
Copy link

januszm commented Mar 4, 2024

Oh shoot, It's because of missing app/assets/builds/.keep indeed ! I just noticed this in my shakapacker=>esbuild migration branch

removed: app/assets/builds/.keep

I think you can resolve this issue then

@januszm
Copy link

januszm commented Mar 4, 2024

Actually I would keep the issue and add a PR that resolves it, I think dartsass should create app/assets/builds if one does not exist.

Something similar to what other rails projects use, e.g.

FileUtils.mkdir_p(builds_root)
keep = File.join(builds_root, ".keep")
FileUtils.touch(keep)

@nimmolo
Copy link

nimmolo commented Apr 22, 2024

Yes. I agree this seems necessary @januszm. It's a gotcha that actually brought our server down for hours yesterday, it took us awhile to figure what the heck could be wrong.

What we tried was a very minimal PR to switch a legacy Rails 7.1.3 app to dartsass-rails from dartsass-sprockets. Everything worked locally and in CI, but the deploy failed on production during assets:precompile with the above error.

Even reverting the changes wouldn't allow the server to boot — at that point the builds folder did exist, and threw off the assets:precompile process in a different way. Very frustrating and should not happen.

@januszm
Copy link

januszm commented Apr 22, 2024

I've just added this as an extra deployment step:

yarn install; bin/rails assets:precompile

if something is missing, it will do the work, if node_modules are installed and all assets precompiled already, it exits really fast so it won't delay the deployment.

@nimmolo
Copy link

nimmolo commented Apr 23, 2024

thanks @januszm - Hm. Our deploy script already tries to run assets:precompile - that's where we're getting the failure. But we don't have any JS to compile, we're only using importmaps, so we don't have a yarn step.

@johnfisherman
Copy link

I fixed this by adding back app/assets/builds/.keep, which I had deleted at some point.

I have this on my gitignore and dockerignore, which I believe is the default for Rails these days:

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

For some reason my .keep file was also lost somewhere in my development process. It took me 6 hours to figure this out, but hey, at least now I have an idea of how Sprocket and Dart Sass work together. Thanks for sharing, guys!

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

6 participants