Skip to content

fix: keep the user's HOME when wrapping commands with sudo - #588

Merged
lvaroqui merged 1 commit into
mainfrom
fix/sudo-preserve-home
Oct 16, 2026
Merged

lvaroqui merged 1 commit into
mainfrom
fix/sudo-preserve-home

Conversation

@lvaroqui

Copy link
Copy Markdown
Contributor

Pass the bare --preserve-env to sudo alongside the explicit --preserve-env=<list>, so commands wrapped with sudo keep the invoking user's HOME again.

#574 switched to the list form because sudo-rs ignores the bare flag. With only the list, classic sudo keeps env_reset on, and in that mode variables exported by PAM session modules override the preserved ones (env_merge only skips variables in env_keep). Since systemd v260, pam_systemd always exports HOME with the target user's home directory, so on distros whose /etc/pam.d/sudo loads it (Arch: session optional pam_systemd.so class=none), wrapped commands ran with HOME=/root.

One visible effect: the walltime samply re-exec inherits CODSPEED_PROFILE, reads root's CodSpeed config, and fails with CodSpeed profile `<name>` does not exist when the profile only exists in the user's config. Anything else run through wrap_with_sudo that relies on the user's HOME (toolchains, caches) was affected too.

With both flags, classic sudo disables env_reset (as before #574) and sudo-rs still honors the list. HOME, CODSPEED_PROFILE and an arbitrary variable reach the command unchanged with both flags on:

sudo before #574 (bare) #574 (list) this PR (both)
sudo-rs 0.2.13, Ubuntu 26.04 hosted runner and container all dropped, HOME=/root kept kept
classic sudo (sudo.ws), Ubuntu 26.04 kept kept kept
classic sudo 1.9.15p5, Ubuntu 24.04 kept kept kept
classic sudo 1.9.17p2 + systemd 261, Arch kept HOME=/root kept

Refs COD-3305: that issue tracks Ubuntu 26.04 support, which this keeps working while fixing a regression it introduced elsewhere.

Since we pass only `--preserve-env=<list>` (for sudo-rs, which ignores the
bare flag), classic sudo keeps `env_reset` on. Variables exported by PAM
session modules then override the preserved ones: systemd >= 260's
pam_systemd exports HOME, so on distros whose sudo PAM stack loads it
(e.g. Arch) commands ran with HOME=/root. The samply re-exec then read
root's CodSpeed config and failed when the selected profile only existed
in the user's.

Pass the bare `--preserve-env` as well: classic sudo disables `env_reset`
and keeps HOME as before, and sudo-rs still honors the list.

Refs COD-3305
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lvaroqui
lvaroqui marked this pull request as ready for review October 15, 2026 16:18
@greptile-apps

greptile-apps Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The change appears safe to merge.

Summary

Updates wrap_with_sudo to pass both forms of --preserve-env.

  • Commands wrapped with sudo keep the caller’s environment.

Reviews (1) · Last reviewed commit: "fix: keep the user's HOME when wrapping ..." · Reviewed by Greptile

@codspeed

codspeed Bot commented Oct 15, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

⚠️ 8 benchmarks spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

✅ 41 untouched benchmarks
⏩ 6 skipped benchmarks1


Comparing fix/sudo-preserve-home (f52d120) with main (9e74317)

Open in CodSpeed

Footnotes

  1. 6 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@GuillaumeLagrange GuillaumeLagrange left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@lvaroqui
lvaroqui merged commit 53c7660 into main Oct 16, 2026
66 checks passed
@lvaroqui
lvaroqui deleted the fix/sudo-preserve-home branch October 16, 2026 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants