From f4265aebc8ee2a8f038d10430e12e569ce848e23 Mon Sep 17 00:00:00 2001 From: Markus Doits Date: Thu, 10 Dec 2020 18:13:02 +0100 Subject: [PATCH 1/3] put yarn binary in quotes to allow spaces in path to it --- railties/lib/rails/tasks/yarn.rake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/railties/lib/rails/tasks/yarn.rake b/railties/lib/rails/tasks/yarn.rake index 975a28d988..ddb1a41582 100644 --- a/railties/lib/rails/tasks/yarn.rake +++ b/railties/lib/rails/tasks/yarn.rake @@ -10,13 +10,13 @@ namespace :yarn do end yarn_flags = - if `#{Rails.root}/bin/yarn --version`.start_with?("1") + if `"#{Rails.root}/bin/yarn" --version`.start_with?("1") "--no-progress --frozen-lockfile" else "--immutable" end - system({ "NODE_ENV" => node_env }, "#{Rails.root}/bin/yarn install #{yarn_flags}") + system({ "NODE_ENV" => node_env }, "\"#{Rails.root}/bin/yarn\" install #{yarn_flags}") end end From 01801281ca302de4480c28077b88c00a9ad11f8d Mon Sep 17 00:00:00 2001 From: Markus Doits Date: Fri, 11 Dec 2020 11:14:11 +0100 Subject: [PATCH 2/3] Add an error message in case `bin/yarn` is missing One reason why it could be missing because Rails was upgraded but `rails app:update` was not run. Running `rails app:update:bin` should create it. refs #40795 --- railties/lib/rails/tasks/yarn.rake | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/railties/lib/rails/tasks/yarn.rake b/railties/lib/rails/tasks/yarn.rake index ddb1a41582..527614237e 100644 --- a/railties/lib/rails/tasks/yarn.rake +++ b/railties/lib/rails/tasks/yarn.rake @@ -17,6 +17,10 @@ namespace :yarn do end system({ "NODE_ENV" => node_env }, "\"#{Rails.root}/bin/yarn\" install #{yarn_flags}") + rescue Errno::ENOENT + $stderr.puts "bin/yarn was not found." + $stderr.puts "Please run `bundle exec rails app:update:bin` to create it." + exit 1 end end From 87e9ae053d661daa3b8549e1cc9ea5ecd3b8ad62 Mon Sep 17 00:00:00 2001 From: Markus Doits Date: Fri, 11 Dec 2020 15:44:06 +0100 Subject: [PATCH 3/3] Only run `yarn:install` before `assets:precompile` if `bin/yarn` present Based on discussion in https://github.com/rails/rails/issues/40795, it looks like `yarn:install` is *always* run, even if the Rails project disabled javascript and there is no `bin/yarn`. Check for the existence of `bin/yarn` to decide if `yarn:install` should be run or not. The check for this is taken from `railties/lib/rails/app_updater.rb`, where it does the same: ```ruby options[:skip_javascript] = !File.exist?(Rails.root.join("bin", "yarn")) ``` --- railties/lib/rails/tasks/yarn.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/lib/rails/tasks/yarn.rake b/railties/lib/rails/tasks/yarn.rake index 527614237e..5da0b4081b 100644 --- a/railties/lib/rails/tasks/yarn.rake +++ b/railties/lib/rails/tasks/yarn.rake @@ -25,6 +25,6 @@ namespace :yarn do end # Run Yarn prior to Sprockets assets precompilation, so dependencies are available for use. -if Rake::Task.task_defined?("assets:precompile") +if Rake::Task.task_defined?("assets:precompile") && File.exist?(Rails.root.join("bin", "yarn")) Rake::Task["assets:precompile"].enhance [ "yarn:install" ] end