I am building a rails app and having some odd sass compilation issues.
My setup:
- Rails 6.1.3
- Ruby 3.0.0
- sass-rails 6.0.0
- sassc-rails 2.1.2
My application.scss basically loads a few sass files that set some variables, and at the end imports the layout.scss which effectively uses the imported variables.
// application.scss
@import "normalize";
@import "variables/colors";
@import "variables/typography";
@import "variables/sizes";
@import "layout";
// variables/_typography.scss
$ui-font-family: "Poppins", sans-serif;
$ui-font-family-title: "Lato", sans-serif;
// layout.scss
h1, h2, h3, h4, h5, h6 {
font-family: $ui-font-family-title;
}
// views/layouts/application.html.erb
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
At the beginning it worked, but then I noticed that sometimes I got an error saying
Error: Undefined variable: "$ui-font-family-title".
on line 3:16 of app/assets/stylesheets/layout.scss
>> font-family: $ui-font-family-title;
However, sometimes after recompiling assets (because of a change in any scss file) I noticed it could either work or fail (with that error).
I added a @debug "Compiling filename" to each sass file to understand the compilation order and found something very odd to me.
It always compiles every sass file two times, and the problem is that sometimes it compiles variables once and therefore it fails when compiling the layout.scss the second time.
This is how it looks when it works:
And this is how it looks when failing:
As you can see, when it fails it compiles the layout.scss file twice in a row, and my guess is that since on the second iteration it doesn't import the other files, it can't find the variables as expected.
Do you have any idea on what could be going wrong here?

