Can't deploy Rails 7: Error: Can't find stylesheet to import.

I’m trying to deploy a Rails 7 app which is based on another one that I already got successfully up and running on fly.io.

The issue I encounter again and again is the following message after calling fly deploy:

#0 15.01 Error: Can't find stylesheet to import.
#0 15.01   ╷
#0 15.01 1 │ @import "bootstrap/scss/bootstrap";
#0 15.01   │         ^^^^^^^^^^^^^^^^^^^^^^^^^^
#0 15.01   ╵
#0 15.01   app/assets/stylesheets/application.scss 1:9  root stylesheet
#0 15.03 rails aborted!
#0 15.03 cssbundling-rails: Command build failed, ensure `yarn build:css` runs without errors
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/cssbundling-rails-1.3.2/lib/tasks/cssbundling/build.rake:14:in `block (2 levels) in <main>'
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `block in execute'
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `each'
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `execute'
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/sentry-ruby-5.11.0/lib/sentry/rake.rb:24:in `execute'
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:219:in `block in invoke_with_call_chain'
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `synchronize'
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `invoke_with_call_chain'
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:243:in `block in invoke_prerequisites'
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:241:in `each'
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:241:in `invoke_prerequisites'
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:218:in `block in invoke_with_call_chain'
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `synchronize'
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `invoke_with_call_chain'
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:188:in `invoke'
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/application.rb:160:in `invoke_task'
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `block (2 levels) in top_level'
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `each'
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `block in top_level'
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/application.rb:125:in `run_with_threads'
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/application.rb:110:in `top_level'
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/railties-7.0.8/lib/rails/commands/rake/rake_command.rb:24:in `block (2 levels) in perform'
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/application.rb:186:in `standard_exception_handling'
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/railties-7.0.8/lib/rails/commands/rake/rake_command.rb:24:in `block in perform'
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/rake_module.rb:59:in `with_application'
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/railties-7.0.8/lib/rails/commands/rake/rake_command.rb:18:in `perform'
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/railties-7.0.8/lib/rails/command.rb:51:in `invoke'
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/railties-7.0.8/lib/rails/commands.rb:18:in `<main>'
#0 15.03 /rails/vendor/bundle/ruby/3.2.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
#0 15.03 Tasks: TOP => assets:precompile => css:build
#0 15.03 (See full trace by running task with --trace)
------
Error: failed to fetch an image or build from source: error building: failed to solve: executor failed running [/bin/sh -c SECRET_KEY_BASE=DUMMY ./bin/rails assets:precompile]: exit code: 1

Running yarn build:css locally works without any problems, the same is true for rails assets:precompile.

application.scss starts like shown below, “colors” and “fonts” are files in the same directory and don’t create any issues. It’s exactly the same as with my other application that already works and didn’t throw the “Can’t find stylesheet to import.” error.

@import "bootstrap/scss/bootstrap";
@import "bootstrap-icons/font/bootstrap-icons";
@import "colors";
@import "fonts";

... no more imports, just some CSS

Changing the bootstrap imports to something like @import "../../../node_modules/bootstrap-icons/font/bootstrap-icons"; didn’t solve the issue.

Here is the entire package.json file:

{
  "name": "app",
  "dependencies": {
    "@hotwired/turbo-rails": "^7.3.0",
    "@popperjs/core": "^2.11.8",
    "bootstrap": "^5.3.2",
    "bootstrap-icons": "^1.11.1",
    "list.js": "^2.3.1",
    "normalize.css": "^8.0.1",
    "pinia": "^2.1",
    "sass": "^1.67.0",
    "vue": "^3.3"
  },
  "devDependencies": {
    "@babel/core": "^7.22.20",
    "babel-loader": "^9.1.3",
    "css-loader": "^6.8.1",
    "sass-loader": "^13.3.2",
    "vue-loader": "^17.2.2",
    "vue-style-loader": "^4.1.3",
    "webpack": "^5.88.2",
    "webpack-cli": "^5.1.4"
  },
  "scripts": {
    "build": "webpack --config webpack.config.js",
    "build:css": "sass --no-source-map --load-path=node_modules ./app/assets/stylesheets/application.scss:./app/assets/builds/application.css"
  },
  "packageManager": "yarn@3.6.3"
}

And here is the Docker file:

# syntax = docker/dockerfile:1

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.2.1
FROM ruby:$RUBY_VERSION-slim as base

LABEL fly_launch_runtime="rails"

# Rails app lives here
WORKDIR /rails

# Set production environment
ENV RAILS_ENV="production" \
    BUNDLE_WITHOUT="development:test" \
    BUNDLE_DEPLOYMENT="1"

# Update gems and bundler
RUN gem update --system --no-document && \
    gem install -N bundler


# Throw-away build stage to reduce size of final image
FROM base as build

# Install packages needed to build gems and node modules
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y build-essential curl libpq-dev libvips node-gyp pkg-config python-is-python3

# Install JavaScript dependencies
ARG NODE_VERSION=16.16.0
ARG YARN_VERSION=3.6.3
ENV PATH=/usr/local/node/bin:$PATH
RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \
    /tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \
    corepack enable && \
    corepack prepare yarn@$YARN_VERSION --activate && \
    rm -rf /tmp/node-build-master

# Install application gems
COPY --link Gemfile Gemfile.lock ./
RUN bundle install && \
    bundle exec bootsnap precompile --gemfile && \
    rm -rf ~/.bundle/ $BUNDLE_PATH/ruby/*/cache $BUNDLE_PATH/ruby/*/bundler/gems/*/.git

# Install node modules
COPY --link package.json yarn.lock ./
RUN yarn install --frozen-lockfile

# Copy application code
COPY --link . .

# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile app/ lib/

# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
RUN SECRET_KEY_BASE=DUMMY ./bin/rails assets:precompile


# Final stage for app image
FROM base

# Install packages needed for deployment
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y curl imagemagick libvips postgresql-client && \
    rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Copy built artifacts: gems, application
COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
ARG UID=1000 \
    GID=1000
RUN groupadd -f -g $GID rails && \
    useradd -u $UID -g $GID rails --create-home --shell /bin/bash && \
    chown -R rails:rails db log storage tmp
USER rails:rails

# Deployment options
ENV RAILS_LOG_TO_STDOUT="1" \
    RAILS_SERVE_STATIC_FILES="true"

# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["./bin/rails", "server"]

Thanks for your help!

Is this still true if you set 'RAILS_ENV=production`?

I’ll note that if I create a new rails 7.0 project, I get:

  "scripts": {
    "build": "webpack --config webpack.config.js",
    "build:css:compile": "sass ./app/assets/stylesheets/application.bootstrap.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules",
    "build:css:prefix": "postcss ./app/assets/builds/application.css --use=autoprefixer --output=./app/assets/builds/application.css",
    "build:css": "yarn build:css:compile && yarn build:css:prefix"
  },

Thanks for the quick reply.

Both, RAILS_ENV=production rails assets:precompile and yarn build:css worked with the old package.json version and still do with new one. I changed it to:

{
  "name": "app",
  "dependencies": {
    "@hotwired/turbo-rails": "^7.3.0",
    "@popperjs/core": "^2.11.8",
    "autoprefixer": "^10.4.15",
    "bootstrap": "^5.3.2",
    "bootstrap-icons": "^1.11.1",
    "list.js": "^2.3.1",
    "normalize.css": "^8.0.1",
    "pinia": "^2.1",
    "postcss": "^8.4.29",
    "postcss-cli": "^10.1.0",
    "sass": "^1.67.0",
    "vue": "^3.3"
  },
  "devDependencies": {
    "@babel/core": "^7.22.20",
    "babel-loader": "^9.1.3",
    "css-loader": "^6.8.1",
    "sass-loader": "^13.3.2",
    "vue-loader": "^17.2.2",
    "vue-style-loader": "^4.1.3",
    "webpack": "^5.88.2",
    "webpack-cli": "^5.1.4"
  },
  "scripts": {
    "build": "webpack --config webpack.config.js",
    "build:css:compile": "sass ./app/assets/stylesheets/application.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules",
    "build:css:prefix": "postcss ./app/assets/builds/application.css --use=autoprefixer --output=./app/assets/builds/application.css",
    "build:css": "yarn build:css:compile && yarn build:css:prefix"
  },
  "browserslist": [
    "defaults"
  ],
  "packageManager": "yarn@3.6.3"
}

(I looked it up in my git repo, the first version the package.json was created with was Rails 7.0.4, my other project which successfully runs on fly.io is currently based on 7.0.6 and this project here is on 7.0.8.)

The error didn’t change:

#0 16.57 Error: Can't find stylesheet to import.
#0 16.57   ╷
#0 16.57 2 │ @import "bootstrap/scss/bootstrap";
#0 16.57   │         ^^^^^^^^^^^^^^^^^^^^^^^^^^
#0 16.57   ╵
#0 16.57   app/assets/stylesheets/application.scss 2:9  root stylesheet
#0 16.62 rails aborted!
#0 16.62 cssbundling-rails: Command build failed, ensure `yarn build:css` runs without errors
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/cssbundling-rails-1.3.2/lib/tasks/cssbundling/build.rake:14:in `block (2 levels) in <main>'
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `block in execute'
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `each'
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `execute'
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/sentry-ruby-5.11.0/lib/sentry/rake.rb:24:in `execute'
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:219:in `block in invoke_with_call_chain'
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `synchronize'
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `invoke_with_call_chain'
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:243:in `block in invoke_prerequisites'
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:241:in `each'
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:241:in `invoke_prerequisites'
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:218:in `block in invoke_with_call_chain'
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `synchronize'
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `invoke_with_call_chain'
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:188:in `invoke'
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/application.rb:160:in `invoke_task'
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `block (2 levels) in top_level'
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `each'
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `block in top_level'
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/application.rb:125:in `run_with_threads'
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/application.rb:110:in `top_level'
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/railties-7.0.8/lib/rails/commands/rake/rake_command.rb:24:in `block (2 levels) in perform'
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/application.rb:186:in `standard_exception_handling'
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/railties-7.0.8/lib/rails/commands/rake/rake_command.rb:24:in `block in perform'
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/rake_module.rb:59:in `with_application'
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/railties-7.0.8/lib/rails/commands/rake/rake_command.rb:18:in `perform'
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/railties-7.0.8/lib/rails/command.rb:51:in `invoke'
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/railties-7.0.8/lib/rails/commands.rb:18:in `<main>'
#0 16.62 /rails/vendor/bundle/ruby/3.2.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
#0 16.62 Tasks: TOP => assets:precompile => css:build
#0 16.62 (See full trace by running task with --trace)
------
Error: failed to fetch an image or build from source: error building: failed to solve: executor failed running [/bin/sh -c SECRET_KEY_BASE=DUMMY ./bin/rails assets:precompile]: exit code: 1

I get the same error when I try to run Docker locally via https://fly.io/docs/rails/getting-started/dockerfiles/#testing-locally.

Unfortunately, it didn’t help, and I don’t have much to offer. Recapping, you have:

  • An existing application that works
  • A second application that works locally, even with RAILS_ENV=production, but not with Docker or fly
  • Your Dockerfile looks fine
  • Your package.json looks fine

This application shows signs of having been created using a previous version of Rails and upgraded; which is shouldn’t be an issue.

The only difference I spot is that with a new project the application scss file is named app/assets/stylesheets/application.bootstrap.scss. But renaming it to application.scss and modifying the build:css:compile step works.

The symptoms imply that you have something installed globally that you need to build your project that is not listed in your package.json; but again I don’t spot anything missing.

At this point, all I can suggest is that you create a new Rails 7.0 project with bootstrap and webpack; verify that you can deploy it, and then incrementally copy your existing project into and over the new project until either something breaks or inexplicably it works.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.