Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Swat123 opened this issue Mar 20, 2018 · 81 comments
Open

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

Swat123 opened this issue Mar 20, 2018 · 81 comments

Comments

@Swat123
Copy link

Swat123 commented Mar 20, 2018

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"
}
@rhys-vdw
Copy link
Collaborator

rhys-vdw commented Mar 20, 2018

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.

@Swat123
Copy link
Author

Swat123 commented Mar 21, 2018

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 ?

@rhys-vdw
Copy link
Collaborator

rhys-vdw commented Mar 21, 2018

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
Copy link
Collaborator

rhys-vdw commented Mar 21, 2018

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

@dreyks
Copy link

dreyks commented Apr 13, 2018

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

@rhys-vdw
Copy link
Collaborator

@dreyks oh dear!

@kofronpi
Copy link

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

@rhys-vdw
Copy link
Collaborator

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

@akaspick
Copy link

akaspick commented May 3, 2018

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
Copy link

akaspick commented May 3, 2018

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.

@rhys-vdw
Copy link
Collaborator

rhys-vdw commented May 8, 2018

@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?

@akaspick
Copy link

akaspick commented May 8, 2018

@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.

@rhys-vdw
Copy link
Collaborator

rhys-vdw commented May 8, 2018

@akaspick no problem, see #65.

@rhys-vdw
Copy link
Collaborator

rhys-vdw commented May 8, 2018

@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.

@n-rodriguez
Copy link

Setting NODE_ENV might save you : rails/webpacker#1374

@n-rodriguez
Copy link

n-rodriguez commented May 15, 2018

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 :)

@rhys-vdw
Copy link
Collaborator

rhys-vdw commented May 16, 2018

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

@n-rodriguez
Copy link

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

well it seems to be...

@n-rodriguez
Copy link

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.

@rhys-vdw
Copy link
Collaborator

rhys-vdw commented May 16, 2018

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 rhys-vdw changed the title randomly getting error while webpacker compile Error: rails-erb-loader failed with code: 1 May 16, 2018
@andybluey
Copy link

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

@n-rodriguez
Copy link

Setting NODE_ENV env var saved my day :)

@dmke
Copy link

dmke commented May 29, 2018

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...

@rhys-vdw
Copy link
Collaborator

@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.

@andybluey
Copy link

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

@tfwright
Copy link

tfwright commented Apr 4, 2020

In case it helps someone else having this issue, it was happening to me when trying to deploy to heroku using ../bin/rails runner for the runner option. Along the lines of the suggestion in #63 (comment) changing it to ../bin/bundle exec rails runner fixed the issue.

@Yardboy
Copy link

Yardboy commented Apr 27, 2020

Just chiming in with my solution, for anyone that comes looking in the future. I ran into this issue trying to deploy a Rails 6 application to Dokku using ERB javascript templates. Everything worked great locally under development and production env settings, but when pushed to Dokku it would fail at the assets:precompile step. None of the solutions I found here or elsewhere solved the problem.

Ultimately, I discovered that my binstubs were not executable upon deploy. #fml

This was the error I was getting.

remote: events.js:174
remote:       throw er; // Unhandled 'error' event
remote:       ^
remote:
remote: Error: spawn bin/rails EACCES
remote:     at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
remote:     at onErrorNT (internal/child_process.js:415:16)
remote:     at process._tickCallback (internal/process/next_tick.js:63:19)
remote: Emitted 'error' event at:
remote:     at Process.ChildProcess._handle.onexit (internal/child_process.js:246:12)
remote:     at onErrorNT (internal/child_process.js:415:16)
remote:     at process._tickCallback (internal/process/next_tick.js:63:19)
remote:
remote: The command '/bin/sh -c bundle exec rails assets:precompile' returned a non-zero code: 1

@amkisko
Copy link
Contributor

amkisko commented May 13, 2020

Maybe add console.log here: https://github.com/usabilityhub/rails-erb-loader/blob/master/index.js#L93

  child.stdout.on('data', function (data) {
    console.log(data);
    dataBuffers.push(data);
  })

Would be good to have options: { debug: true } that turns on verbose logging like printing dataBuffers.

#83

@mcmaddox
Copy link

FWIW, I resolved this issue by correcting a Zeitwork::NameError thrown while deploying to Heroku (Rails 6.0.3; Ruby 2.7.1p83; WSL [Ubuntu 18.04]) :

``on_file_autoloaded': expected file /tmp/build_10e9581d550a3c3f1134ca4159ab62d0/app/controllers/admins/[snake_case].rb to define constant Admins::[CamelCase], but didn't (Zeitwerk::NameError)`

My fix was simple: For each file that caused this error, I pluralized the module name (e.g., "Admins" instead of "Admin") to resolve the error.

I'm not sure if there is a conflict between Zeitwork and rails-erb-loader, but this resolved compilation errors for TWO failing js.erb files.

@MtnBiker
Copy link

MtnBiker commented Oct 10, 2020

Not sure another me too will help; but the error is in a new simple app in development mode. Here are all the gems including ones from development mode which I'm in. Using webpacker for JS and SCSS, but not for images.

A new small tutorial app. But I wanted <%= icon("icons8-marker-48.png") %> in maps.js.erb

ruby '2.7.0'
gem 'rails', '~> 6.0.3', '>= 6.0.3.2'
gem 'pg', '>= 0.18', '< 2.0'
gem 'puma', '~> 4.1'
gem 'jbuilder', '~> 2.7'
gem 'webpacker'
gem 'bootsnap', '>= 1.4.2', require: false
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'web-console', '>= 3.3.0'
gem 'listen', '~> 3.2'
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

And

yarn add @rails/webpacker
rails webpacker:install
yarn add rails-erb-loader
yarn add ol // openlayers which is what the tutorial is about

Error in localhost:

bootstrap:83 Uncaught Error: Module build failed (from /Users/gscar/node_modules/rails-erb-loader/index.js):
Error: rails-erb-loader failed with code: 1
    at ChildProcess.<anonymous> (:3000/Users/gscar/node_modules/rails-erb-loader/index.js:128)
    at ChildProcess.emit (:3000/events.js:314)
    at maybeClose (:3000/internal/child_process.js:1047)
    at Socket.<anonymous> (:3000/internal/child_process.js:438)
    at Socket.emit (:3000/events.js:314)
    at Pipe.<anonymous> (:3000/net.js:672)
    at Object../app/javascript/packs/maps.js.erb (bootstrap:83)
    at __webpack_require__ (bootstrap:19)
    at bootstrap:83
    at bootstrap:83
./app/javascript/packs/maps.js.erb @ bootstrap:83
__webpack_require__ @ bootstrap:19
(anonymous) @ bootstrap:83
(anonymous) @ bootstrap:83

Without the erb in the js script page loads fine.

@rhys-vdw
Copy link
Collaborator

@MtnBiker are you sure the actual error isn't being printed out further up the build output? In any case I'd strongly advise against using this tool in a new stack, rails-erb-loader was designed as a stopgap for if you have legacy ERB from a sprockets project that is now being built w/ webpack. There are better ways to get image paths into your JS that don't require templating. Read the guide.

@MtnBiker
Copy link

MtnBiker commented Oct 10, 2020

@rhys-vdw Not sure about your first sentence. The error I showed is the only error in Chrome Console.

But I'll take your advice. Many recommended against handling images with Webpacker, but I guess I'm pushing the limits, so will dive in. Part of it is the changing recommendations and to start with I don't have a webpack.config.js in my project. I realize Rails handles these things differently but it's more to wade through. And Rails guides hasn't caught up.

Thank you for answering.

EDIT for others as lacking in knowledge as I appears rails-erb-loader is added anyway, but .erb is not needed AFAIK.

@rhys-vdw
Copy link
Collaborator

rhys-vdw commented Oct 11, 2020

@MtnBiker

Not sure about your first sentence. The error I showed is the only error in Chrome Console.

When you run webpack normally it just spits output into the console, and any error from the rails-erb-loader process is piped out to STDERR before the exception is fired. I can't recall if this output it included in Chrome.

Many recommended against handling images with Webpacker

I can't comment on webpacker, it's a gem that relies on this module but I've never used it. It's very normal to require images with webpack itself though, just don't use this module for it. rails-erb-loader is very slow and error-prone, and needlessly heavy handed for that task. Furthermore ERB prevents your JS from being statically analysed by linters, prettier and other code tools.

Part of it is the changing recommendations and to start with I don't have a webpack.config.js in my project.

My guess is that webpacker would be set up to handle images via image-loader (it's a built-in package). Try require()ing an image path from your source folder and it should work. I've never had a project that doesn't require modification of the webpack config, might want to look into how to get access to it 🤷.

@ethonlyup
Copy link

ethonlyup commented Oct 13, 2020

I get this error message when attempting to compile a .js.erb file with webpacker.

`ERROR in ./app/javascript/packs/freelancer.js.erb Module build failed (from ./node_modules/rails-erb-loader/index.js): Error: rails-erb-loader failed with code: 1 
at ChildProcess.<anonymous> (/mnt/c/Code/helloworld/node_modules/rails-erb-loader/index.js:128:16) 
at ChildProcess.emit (events.js:198:13) 
at maybeClose (internal/child_process.js:982:16) 
at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5) (erb):12:in

': undefined method render' for main:Object (NoMethodError) from /home/ben/.rbenv/versions/2.6.5/lib/ruby/2.6.0/erb.rb:901:in eval'
from /home/ben/.rbenv/versions/2.6.5/lib/ruby/2.6.0/erb.rb:901:in result' from /mnt/c/Code/helloworld/node_modules/rails-erb-loader/erb_transformer.rb:20:in '`

My erb.js file looks like this.

`module.exports = { test: /\.erb$/, enforce: 'pre', exclude: /node_modules/, use: [{ loader: 'rails-erb-loader', options: { runner: (/^win/.test(process.platform) ? 'ruby ' : '') + 'bin/rails runner', env: { ...process.env, DISABLE_SPRING: 1, }, }, }], }`

I've tried a few solutions here, involving changing the path to 'ruby bin\ rails runner' since I'm on a windows machine, if I do that I get this error

'Error: spawn DISABLE_SPRING=1 ENOENT erb-rails-loader'.

I've tried adding

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

same ENOENT error.

I've tried adding the below snippet to erb.js, same exit code: 1

engine: { name: 'erubis', includes: [ 'ActionView::Helpers', 'Rails.application.routes.url_helpers' ] }

I've also tried adding the helper in my transformer_erb file like such
require 'erb' include Rails.application.routes.url_helpers ERB

To no avail, not sure where to go from here. For context, i'm on WSL 2 running ubuntu 16.

I've isolated my compiling error to these two lines of code.

$('#records_table').append("<%= j(render 'partials/gallery_items')%>"); $('#div_next_link').html("<%= j(render 'partials/next_link') %>");

@n-rodriguez
Copy link

n-rodriguez commented Oct 13, 2020

I'm commenting on #63 (comment) but it should be a starter for anyone who has issues with rails-erb :

I'm not sure if there is a conflict between Zeitwork and rails-erb-loader, but this resolved compilation errors for TWO failing js.erb files.

It's not a conflict but the thing is : rails-erb-loader calls the rails runner command (https://guides.rubyonrails.org/command_line.html#rails-runner) to generate JS from js.erb templates.

What it says:

runner runs Ruby code in the context of Rails non-interactively

So your app must be fully functionnal before calling rake assets:precompile in any environment.

To be sure: run your app locally but in production mode (rails -e production). If you have some issues here (Rails don't start because of Zeitwerk or any other issues like environment variables) rake assets:precompile will surely fail later.
It also means that your view.js.erb file must contain valid Ruby/Rails code or it will fail too.

Technically what it does is :

# something like that
cat myfile | bin/rails runner <path to erb_transformer.rb> > output.js

https://github.com/usabilityhub/rails-erb-loader/blob/master/index.js#L21
https://github.com/usabilityhub/rails-erb-loader/blob/master/index.js#L75
https://github.com/usabilityhub/rails-erb-loader/blob/master/erb_transformer.rb

You can even try it manually :)

@rhys-vdw
Copy link
Collaborator

rhys-vdw commented Oct 15, 2020

Thanks for detailed response @n-rodriguez. This is correct (but you also need to pass "engine" and "delimiter" arguments to the erb_transformer.)

@yosupgurl In this case though there are no real surprises here (for me, the author of this module). You're hitting issue #33. The runner doesn't add anything to scope. You'll see that error if you're trying to call a function render which is not available. I don't know how to do what you're doing, but there is some discussion in #33 you can refer to.

We always used render at runtime for serving requests, not for baking HTML into JS strings at build time. Are you sure that's even a good idea? Why not just inline the HTML straight into this file. Basically if you want to use webpack then you shouldn't be using render and partial, that's the point. Use raw-loader on a HTML file or something.

Also same general advice I gave above: This is not a good tool for web development, it's a transitional tool for moving away from server-side helpers and into SPA land using the superior webpack system. It's slow and bad and should really be used as minimally as possible (until someone volunteers some fixes).

@ethonlyup
Copy link

@rhys-vdw I'm using pagy to paginate a list of items. I'm initializing the variable that contains the paginated list before I call the j render function, this should allow for this functionality, no?

Sorry, I'm fairly new to web dev and I've been stuck on this for quite some time. None of the solutions allow me to properly render the paginated partial on click. Perhaps you could link me to a resource that would allow me to implement this feature?

@mattwd7
Copy link

mattwd7 commented Nov 18, 2020

@mcmaddox and @n-rodriguez saved me on this in the end. Despite Error: rails-erb-loader failed with code: 1 higher up in my logs, it was the Zeitwork error further down that was the root of the issue. In my case, I needed to rename/remove compiled directory names beginning with a number to make Zeitwork happy, which was pretty explicit in the error logging...!

ALSO, for anyone coming here on a ModuleNotFoundError on their rails-erb-loader, check your package.json!! Following the instructions in the rails-erb-loader Readme will have you adding the module in your devDependencies which is invisible to your production env :(

For a good time, add rails-erb-loader as a standard dependency, not a devDependency.

@rcerqueira-dv
Copy link

Hello everyone, any updates on this problem?

"rails-erb-loader": "^5.5.2"

my erb.js

/* put this in file like /config/webpack/loaders/erb.js */
/* global process:false */

module.exports = {
  test:     /\.erb$/,
  enforce:  "pre",
  exclude:  /node_modules/,

  use: [{
    loader:   "rails-erb-loader",
    options:  {
      timeoutMs: 20000,
      runner:     (/^win/.test(process.platform) ? "ruby " : "") + "bin/rails runner",
      env:        {
        ...process.env,
        DISABLE_SPRING: 1,
      },
    },
  }],
}

My output on precompile, compile webpacker

image

@rhys-vdw
Copy link
Collaborator

rhys-vdw commented Jun 21, 2021

I think I made this change to force Ruby to output the error, but it never got merged. You could try making that modification to your erb_transformer.rb file and see if it prints your error.

@cassioalmeidas
Copy link

I'm having the same problem on heroku with rails 6.1, it compiles with command RAILS_ENV=production assets:precompile on my machine, but when pushing to heroku the following error is displayed:

       ERROR in ./app/javascript/i18n-js/index.js.erb
        Module build failed (from ./node_modules/rails-erb-loader/index.js):
        Error: rails-erb-loader failed with code: 1
            at ChildProcess. (/tmp/build_4d3416a8/node_modules/rails-erb-loader/index.js:128:16)
            at ChildProcess.emit (events.js:375:28)
            at maybeClose (internal/child_process.js:1055:16)
            at Socket. (internal/child_process.js:441:11)
            at Socket.emit (events.js:375:28)
            at Pipe. (net.js:675:12)
         @ ./app/javascript/packs/application.js 5:392-431 5:443-447 5:448-452 

        ERROR in ./app/javascript/packs/hello_erb.js.erb
        Module build failed (from ./node_modules/rails-erb-loader/index.js):
        Error: rails-erb-loader failed with code: 1
            at ChildProcess. (/tmp/build_4d3416a8/node_modules/rails-erb-loader/index.js:128:16)
            at ChildProcess.emit (events.js:375:28)
            at maybeClose (internal/child_process.js:1055:16)
            at Socket. (internal/child_process.js:441:11)
            at Socket.emit (events.js:375:28)
            at Pipe. (net.js:675:12)

@rcerqueira-dv
Copy link

rcerqueira-dv commented Jun 29, 2021

In my case is while doing the docker image, could it be the same problem in heroku, because without the DISABLE_SPRING the problem is that ruby does not exists in the linux machine that im creating the docker, but thats the thing, it should not need ruby outside the docker to use ruby inside the docker

@cassioalmeidas
Copy link

@rcerqueira11AP I've already set DISABLE_SPRING=1 in heroku's environment variables, but it didn't do any good

@jwoodrow
Copy link

I had this issue with a PR that changed both the ruby version and rails version to ruby 3.0.1 and rails 6.1.3.2 that kept failing only on Travis.

After a lot of debugging I found that my error was FATAL: Listen error: unable to monitor directories for changes so after adding this to my travis.yml my assets:precompile started working again 🎉

before_script:
  - echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p

maybe this can help someone else having the same issues because this thread had me thinking it was a spring problem but in my case it was not since it's working without any DISABLE_SPRING=1

@0xradical
Copy link

0xradical commented Jul 21, 2021

After running webpack on a heroku bash session, I found out that there's something odd going on with the ARGV being passed to bin/rails...

/app/app/node_modules/rails-erb-loader/erb_transformer.rb:13:in `<top (required)>': Unknown templating engine `__RAILS_ERB_LOADER_DELIMETER__` (RuntimeError)

It seems the process is receiving the following ARGV, which is logically incorrect per erb_transformer.rb

["/app/node_modules/rails-erb-loader/erb_transformer.rb", "__RAILS_ERB_LOADER_DELIMETER__", "erb"]

I think this is due to the eager loading that happens on production, for some reason app/node_modules is an eager loaded path and when it tries to load the erb_transformer.rb file, it blows up ...

@cassioalmeidas
Copy link

@thiagobrandam see how i solved my problem jbox-web/ajax-datatables-rails#394

@lcampanari
Copy link

I ran into an issue that exemplifies the following statement given by @n-rodriguez, and wanted to highlight it.

So your app must be fully functionnal before calling rake assets:precompile in any environment.

On dev environment the erb-loader was working fine. The issue would appear only when deploying to Heroku review apps. The logs would show

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

for all .js.erb files, and further down there was also other database errors that seemed unrelated:

PG::UndefinedTable: ERROR:  relation "xxx" does not exist (ActiveRecord::StatementInvalid)

After hours of debugging, we found out that bin/rails runner was trying to load database dependencies when the db had no schema loaded yet. On Heroku review apps the assets:precompile is executed on the build phase, while db:schema:load is executed on post-deploy.

I couldn't find a way to overcome that by changing Heroku deploy configs. The solution was to remove this gem and use asset pipeline setup for this specific use case 😔. If anyone knows another solution for this please let me know. Thanks!

@rhys-vdw
Copy link
Collaborator

After hours of debugging, we found out that bin/rails runner was trying to load database dependencies when the db had no schema loaded yet.

So this module will just do whatever happens when you call rails runner. If that depends on your schema then it'll have to be there. It's an unfortunate design choice of ActiveRecord that it reads your schema at startup, compounded with the unfortunate design of Rails loading up every single object at startup.

However do you need to transpile your JS at this stage? Surely your backend doesn't rely on JS being compiled. Just run webpack as the last step after your database has been migrated.

The solution was to remove this gem and use asset pipeline setup for this specific use case 😔.

This is not a gem, it's an npm package. If your issue is with webpacker, as opposed to this package, please raise issues there. I am not in control of how it's integrated in other packages. rails-erb-loader predates webpacker.

Might I add that this was created so that we could stop using assets:precompile and develop our app as an SPA using webpack. rails-erb-loader should be treated as a necessary evil to migrate a codebase away from Rails asset pipeline, not as a permanent part of your stack. It's not particularly good, but it'll save you migrating your old erb files over in one go. There are better options available for templating your front end dependencies that do not require Rails at all. The simplest of these is simply storing your shared constants in a JSON file that is read by both json-loader and Rails itself at startup.

@sudoudaisuke
Copy link

sudoudaisuke commented Jun 2, 2022

I tryied same error.
I got this error message. I think that "runner_command.rb:46" can not find a file (or not exist). The erb_transformer.rb file path is under ".yarn/__virtual__/". Why rails-erb-loader can not find the file?

I created repro this Dockerfile.zip.

not found filename

/web/.yarn/__virtual__/rails-erb-loader-virtual-27b0bedac7/0/cache/rails-erb-loader-npm-5.5.2-8c1ad94c6e-f89ac4f14b.zip/node_modules/rails-erb-loader/erb_transformer.rb

error message

#yarn build
Please specify a valid ruby command or the path of a script to run.
Run 'rails runner -h' for help.

/web/vendor/bundle/ruby/3.1.0/gems/railties-7.0.3/lib/rails/commands/runner/runner_command.rb:46: syntax error, unexpected local variable or method, expecting end-of-input
...s-erb-loader-virtual-27b0bedac7/0/cache/rails-erb-loader-npm...
...                       ^~~~~~~~
assets by status 1.05 KiB [cached] 1 asset
runtime modules 663 bytes 3 modules
cacheable modules 63 bytes
  ./app/javascript/app.js 24 bytes [built] [code generated]
  ./app/javascript/myerb.js.erb 39 bytes [built] [code generated] [1 error]

ERROR in ./app/javascript/myerb.js.erb
Module build failed (from ./.yarn/__virtual__/rails-erb-loader-virtual-27b0bedac7/0/cache/rails-erb-loader-npm-5.5.2-8c1ad94c6e-f89ac4f14b.zip/node_modules/rails-erb-loader/index.js):
Error: rails-erb-loader failed with code: 1

webpack.config.js

  module: {
    rules: [
      {
        test: /\.erb$/,
        enforce: "pre",
        loader: "rails-erb-loader",
        options: {
          runner: "bundle exec bin/rails runner"
        }
      }
    ]
  },

environments

  • ruby: 3.1.2
  • webpack: 5.72.1
  • yarn: 3.2.1
  • rails: 7.0

@unRARed
Copy link

unRARed commented Aug 11, 2022

Per @rhys-vdw's comment

I ran into an issue that exemplifies the following statement given by @n-rodriguez, and wanted to highlight it.

So your app must be fully functionnal before calling rake assets:precompile in any environment.

On dev environment the erb-loader was working fine. The issue would appear only when deploying to Heroku review apps. The logs would show

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

for all .js.erb files, and further down there was also other database errors that seemed unrelated:

PG::UndefinedTable: ERROR:  relation "xxx" does not exist (ActiveRecord::StatementInvalid)

After hours of debugging, we found out that bin/rails runner was trying to load database dependencies when the db had no schema loaded yet. On Heroku review apps the assets:precompile is executed on the build phase, while db:schema:load is executed on post-deploy.

I couldn't find a way to overcome that by changing Heroku deploy configs. The solution was to remove this gem and use asset pipeline setup for this specific use case 😔. If anyone knows another solution for this please let me know. Thanks!

We ran into the same situation. For specifically this Heroku Review Apps + webpacker + rails-erb-loader issue, I think I solved by sort of monkey patching the Rake Task:

./lib/tasks/heroku_review_app_assets_precompile.rake

if ENV["RAILS_ENV"] == "review"
  Rake::Task["assets:precompile"].enhance(["db:schema:load"])
end

It does mean that rails db:schema:load happens twice, but seems like only a nit? Or maybe this is too naive?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests