Default config should not watch `log` directory (and probably others)
alexandergitter opened this issue · 16 comments
I noticed that on a new Rails app, Tailwind seems to be running quite frequently during HTTP requests, even if no files have changed. It appears that Tailwind watches the log files, where new log lines get appended frequently.
To reproduce this I'm doing the following:
- create a new Rails app via
rails new MyApp --css tailwind - run it with
bin/dev - change something in a log file, e.g.
echo "foo" >> log/development.log - observe that tailwind outputs something like
17:01:26 css.1 | Done in 3ms
It would be nice if the default generated configuration excludes the log directory and probably others like tmp and storage.
OS: Ubuntu 24.04
Ruby: 3.3.6
Rails: 8.0.1
tailwindcss-rails: 4.0.0
tailwindcss-ruby: 4.0.4
@alexandergitter Thanks for opening this issue, sorry you're experiencing that.
I can't reproduce what you're seeing, though! Tailwind v4 has some heuristics for what files it chooses to scan:
https://tailwindcss.com/blog/tailwindcss-v4#automatic-content-detection
Notably I'll quote from that section:
For example, we automatically ignore anything in your .gitignore file
By default Rails include /tmp/* and /log/* in the generated .gitignore, so I think that's why I can't reproduce this in my environment. Have you modified your ignores?
Any additional information you can provide me so that I can reproduce what you're seeing would be valuable
I think this is a configuration issue. v4 dropped the configuration file (tailwind.config.js) in favor of css directives. By default, tailwind will scan everything under the cwd. See detecting classes in source files.
Indeed, if I echo "foo" >> log/development.log it will re-build the css file.
But you can fix that by doing:
@import "tailwindcss" source("../../");
See registering sources.
this will only watch the app directory.
@patriciomacadden I think we posted at the same time. Please see my comment above about how the v4 cli chooses to ignore files, it does not scan everything.
🤔 Ok, I think I know what's going on here. When you echo something into "log/development.log" you see a line from the tailwindcss process Done in 624µs or similar. OK.
But the CSS files are not being regenerated when this happens. You can demonstrate this by deleting app/assets/build/tailwind.css and echoing into the log file. It does not get regenerated.
I think the "Done in Xms" message is saying that a file changed on disk, not that the CSS file has been regenerated.
In any case, this gem doesn't have control over what the upstream tailwindcss CLI executable emits, unfortunately. You may want to open an issue upstream?
Closing because I don't think there's a specific config change I can (or should) make here.
@patriciomacadden If you think changing the config to @import "tailwindcss" source("../../"); is the right thing to do, open a PR and we can talk about it?
I'm not at all sure that limiting tailwind to the app directory is going to necessarily be the right thing, though. Specifically I'm thinking about helper methods in lib, etc.
Ah, hmm, this looks like it's a bug in upstream in any case: tailwindlabs/tailwindcss#15684
I don't think it's necessary right now. It's not building the css and it's not spending a lot of time doing what it does. Even though we could add other directories using @source, it could make things harder debug, for example.
All good for me.
Thank you both, I'll keep an eye on the upstream issue.
I’d like to point out that while Tailwind v4 might not change the generated output and is rather quick in doing so, this behavior is still an issue overall because it causes a lot of CPU load. In my case, I use GoodJob with a bunch of scheduled (cron/periodic) tasks. As GoodJob logs to log/development.log and this is picked up by Tailwind, this causes said high CPU load. Having said, I agree that this is an upstream issue.