From 5a9ebfa26d8f8c1fe144e7df2f66b24438fc21f6 Mon Sep 17 00:00:00 2001 From: Carl Brasic <677224+brasic@users.noreply.github.com> Date: Thu, 14 Dec 2017 14:18:37 -0600 Subject: [PATCH 1/2] Raise ENOENT like vanilla ruby Running this task when rails is loaded doesn't behave as expected because ActiveSupport monkeypatches Kernel#` as follows: https://github.com/rails/rails/blob/7d75599c875b081f63fc292e454b25e8e42eb60e/activesupport/lib/active_support/core_ext/kernel/agnostics.rb to suppress raising Errno::ENOENT. With this monkeypatch in place the code never reaches the fallback branch for legacy node versions introduced in https://github.com/rails/webpacker/pull/798 We can fix this by manually raising the exception that the activesupport monkeypatch has suppressed. --- lib/tasks/webpacker/check_node.rake | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/tasks/webpacker/check_node.rake b/lib/tasks/webpacker/check_node.rake index 1caa44103..f65e0f8bb 100644 --- a/lib/tasks/webpacker/check_node.rake +++ b/lib/tasks/webpacker/check_node.rake @@ -4,6 +4,7 @@ namespace :webpacker do begin begin node_version = `node -v` + raise Errno::ENOENT if node_version.blank? rescue Errno::ENOENT node_version = `nodejs -v` raise Errno::ENOENT if node_version.blank? From e49d10a0cac36fdb3206802206328418e4fb7f13 Mon Sep 17 00:00:00 2001 From: Carl Brasic <677224+brasic@users.noreply.github.com> Date: Thu, 14 Dec 2017 20:19:44 -0600 Subject: [PATCH 2/2] Simplify nested rescues Using shell syntax to choose either node or nodejs has the extra benefit of never raising Errno::ENOENT whether or not Kernel#` has been monkeypatched by ActiveSupport. --- lib/tasks/webpacker/check_node.rake | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/tasks/webpacker/check_node.rake b/lib/tasks/webpacker/check_node.rake index f65e0f8bb..63cdd03d4 100644 --- a/lib/tasks/webpacker/check_node.rake +++ b/lib/tasks/webpacker/check_node.rake @@ -2,13 +2,8 @@ namespace :webpacker do desc "Verifies if Node.js is installed" task :check_node do begin - begin - node_version = `node -v` - raise Errno::ENOENT if node_version.blank? - rescue Errno::ENOENT - node_version = `nodejs -v` - raise Errno::ENOENT if node_version.blank? - end + node_version = `node -v || nodejs -v` + raise Errno::ENOENT if node_version.blank? pkg_path = Pathname.new("#{__dir__}/../../../package.json").realpath node_requirement = JSON.parse(pkg_path.read)["engines"]["node"]