From fc8d01306556de5f5935b2ea91026063a1f6daec Mon Sep 17 00:00:00 2001 From: Brice TEXIER Date: Thu, 29 Oct 2026 16:08:59 +0100 Subject: [PATCH 1/2] Compiler Ruby avec YJIT quand rustc est sur le serveur MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ruby ne compile YJIT que s'il trouve rustc au moment de la compilation, et asdf install saute une version déjà installée : le Ruby 4.0.6 d'app5, compilé sans rustc, ne recevrait jamais YJIT. La variable asdf_ruby_use_yjit (vrai par défaut) ajoute --enable-yjit aux options de compilation de Ruby quand rustc est présent, et asdf:install recompile dans son propre dossier un Ruby installé sans YJIT. À faux, Ruby est compilé avec --disable-yjit. Co-Authored-By: Claude Opus 5.5 --- README.md | 14 ++++ lib/capistrano/tasks/asdf.rake | 30 ++++++- test/capistrano/test_map_bins.rb | 25 +----- test/capistrano/test_yjit.rb | 140 +++++++++++++++++++++++++++++++ test/test_helper.rb | 48 +++++++++++ 5 files changed, 233 insertions(+), 24 deletions(-) create mode 100644 test/capistrano/test_yjit.rb diff --git a/README.md b/README.md index 486f3ac..618bc40 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ If you need some special settings, set those in the stage file for your server: set :asdf_tools, %w{ ruby } # defaults to %{ ruby nodejs } set :asdf_map_ruby_bins, %w{ bundle gem } # defaults to %w{ rake gem bundle ruby rails } set :asdf_map_nodejs_bins, %w{ node npm } # defaults to %w{ node npm yarn } + set :asdf_ruby_use_yjit, false # defaults to true ### Custom ASDF path: `:asdf_path` @@ -72,6 +73,19 @@ For example; if you just want to map `node` and `npm` nodejs binaries, you may s set :asdf_map_nodejs_bins, %w{ node npm } +### Ruby with YJIT: `:asdf_ruby_use_yjit` + +Ruby only builds YJIT when it finds `rustc` (1.58 or later) while it compiles. +When `rustc` is in the server's default `PATH`, as with a system package, the +gem builds Ruby with `--enable-yjit`. A Ruby of your `.tool-versions` that was +built without YJIT is built again at the next deploy, in its own directory: it +keeps running until the new build replaces its files. Without `rustc`, nothing +changes. + +To build Ruby without YJIT, even when `rustc` is there: + + set :asdf_ruby_use_yjit, false + ## Restrictions Capistrano can't use ASDF to install rubies, nodes or other tools yet. diff --git a/lib/capistrano/tasks/asdf.rake b/lib/capistrano/tasks/asdf.rake index 7665814..b8372b0 100644 --- a/lib/capistrano/tasks/asdf.rake +++ b/lib/capistrano/tasks/asdf.rake @@ -59,6 +59,19 @@ namespace :asdf do end end execute(:asdf, "install") + + # `asdf install` skips a version that is already there, so a Ruby built + # before rustc was around would never get YJIT: build it again in its + # own directory. ruby-build compiles in a temporary directory and only + # then replaces the files, so this Ruby keeps working meanwhile. + if SSHKit.config.default_env[:ruby_configure_opts]&.include?("--enable-yjit") + ruby_without_yjit = capture(:asdf, "exec", "ruby", "-e", "'print RbConfig::CONFIG[%q(prefix)] if RbConfig::CONFIG[%q(YJIT_SUPPORT)] == %q(no)'") + unless ruby_without_yjit.empty? + with(asdf_install_type: "version", asdf_install_version: File.basename(ruby_without_yjit), asdf_install_path: ruby_without_yjit) do + execute("#{fetch(:asdf_path)}/plugins/ruby/bin/install") + end + end + end end end end @@ -97,11 +110,21 @@ namespace :asdf do asdf_prefix = fetch(:asdf_prefix, -> { "#{fetch(:asdf_path)}/bin/asdf exec" }) SSHKit.config.command_map[:asdf] = "#{fetch(:asdf_path)}/bin/asdf" - if fetch(:asdf_tools).include?("ruby") && fetch(:asdf_ruby_use_jemalloc) + if fetch(:asdf_tools).include?("ruby") on roles(fetch(:asdf_roles)) do - if test("[ -f #{fetch(:asdf_jemalloc_path)}/jemalloc.h ]") - SSHKit.config.default_env.merge!(ruby_configure_opts: "--with-jemalloc=#{fetch(:asdf_jemalloc_path)}") + ruby_configure_opts = [] + if fetch(:asdf_ruby_use_jemalloc) && test("[ -f #{fetch(:asdf_jemalloc_path)}/jemalloc.h ]") + ruby_configure_opts << "--with-jemalloc=#{fetch(:asdf_jemalloc_path)}" + end + # Ruby builds YJIT by itself whenever it finds rustc, hence the explicit + # opt-out. Asking for it turns a rustc unable to build it into a failed + # install instead of a Ruby silently built without YJIT. + if !fetch(:asdf_ruby_use_yjit) + ruby_configure_opts << "--disable-yjit" + elsif test("command -v rustc") + ruby_configure_opts << "--enable-yjit" end + SSHKit.config.default_env.merge!(ruby_configure_opts: ruby_configure_opts.join(" ")) if ruby_configure_opts.any? end end @@ -128,6 +151,7 @@ namespace :load do set :asdf_roles, fetch(:asdf_roles, :all) set :asdf_ruby_use_jemalloc, fetch(:asdf_ruby_use_jemalloc, true) set :asdf_jemalloc_path, fetch(:asdf_jemalloc_path, "/usr/include/jemalloc") + set :asdf_ruby_use_yjit, fetch(:asdf_ruby_use_yjit, true) # Autodetected from the project .tool-versions, but only when the # application has not listed its tools itself: a passed default is always # evaluated, and reading a missing file would raise. diff --git a/test/capistrano/test_map_bins.rb b/test/capistrano/test_map_bins.rb index ec5dfec..751981c 100644 --- a/test/capistrano/test_map_bins.rb +++ b/test/capistrano/test_map_bins.rb @@ -2,35 +2,18 @@ require "test_helper" -require "capistrano/all" -require "sshkit" - -# A Capfile mixes the DSL into the top-level object; the rake file under test -# reaches for `Capistrano::DSL.stages` as it loads, which relies on it. -include Capistrano::DSL # standard:disable Style/MixinUsage - module Capistrano # `asdf:map_bins` prepends the asdf shims to the PATH that SSHKit exports in # front of every remote command. SSHKit writes that environment inside double # quotes (`PATH="..."`), and a shell does not expand a tilde there, so the # PATH must be built from `$HOME` rather than from `~`. class TestMapBins < Minitest::Test - include Capistrano::DSL + include AsdfTasks def setup - # `capistrano/all` turns Rake's trace on; keep it out of the test output. - Rake.application.options.trace = false - Rake::Task.clear - Capistrano::Configuration.reset! - SSHKit.config.default_env = {} - # The rake file hooks itself onto this task as it loads. - Rake::Task.define_task("deploy:updating") - load File.expand_path("../../lib/capistrano/tasks/asdf.rake", __dir__) - # Listing the tools keeps the defaults away from the .tool-versions - # autodetection, and the task away from its jemalloc branch, which opens - # an SSH connection. - set :asdf_tools, %w[nodejs] - Rake::Task["load:defaults"].invoke + # Without ruby, the task stays away from the Ruby build options, which + # open an SSH connection. + load_asdf_tasks(%w[nodejs]) end def teardown diff --git a/test/capistrano/test_yjit.rb b/test/capistrano/test_yjit.rb new file mode 100644 index 0000000..e3b9d88 --- /dev/null +++ b/test/capistrano/test_yjit.rb @@ -0,0 +1,140 @@ +# frozen_string_literal: true + +require "test_helper" + +module Capistrano + # Ruby builds YJIT only when it finds rustc at compile time. With rustc on the + # server, `asdf:map_bins` asks for YJIT in the Ruby build options, and the + # deploy builds again an installed Ruby that was compiled without it, which + # `asdf install` alone would skip. + class TestYjit < Minitest::Test + include AsdfTasks + + RUSTC = /\Acommand -v rustc\z/ + JEMALLOC = /jemalloc\.h/ + YJIT_SUPPORT = /YJIT_SUPPORT/ + RUBY_PREFIX = "/home/deploy/.asdf/installs/ruby/4.0.6" + + def setup + load_asdf_tasks(%w[ruby]) + server "app.example.com", roles: %w[app] + set :deploy_to, "/var/www/app" + @backend = SSHKit.config.backend + SSHKit.config.backend = FakeBackend + FakeBackend.commands = [] + FakeBackend.replies = [[JEMALLOC, 1], [RUSTC, 1]] + end + + def teardown + SSHKit.config.backend = @backend + SSHKit.config.default_env = {} + end + + def test_rustc_on_the_server_asks_ruby_for_yjit + reply RUSTC, 0 + + Rake::Task["asdf:map_bins"].invoke + + assert_equal "--enable-yjit", ruby_configure_opts + end + + def test_jemalloc_is_kept_beside_yjit + reply RUSTC, 0 + reply JEMALLOC, 0 + + Rake::Task["asdf:map_bins"].invoke + + assert_equal "--with-jemalloc=/usr/include/jemalloc --enable-yjit", ruby_configure_opts + end + + def test_without_rustc_ruby_is_built_as_before + reply JEMALLOC, 0 + + Rake::Task["asdf:map_bins"].invoke + + assert_equal "--with-jemalloc=/usr/include/jemalloc", ruby_configure_opts + end + + def test_the_application_can_turn_yjit_off + set :asdf_ruby_use_yjit, false + reply RUSTC, 0 + + Rake::Task["asdf:map_bins"].invoke + + assert_equal "--disable-yjit", ruby_configure_opts + end + + def test_the_application_can_still_turn_jemalloc_off + set :asdf_ruby_use_jemalloc, false + reply JEMALLOC, 0 + + Rake::Task["asdf:map_bins"].invoke + + assert_nil ruby_configure_opts + end + + def test_an_application_without_ruby_gets_no_ruby_build_options + set :asdf_tools, %w[nodejs] + reply RUSTC, 0 + reply JEMALLOC, 0 + + Rake::Task["asdf:map_bins"].invoke + + assert_nil ruby_configure_opts + end + + def test_the_deploy_builds_again_a_ruby_without_yjit + reply RUSTC, 0 + reply YJIT_SUPPORT, 0, RUBY_PREFIX + + deploy + + assert_equal 1, ruby_builds.size + build = ruby_builds.first + assert build.start_with?("cd /var/www/app/current && ") + assert_includes build, 'ASDF_INSTALL_TYPE="version"' + assert_includes build, 'ASDF_INSTALL_VERSION="4.0.6"' + assert_includes build, %(ASDF_INSTALL_PATH="#{RUBY_PREFIX}") + assert_includes build, 'RUBY_CONFIGURE_OPTS="--enable-yjit"' + end + + def test_the_deploy_leaves_a_ruby_with_yjit_alone + reply RUSTC, 0 + reply YJIT_SUPPORT, 0, "" + + deploy + + assert_empty ruby_builds + end + + def test_without_rustc_the_deploy_builds_nothing_again + reply YJIT_SUPPORT, 0, RUBY_PREFIX + + deploy + + assert_empty FakeBackend.commands.grep(YJIT_SUPPORT) + assert_empty ruby_builds + end + + private + + def reply(pattern, exit_status, stdout = "") + FakeBackend.replies.unshift([pattern, exit_status, stdout]) + end + + # `asdf:map_bins` runs as the stage loads, before the deploy installs the + # tools. + def deploy + Rake::Task["asdf:map_bins"].invoke + Rake::Task["asdf:install"].invoke + end + + def ruby_configure_opts + SSHKit.config.default_env[:ruby_configure_opts] + end + + def ruby_builds + FakeBackend.commands.grep(%r{/plugins/ruby/bin/install}) + end + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 9f80126..907a83e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -4,3 +4,51 @@ require "capistrano/asdf/version" require "minitest/autorun" + +require "capistrano/all" +require "sshkit" + +# A Capfile mixes the DSL into the top-level object; the rake file under test +# reaches for `Capistrano::DSL.stages` as it loads, which relies on it. +include Capistrano::DSL # standard:disable Style/MixinUsage + +module AsdfTasks + include Capistrano::DSL + + # Loads the asdf tasks afresh, for an application whose asdf tools are + # `tools`. + def load_asdf_tasks(tools) + # `capistrano/all` turns Rake's trace on; keep it out of the test output. + Rake.application.options.trace = false + Rake::Task.clear + Capistrano::Configuration.reset! + SSHKit.config.default_env = {} + # The rake file hooks itself onto this task as it loads. + Rake::Task.define_task("deploy:updating") + load File.expand_path("../lib/capistrano/tasks/asdf.rake", __dir__) + # Listing the tools keeps the defaults away from the .tool-versions + # autodetection. + set :asdf_tools, tools + Rake::Task["load:defaults"].invoke + end +end + +# Stands for the SSH connection: records each remote command as the shell +# would receive it, and answers it from `replies`, a list of +# `[pattern, exit_status, stdout]` where the first matching pattern wins. Any +# other command succeeds without output. +class FakeBackend < SSHKit::Backend::Abstract + class << self + attr_accessor :commands, :replies + end + + private + + def execute_command(cmd) + command = cmd.to_command + self.class.commands << command + _, exit_status, stdout = self.class.replies.find { |pattern, *| pattern.match?(command) } + cmd.on_stdout(nil, stdout.to_s) + cmd.exit_status = exit_status || 0 + end +end From 60716f203a341fa832b0b608af0f229cfb2c36c6 Mon Sep 17 00:00:00 2001 From: Brice TEXIER Date: Thu, 29 Oct 2026 16:08:59 +0100 Subject: [PATCH 2/2] Passer en 1.6.0 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 16 ++++++++++++++++ lib/capistrano/asdf/version.rb | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5999b1e..1467022 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,22 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.6.0] - 2026-10-29 + +### Added + +- `asdf_ruby_use_yjit` variable, defaulting to `true`. When `rustc` is found on + the server, Ruby is built with `--enable-yjit`, and `asdf:install` builds + again, in its own directory, an installed Ruby that was compiled without + YJIT, which `asdf install` skips as already installed. Set it to `false` to + build Ruby with `--disable-yjit`. + +### Fixed + +- `load:defaults` no longer reads the project `.tool-versions` when + `asdf_tools` is already set as it runs, that is in the Capfile. Set in + `deploy.rb` or in a stage file, it comes too late: the file is still read. + ## [1.5.5] - 2026-09-15 ### Fixed diff --git a/lib/capistrano/asdf/version.rb b/lib/capistrano/asdf/version.rb index 4d71e3d..ff9f119 100644 --- a/lib/capistrano/asdf/version.rb +++ b/lib/capistrano/asdf/version.rb @@ -2,6 +2,6 @@ module Capistrano module Asdf - VERSION = "1.5.5" + VERSION = "1.6.0" end end