Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/capistrano/asdf/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Capistrano
module Asdf
VERSION = "1.5.5"
VERSION = "1.6.0"
end
end
30 changes: 27 additions & 3 deletions lib/capistrano/tasks/asdf.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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.
Expand Down
25 changes: 4 additions & 21 deletions test/capistrano/test_map_bins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
140 changes: 140 additions & 0 deletions test/capistrano/test_yjit.rb
Original file line number Diff line number Diff line change
@@ -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
48 changes: 48 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading