Skip to content

Expand default_env values in the systemd service - #12

Merged
burisu merged 2 commits into
mainfrom
default-env-shell-expansion
Nov 9, 2026
Merged

burisu merged 2 commits into
mainfrom
default-env-shell-expansion

Conversation

@burisu

@burisu burisu commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Based on #10 (reuses run_cap from test/test_helper.rb). Merge after #10.

Problem

Issue #1: with rbenv, the service only starts after replacing ExecStart=/home/ubuntu/.rbenv/bin/rbenv exec bundle … by hand with ExecStart=/home/ubuntu/.rbenv/shims/bundle ….

capistrano-rbenv 2.2.0 (rbenv:map_bins, run after the stage is loaded):

  • prefixes bundle with $HOME/.rbenv/bin/rbenv exec;
  • adds rbenv_root: "$HOME/.rbenv" and rbenv_version to SSHKit.config.default_env, the same hash as fetch(:default_env).

In Capistrano commands, SSHKit exports each value inside double quotes (sshkit/command.rb:153-158), so the server shell replaces $HOME. The template, however, wrote the value as is. The generated unit contained:

Environment='RBENV_ROOT=$HOME/.rbenv'
Environment='RBENV_VERSION=3.3.0'
ExecStart=/home/ubuntu/.rbenv/bin/rbenv exec bundle exec rake solid_queue:start

ExecStart already goes through echo on the server, so its $HOME is replaced; the one in RBENV_ROOT isn't. The bundle shim exports RBENV_ROOT itself with an absolute path, hence the workaround in the issue.

Changes

Each default_env value is printed by the server shell (printf %s "<value>", double quotes escaped as in SSHKit) before being written into the unit. It therefore has the same value there as in Capistrano commands. An installed service isn't regenerated on deploy: cap <stage> solid_queue:install has to be run again (noted in the CHANGELOG).

Tests

  • test_default_env_is_expanded_by_the_server_shell: set :default_env, { rbenv_root: "$HOME/.rbenv", rbenv_version: "3.3.0" } → the unit contains RBENV_ROOT=<home>/.rbenv. Fails without the fix.
  • test_default_env_keeps_double_quotes: a say "hi" value stays say "hi".
  • test_default_env_keeps_values_starting_with_dashes: RUBY_CONFIGURE_OPTS=--with-jemalloc=… --enable-yjit (added by capistrano-asdf) comes through intact, with the printf of the CI (Linux).
  • Mutation: putting value.to_s back makes the first test fail; removing the double-quote escaping makes the second one fail.
  • By hand, with the three lines of rbenv:map_bins reproduced in a Capfile (after stage hook): the unit now contains Environment='RBENV_ROOT=/Users/brice/.rbenv'.
  • By hand, with capistrano-asdf 1.6.0 (origin/main of codeur/capistrano-asdf) loaded in the Capfile: same unit before and after the fix (PATH still excluded, RUBY_CONFIGURE_OPTS unchanged, ExecStart=<home>/.asdf/bin/asdf exec bundle …).
  • Not tested in systemd itself: that systemd doesn't expand $HOME in Environment= comes from its documentation (systemd.exec) and from the workaround described in the issue.
  • bundle exec rake (tests + standard): green locally (Ruby 4.0.6).

🤖 Generated with Claude Code

burisu and others added 2 commits November 9, 2026 09:29
capistrano-rbenv ajoute RBENV_ROOT="$HOME/.rbenv" à default_env. Dans
les commandes Capistrano, SSHKit exporte chaque valeur entre guillemets
doubles, donc le shell du serveur remplace $HOME. Le template écrivait
la valeur telle quelle : l'unité contenait RBENV_ROOT=$HOME/.rbenv,
alors que son ExecStart (rbenv exec bundle) passe par le shell et
contient le chemin absolu. Issue #1 : remplacer ExecStart par le shim
bundle, qui exporte lui-même RBENV_ROOT, faisait démarrer le service.

Chaque valeur passe maintenant par `printf %s "<valeur>"` sur le
serveur, avec les guillemets doubles échappés comme le fait SSHKit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
capistrano-asdf ajoute à default_env RUBY_CONFIGURE_OPTS, dont la
valeur commence par « --with-jemalloc » ou « --enable-yjit ». Depuis le
commit précédent, la valeur passe par printf sur le serveur : le test
vérifie, sur le printf de la CI (Linux), qu'elle arrive intacte dans
l'unité.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@burisu
burisu changed the base branch from quiet-raise-on-non-zero-exit to main November 9, 2026 08:40
@burisu
burisu marked this pull request as ready for review November 9, 2026 08:40
@burisu
burisu merged commit d1981e6 into main Nov 9, 2026
5 checks passed
@burisu
burisu deleted the default-env-shell-expansion branch November 9, 2026 08:40
@burisu burisu mentioned this pull request Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant