webpacker: Can't find packs when running Rspec

Ruby 2.3.7 Rails 4.2.10 Webpacker 4.0.0.pre.pre.2

File config/webpacker.yml:

# Note: You must restart bin/webpack-dev-server for changes to take effect

default: &default
  source_path: app/javascript
  source_entry_path: packs
  public_output_path: packs
  cache_path: tmp/cache/webpacker

  # Additional paths webpack should lookup modules
  # ['app/assets', 'engine/foo/app/assets']
  resolved_paths: []

  # Reload manifest.json on all requests so we reload latest compiled packs
  cache_manifest: false

  extensions:
    - .js
    - .sass
    - .scss
    - .css
    - .module.sass
    - .module.scss
    - .module.css
    - .png
    - .svg
    - .gif
    - .jpeg
    - .jpg
    - .hbs

development:
  <<: *default
  compile: true

  # Reference: https://webpack.js.org/configuration/dev-server/
  dev_server:
    https: false
    host: localhost
    port: 3035
    public: localhost:3035
    hmr: false
    # Inline should be set to true if using HMR
    inline: true
    overlay: true
    compress: true
    disable_host_check: true
    use_local_ip: false
    quiet: false
    headers:
      'Access-Control-Allow-Origin': '*'
    watch_options:
      ignored: /node_modules/

test:
  <<: *default
  compile: true

  # Compile test packs to a separate directory
  public_output_path: packs-test

staging:
  <<: *default

  # Production depends on precompilation of packs prior to booting for performance.
  compile: false

  # Cache manifest.json for performance
  cache_manifest: true

production:
  <<: *default

  # Production depends on precompilation of packs prior to booting for performance.
  compile: false

  # Cache manifest.json for performance
  cache_manifest: true

File config/webpack/test.js:

process.env.NODE_ENV = process.env.NODE_ENV || 'development'

const environment = require('./environment')

module.exports = environment.toWebpackConfig()

Portion of config/environments/test.rb:

config.serve_static_files = true

When I run:

bundle exec rspec spec/controllers/admin/links_controller_spec.rb

I see:

     Failure/Error: = javascript_pack_tag "webpack-runtime"

     ActionView::Template::Error:
       Webpacker can't find webpack-runtime.js in /Users/sharang/Data/work/gumroad/web/public/packs-test/manifest.json. Possible causes:
       1. You want to set webpacker.yml value of compile to true for your environment
          unless you are using the `webpack -w` or the webpack-dev-server.
       2. webpack has not yet re-run to reflect updates.
       3. You have misconfigured Webpacker's config/webpacker.yml file.
       4. Your webpack configuration is not creating a manifest.
       Your manifest contains:
       {
       }

The problem is that pubic/packs-test is not generated at all when the test is run.

When I run the test like this the problem goes away:

RAILS_ENV=test bundle exec rake webpacker:compile && bundle exec rspec spec/controllers/admin/links_controller_spec.rb

It seems like the auto-compilation is not happening in my case. How do I debug this? Thanks!

Note: I read through the closed issues where this problem was discussed but those didn’t help me.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 5
  • Comments: 15 (7 by maintainers)

Commits related to this issue

Most upvoted comments

I know this is closed, but my scenario was not listed here as far as I can see and it was pretty difficult to determine.

As with many others here, I was running RAILS_ENV=test rspec and it kept yelling at me because there was no packs-test directory. Without considering it for way too long, I finally adjusted my .env.test file and removed the RAILS_ENV= from it. Then it worked. As far as I knew command line env vars should override .env files, so this was perplexing to me.

Obviously, this was not expected, so I didn’t even consider it. However, there is a caveat when using the Dotenv gem, you can load .env files via overload vs load, which will override any existing environment variables set, example: `Dotenv.overload(“.env.#{Rails.env}”).

This resulted in the RAILS_ENV being set to an empty string (as it was set in the .env.test file) and webpacker was defaulting to the production environment. By removing RAILS_ENV= from my .env.test file it resolved the issue.

What really confused me was that when I ran RAILS_ENV=test bundle exec rails webpacker:compile the output was…

Compiling...
Compiled all packs in /Users/.../public/packs-test

So, it is still saying that it is outputting to packs-test, but it really isn’t. 😦

Hey! Just wanted to chime in. In some cases it can happen that you maybe ran rails webpacker:compile locally and some files ended up in public/packs/ that shouldn’t be there and might reference code that doesn’t exist anymore.

So what I can recommend if you are facing this issue to just clean out most temporary files, for example

$ rails tmp:clear
$ rm -rf public/assets public/packs public/packs-test

public/packs-test/ is ok to have around I think, but packs/ shouldn’t be there I believe.

I was having the same issue, just deleting the public/packs-test and rebuild solved the issue 👍

I’m closing it because we ended up doing this:

we manually compile our packs before running our specs.

https://github.com/rails/webpacker/issues/1695#issuecomment-424272859.

@sturdynut This helped me 6 months later. Thank you!

I encountered the same issue on docker environment. Turns out if you set NODE_ENV=development while running rspec, it will lazy compile, but when NODE_ENV=test it wont.

I wanted to share that I had been struggling with the exact same problem in an older project I migrated to Webpacker and discovered that my bin/spring file that was initially created by Rails was different than the one in a newer project (in which I don’t experience this problem). Updating that binstub fixed the exact same problem I was having (though I’m not quite sure why).

The more recent and workingbin/spring binstub looks likes:

#!/usr/bin/env ruby

# This file loads spring without using Bundler, in order to be fast.
# It gets overwritten when you run the `spring binstub` command.

unless defined?(Spring)
  require 'rubygems'
  require 'bundler'

  lockfile = Bundler::LockfileParser.new(Bundler.default_lockfile.read)
  spring = lockfile.specs.detect { |spec| spec.name == "spring" }
  if spring
    Gem.use_paths Gem.dir, Bundler.bundle_path.to_s, *Gem.path
    gem 'spring', spring.version
    require 'spring/binstub'
  end
end

…whereas the older one that caused Webpacker not to lazy compile test packs when running bin/rspec looks like this:

#!/usr/bin/env ruby

# This file loads spring without using Bundler, in order to be fast
# It gets overwritten when you run the `spring binstub` command

unless defined?(Spring)
  require "rubygems"
  require "bundler"

  if match = Bundler.default_lockfile.read.match(/^GEM$.*?^    spring \((.*?)\)$.*?^$/m)
    ENV["GEM_PATH"] = ([Bundler.bundle_path.to_s] + Gem.path).join(File::PATH_SEPARATOR)
    ENV["GEM_HOME"] = ""
    Gem.paths = ENV

    gem "spring", match[1]
    require "spring/binstub"
  end
end

Edit: here is my bundle exec rails webpacker:info:

Ruby: ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]
Rails: 5.2.1
Webpacker: 3.5.5
Node: v8.9.0
Yarn: 1.7.0

@rails/webpacker:
/Users/bensheldon/Repositories/codeforamerica/open311status
└── @rails/webpacker@3.5.5

Is bin/webpack present?: true
Is bin/webpack-dev-server present?: true
Is bin/yarn present?: false