rails-erb-loader icon indicating copy to clipboard operation
rails-erb-loader copied to clipboard

Error: rails-erb-loader failed with code: 1

Open Swat123 opened this issue 7 years ago • 81 comments
trafficstars

Running the following: ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /Users/swathishah/shopify-app/bin/spring rails --trace assets:precompile

But I randomly get following error:

ERROR in ./app/javascript/components/layout_tab.js.erb
Module build failed: Error: rails-erb-loader failed with code: 1
    at ChildProcess.<anonymous> (/Users/swathishah/shopify-app/node_modules/rails-erb-loader/index.js:108:16)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:191:7)
    at maybeClose (internal/child_process.js:886:16)
    at Socket.<anonymous> (internal/child_process.js:342:11)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at Pipe._handle.close [as _onclose] (net.js:501:12)
 @ ./app/javascript/components/tabs.js 12:0-44
 @ ./app/javascript/packs/notifications_react.jsx

Package.json has following:

{
  "name": "shopify-app",
  "private": true,
  "dependencies": {
    "@rails/webpacker": "^3.2.1",
....
   "rails-erb-loader": "^5.4.1",
...
    "yarn": "^1.3.2"
  },
  "devDependencies": {
    "webpack-dev-server": "^2.11.1"
}

Swat123 avatar Mar 20 '18 17:03 Swat123

Hi @Swat123. I don't know why this is happening exactly, but I can explain the general context of this error message and maybe we can improve the reporting or find out what's going wrong.

When rails-erb-loader loads a single file:

  • It runs a Ruby script in a new process. The script waits for data to be piped into it.
  • rails-erb-loader streams the input file to the process via stdin
  • The script transforms the source and prints it out.
  • rails-erb-loader reads all the stdout from the script (this will be the output)
  • When the process closes, if it ended without error (ie. returned code 0) then the loader succeeds and the transpiled source is given to webpack.

Typically with Rails you'd use bin/rails runner to execute the transformer script, and you can see that this is the approach that webpacker uses.

In short: bin/rails runner is exiting with an error code.

For @Swat123

I would have expected error output to be piped through to the main process, so you should see error logs on screen if they're printed. Is there any logging before you see that error? If no, is it possible that the wrapper task you're running is somehow suppressing stderr from webpack? I'm aware of webpacker's existence, but I haven't used it myself.

But I randomly get following error[...]

By "randomly" you mean that it only happens sometimes right? If this is the case it might be being caused by spring. You could try running the process with DISABLE_SPRING=1 and see if that stops it. It might make your build slower, but it would help explain the problem.

You can either add it to your loader config:

options: { runner: 'DISABLE_SPRING=1 bin/rails runner' }

Or you should be able to set it with your rails command:

DISABLE_SPRING=1 ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /Users/swathishah/shopify-app/bin/spring rails --trace assets:precompile

(uh... actually looking at the above you might need to remove the explicit call to spring)

Good luck!

For rails-erb-loader

Even if this issue turns out to be out of our control, I'd like to know how we can improve the error reporting from the loader. At very least we should update the error message to something like:

rails-erb-loader: "bin/rails runner" closed with exit code 1

It's also unclear to me why the 'close' callback is being called instead of the 'error' callback.

Additionally I'd like to double check and add test coverage to ensure that we are indeed piping stderr through to the console successfully.

rhys-vdw avatar Mar 20 '18 23:03 rhys-vdw

Thank you so much. You are right it is spring. Disabling spring gets me rid of the error. Any way I can debug and pull in more trace info so that it can help others ?

Swat123 avatar Mar 21 '18 03:03 Swat123

Hey @Swat123, thanks for getting back to me. Glad I could help.

Yes, absolutely. If you're willing to put in some work there are a few actionables here...

  1. Improve the error message to include the actual executable that returned the error code (see my previous comment)
  2. Confirm that spring is actually not outputting any error information on stderr. My understanding is that we're reporting all information to the user at the moment, and you're seeing nothing in this case. There's a chance that spring is actually printing a meaningful error and we're accidentally suppressing it. If this is the case we might need to change the way we handle stderr.
  3. Add a troubleshooting section to the README that suggests trying to disable spring for random failures/hangs. This should still suggest opening/commenting in issues so we can try to nail them down.
  4. Do some research to see if there are any relevant issues about spring failing silently with error code 1. Perhaps there is a known workaround or at least something we can link to in the README.

Any effort on any of these would be greatly appreciated, thanks. :)

rhys-vdw avatar Mar 21 '18 04:03 rhys-vdw

I wonder if we can learn something from the spring server command...

rhys-vdw avatar Mar 21 '18 04:03 rhys-vdw

options: { runner: 'DISABLE_SPRING=1 bin/rails runner' }

I tried this for other ENV var that i needed and this does not work. parseRunner splits by spaces so the ENV var becomes a file that spawn tries to call

dreyks avatar Apr 13 '18 11:04 dreyks

@dreyks oh dear!

rhys-vdw avatar Apr 15 '18 13:04 rhys-vdw

I'm also affected by this with undecipherable errors. I will investigate and report back.

kofronpi avatar Apr 16 '18 15:04 kofronpi

Thanks @kofronpi. It's hard for me to investigate because I am not experiencing this issue at the moment.

rhys-vdw avatar Apr 17 '18 06:04 rhys-vdw

I'm going mad trying to deploy to a production env with rails-erb-loader...

ERROR in ./app/javascript/packs/components/repeat_dialog.vue.erb
Module build failed: Error: rails-erb-loader failed with code: null
    at ChildProcess.<anonymous> (/home/deploy/app/releases/353/node_modules/rails-erb-loader/index.js:108:16)
    at ChildProcess.emit (events.js:182:13)
    at maybeClose (internal/child_process.js:947:16)
    at Socket.stream.socket.on (internal/child_process.js:368:11)
    at Socket.emit (events.js:182:13)
    at Pipe._handle.close [as _onclose] (net.js:598:12)
Child extract-text-webpack-plugin node_modules/extract-text-webpack-plugin/dist node_modules/css-loader/index.js??ref--1-2!node_modules/postcss-loader/lib/index.js??ref--1-3!node_modules/bootstrap-daterangepicker/daterangepicker.css:
       2 modules
Child extract-text-webpack-plugin node_modules/extract-text-webpack-plugin/dist node_modules/css-loader/index.js??ref--1-2!node_modules/postcss-loader/lib/index.js??ref--1-3!node_modules/fullcalendar/dist/fullcalendar.min.css:
       2 modules
Child extract-text-webpack-plugin node_modules/extract-text-webpack-plugin/dist node_modules/css-loader/index.js??ref--1-2!node_modules/postcss-loader/lib/index.js??ref--1-3!node_modules/fullcalendar-scheduler/dist/scheduler.min.css:
       2 modules
Child extract-text-webpack-plugin node_modules/extract-text-webpack-plugin/dist node_modules/css-loader/index.js??ref--1-2!node_modules/postcss-loader/lib/index.js??ref--1-3!node_modules/select2/dist/css/select2.min.css:
       2 modules
Child extract-text-webpack-plugin node_modules/extract-text-webpack-plugin/dist node_modules/css-loader/index.js?minimize!node_modules/vue-loader/lib/style-compiler/index.js?{"optionsId":"0","vue":true,"scoped":false,"sourceMap":false}!node_modules/sass-loader/lib/loader.js!node_modules/vue-loader/lib/selector.js?type=styles&index=0!app/javascript/packs/components/target_table.vue:
       2 modules
Child extract-text-webpack-plugin node_modules/extract-text-webpack-plugin/dist node_modules/css-loader/index.js?minimize!node_modules/vue-loader/lib/style-compiler/index.js?{"optionsId":"0","vue":true,"id":"data-v-3dc4e503","scoped":true,"sourceMap":false}!node_modules/sass-loader/lib/loader.js!node_modules/vue-loader/lib/selector.js?type=styles&index=0!app/javascript/packs/components/target_input.vue:
       2 modules
Child extract-text-webpack-plugin node_modules/extract-text-webpack-plugin/dist node_modules/css-loader/index.js?minimize!node_modules/vue-loader/lib/style-compiler/index.js?{"optionsId":"0","vue":true,"id":"data-v-25afaeb2","scoped":true,"sourceMap":false}!node_modules/sass-loader/lib/loader.js!node_modules/vue-loader/lib/selector.js?type=styles&index=1!app/javascript/packs/components/target_table.vue:
       2 modules

My package.json:

{
  "dependencies": {
    "@rails/webpacker": "3.5",
    "bootstrap-daterangepicker": "^2.1.25",
    "caniuse-lite": "^1.0.30000697",
    "coffeescript": "1.12.7",
    "css-loader": "^0.28.11",
    "expose-loader": "^0.7.3",
    "file-loader": "^0.11.2",
    "foundation-datepicker": "^1.5.6",
    "fullcalendar": "^3.4.0",
    "fullcalendar-scheduler": "^1.6.2",
    "jquery": "^3.2.1",
    "moment": "^2.18.1",
    "rails-erb-loader": "^5.4.1",
    "select2": "^4.0.3",
    "slm": "^1.0.0",
    "vue": "^2.5.16",
    "vue-loader": "14.2.2",
    "vue-template-compiler": "^2.5.16",
    "webpack": "^3.0.0"
  },
  "devDependencies": {
    "webpack-dev-server": "2.11.2"
  }
}

Any ideas? There is nothing to tell me what actually went wrong. This is occurring on my production Ubuntu 14.04 server.

akaspick avatar May 03 '18 02:05 akaspick

Follow up to my last message... now compilation works with no changes to the server. Stress on the randomly in this issues title. I'll keep testing to see if there's any pattern to this... server load, etc.

akaspick avatar May 03 '18 14:05 akaspick

@akaspick I was on holiday.

It seems our failure is here.

  child.on('close', function (code) {
    if (code === 0) {
      // ...
    } else if (child.killed) {
      // ...
    } else {
      callback(new Error('rails-erb-loader failed with code: ' + code))
    }
  })

Explanation here.

If the process exited, code is the final exit code of the process, otherwise null. If the process terminated due to receipt of a signal, signal is the string name of the signal, otherwise null. One of the two will always be non-null.

So should look like this:

  child.on('close', function (code, signal) { // <-- Add signal param here
    if (code === 0) {
      // ...
    } else if (child.killed) { // <-- I'm _assuming_ this is the same as `signal === 'SIGKILL'`
      // ...
    } else if (signal !== null) {  // <-- Add this handler
      callback(new Error('rails-erb-loader terminated by signal: ' + signal))
    } else {
      callback(new Error('rails-erb-loader failed with code: ' + code))
    }
  })

@akaspick you should get more information if we implement these changes. I'm a little behind having just returned from a break. Are you up to making this change?

rhys-vdw avatar May 08 '18 01:05 rhys-vdw

@rhys-vdw I'd love to make the change, but I don't have a reliable way to reproduce the issue. I had non-stop issues one evening last week and then the following day and after I haven't seen the issue since. If this comes up again, I'll take another look. Thanks for the reply.

akaspick avatar May 08 '18 02:05 akaspick

@akaspick no problem, see #65.

rhys-vdw avatar May 08 '18 05:05 rhys-vdw

@akaspick if you update to latest (5.4.2) you should get the name of the signal that killed your process in future. Hopefully you can check your production environment docs to find out what might be causing it.

rhys-vdw avatar May 08 '18 06:05 rhys-vdw

Setting NODE_ENV might save you : https://github.com/rails/webpacker/issues/1374

n-rodriguez avatar May 15 '18 08:05 n-rodriguez

I had the same error with erb-loader with bundle exec rake assets:precompile:

ERROR in ./app/javascript/vendor/javascripts/smart_listing/smart_listing.coffee.erb
Module build failed: Error: rails-erb-loader failed with code: 1
    at ChildProcess.<anonymous> (/home/nicolas/PROJECTS/CONCERTO/concerto/node_modules/rails-erb-loader/index.js:120:16)
    at ChildProcess.emit (events.js:182:13)
    at maybeClose (internal/child_process.js:957:16)
    at Socket.stream.socket.on (internal/child_process.js:378:11)
    at Socket.emit (events.js:182:13)
    at Pipe._handle.close [as _onclose] (net.js:598:12)

But it was weird because :

$ cat app/javascript/vendor/javascripts/smart_listing/smart_listing.coffee.erb | bin/rails runner ./node_modules/rails-erb-loader/erb_transformer.rb __RAILS_ERB_LOADER_DELIMETER__ erb
$ echo $?
0

Setting NODE_ENV saved my day :)

n-rodriguez avatar May 15 '18 08:05 n-rodriguez

@n-rodriguez so this is an issue with webpacker, not not rails-erb-loader?

rhys-vdw avatar May 16 '18 01:05 rhys-vdw

@n-rodriguez so this is an issue with webpacker, not not rails-erb-loader?

well it seems to be...

n-rodriguez avatar May 16 '18 01:05 n-rodriguez

I don't know how or why but when rails/runner is called by rails-erb-loader the exit code is not 0. Running it by hand returns 0.

n-rodriguez avatar May 16 '18 01:05 n-rodriguez

Thanks for the info. We don't use webpacker, but if there's anything we can do to prevent/report this error in the actual loader I'd appreciate the help.

My understanding is that any stderr should be piped through to console when webpack is running, so if Rails is outputting errors via stdout they should be visible. That said this behaviour might be broken.

rhys-vdw avatar May 16 '18 01:05 rhys-vdw

@n-rodriguez - can you please let me know the change you made? Thanks

andybluey avatar May 29 '18 05:05 andybluey

Setting NODE_ENV env var saved my day :)

n-rodriguez avatar May 29 '18 11:05 n-rodriguez

I've come across this error as well. In my case, the Rails Runner script was invoked with the global Ruby installation, not my local rbenv version. bin/rails has the default shebang (#!/usr/bin/env ruby), which works in my default shell, but obviously not with spawn?

The error message I got was

12% building modules 22/25 modules 3 active $RAILS_ROOT/app/javascript/i18n.js.erb
Ignoring bcrypt-3.1.12 because its extensions are not built.  Try: gem pristine bcrypt --version 3.1.12
Ignoring bcrypt_pbkdf-1.0.0 because its extensions are not built.  Try: gem pristine bcrypt_pbkdf --version 1.0.0
Ignoring binding_of_caller-0.8.0 because its extensions are not built.  Try: gem pristine binding_of_caller --version 0.8.0
 $RBENV_GEMSET_DIR/gems/bundler-1.16.2/lib/bundler/spec_set.rb:91:in `block in materialize': Could not find nokogiri-1.8.2 in any of the sources (Bundler::GemNotFound)
	from $RBENV_GEMSET_DIR/gems/bundler-1.16.2/lib/bundler/spec_set.rb:85:in `map!'
	from $RBENV_GEMSET_DIR/gems/bundler-1.16.2/lib/bundler/spec_set.rb:85:in `materialize'
	from $RBENV_GEMSET_DIR/gems/bundler-1.16.2/lib/bundler/definition.rb:171:in `specs'
	from $RBENV_GEMSET_DIR/gems/bundler-1.16.2/lib/bundler/definition.rb:238:in `specs_for'
	from $RBENV_GEMSET_DIR/gems/bundler-1.16.2/lib/bundler/definition.rb:227:in `requested_specs'
	from $RBENV_GEMSET_DIR/gems/bundler-1.16.2/lib/bundler/runtime.rb:108:in `block in definition_method'
	from $RBENV_GEMSET_DIR/gems/bundler-1.16.2/lib/bundler/runtime.rb:20:in `setup'
	from $RBENV_GEMSET_DIR/gems/bundler-1.16.2/lib/bundler.rb:107:in `setup'
	from $RBENV_GEMSET_DIR/gems/bundler-1.16.2/lib/bundler/setup.rb:20:in `<top (required)>'
	from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:127:in `require'
	from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:127:in `rescue in require'
	from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:40:in `require'
	from $RAILS_ROOT/config/boot.rb:3:in `<top (required)>'
	from bin/rails:3:in `require_relative'
	from bin/rails:3:in `<main>'

followed by

ERROR in ./app/javascript/i18n.js.erb
Module build failed: Error: rails-erb-loader failed with code: 1
    at ChildProcess.<anonymous> ($RAILS_ROOT/node_modules/rails-erb-loader/index.js:120:16)
    at emitTwo (events.js:126:13)
    at ChildProcess.emit (events.js:214:7)
    at maybeClose (internal/child_process.js:925:16)
    at Socket.stream.socket.on (internal/child_process.js:346:11)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at Pipe._handle.close [as _onclose] (net.js:557:12)
 @ ./app/javascript/packs/frontend.js 119:0-37
 @ multi (webpack)-dev-server/client?http://0.0.0.0:8080 babel-polyfill ./app/javascript/packs/frontend.js

I'm still running Webpacker 2.0, so I could just modify my config/webpacker/loaders/erb.js:

   options: {
-    runner: 'bin/rails runner',
+    runner: 'bundle exec bin/rails runner',
   },

I don't know, however, why this became a problem today. When working on the very same project on the same machine last week, the webpack was built without hickups...

dmke avatar May 29 '18 11:05 dmke

@dmke FYI current version of rails-erb-loader has a bug that prevents setting env vars that way. See #64. You'll need to set them in the parent environment.

rhys-vdw avatar May 30 '18 00:05 rhys-vdw

Hey @n-rodriguez - sorry, just to confirm, where and how did you add NODE_ENV env var? Thnks

andybluey avatar May 30 '18 00:05 andybluey

@andybluey I use Dotenv gem

n-rodriguez avatar May 30 '18 04:05 n-rodriguez

@rhys-vdw: Is $PATH affected as well?

In my case $PATH contains $HOME/.rbenv/shims before /usr/bin, so that /usr/bin/env ruby (the shebang of bin/rails) picks up $HOME/.rbenv/shims/ruby, which in turn reads my local rbenv config (Ruby version + gemset name), and finally resolves ruby to $HOME/.rbenv/versions/ruby-2.3/bin/ruby.

When calling Node's spawn function in transformSource(), the $PATH seems not to be passed (docs say, options.env defaults to process.env). I'm not sure if I'm reading something wrong, but I don't see why the $PATH of bin/webpack-dev-server should not be the same when invoking bin/rails in the rails-erb-loader (I'm missing a huge chunk of code named webpack between executing those commands though :-))

dmke avatar May 30 '18 08:05 dmke

@dmke I wouldn't expect $PATH to change for reasons you've listed. How are you invoking webpack though? Using webpacker? Maybe it's doing something funky.

rhys-vdw avatar May 31 '18 21:05 rhys-vdw

@andybluey

FYI you can always do this:

$ NODE_ENV=development npm run dev

Where dev is your dev server "script".

rhys-vdw avatar May 31 '18 22:05 rhys-vdw

I'm also facing this issue when deploying to Heroku.

remote:        Running: rake assets:precompile
remote:        yarn install v1.5.1
remote:        [1/4] Resolving packages...
remote:        [2/4] Fetching packages...
remote:        info [email protected]: The platform "linux" is incompatible with this module.
remote:        info "[email protected]" is an optional dependency and failed compatibility check. Excluding it from installation.
remote:        [3/4] Linking dependencies...
remote:        warning "@rails/webpacker > [email protected]" has unmet peer dependency "caniuse-lite@^1.0.30000697".
remote:        warning "@rails/webpacker > [email protected]" has unmet peer dependency "webpack-sources@^1.0".
remote:        warning " > [email protected]" has unmet peer dependency "webpack@^2.0.0 || >= 3.0.0-rc.0 || ^3.0.0".
remote:        warning " > [email protected]" has unmet peer dependency "webpack@^4.0.0-beta.1".
remote:        warning "webpack-dev-server > [email protected]" has unmet peer dependency "webpack@^4.0.0".
remote:        [4/4] Building fresh packages...
remote:        Done in 30.74s.
remote:        Webpacker is installed 🎉 🍰
remote:        Using /tmp/build_2fd1132ac534cf5d82a21025657aba3d/config/webpacker.yml file for setting up webpack paths
remote:        Compiling…
remote:        Compilation failed:
remote:        Hash: 5155e8b55fcc41263913
remote:        Version: webpack 4.10.2
remote:        Time: 3632ms
remote:        Built at: 2018-06-05 17:25:34
remote:         6 assets
remote:        [0] ./app/javascript/packs/hello_react.jsx.erb 539 bytes {0} [built] [failed] [1 error]
remote:        [2] ./app/javascript/stylesheets/application.scss 39 bytes {1} [built]
remote:        [3] ./app/javascript/packs/application.js 558 bytes {1} [built]
remote:            + 1 hidden module
remote:        
remote:        ERROR in ./app/javascript/packs/hello_react.jsx.erb
remote:        Module build failed: Error: rails-erb-loader failed with code: 1
remote:            at ChildProcess.<anonymous> (/tmp/build_2fd1132ac534cf5d82a21025657aba3d/node_modules/rails-erb-loader/index.js:120:16)
remote:            at emitTwo (events.js:126:13)
remote:            at ChildProcess.emit (events.js:214:7)
remote:            at maybeClose (internal/child_process.js:925:16)
remote:            at Socket.stream.socket.on (internal/child_process.js:346:11)
remote:            at emitOne (events.js:116:13)
remote:            at Socket.emit (events.js:211:7)
remote:            at Pipe._handle.close [as _onclose] (net.js:567:12)

I'm using rails-webpacker (with webpacker 4.10.2). I know it's not supported yet by erb-loader, but I've been using it without problems in development. The strangest part is that when I run this command it finishes without problem:

RAILS_ENV=production NODE_ENV=production ./bin/webpack --progress --config config/webpack/production.js

I also run NODE_ENV=production RAILS_ENV=production bundle exec rails assets:precompile with no errors.

I tried setting DISABLE_SPRING=1 in heroku but it's still not working.

I created a project that can reproduce this error: https://github.com/luislezcair/erb-loader-test when pushing to Heroku.

luislezcair avatar Jun 05 '18 17:06 luislezcair