Skip to content

[core] Fix shell detection with multiline commands - #23747

Open
jinchuntan wants to merge 1 commit into
root-project:masterfrom
jinchuntan:fix-thisroot-cmdline-newline
Open

jinchuntan wants to merge 1 commit into
root-project:masterfrom
jinchuntan:fix-thisroot-cmdline-newline

Conversation

@jinchuntan

Copy link
Copy Markdown

This pull request

Changes or fixes

Fixes #23746.

A newline in a shell argument makes cut include text from later lines. This can send thisroot.sh through the wrong setup branch and leave ROOTSYS unset.

This follows the approach suggested in the issue. It converts the NUL separators to newlines and prints the first line. Using sed keeps the input flowing so long commands also work with pipefail enabled.

The regression test covers bash and zsh with single line commands and multiline commands. It also checks a long command with pipefail enabled.

Checklist

  • Tested the changed behavior locally with configured script fixtures
  • No documentation update needed

The original version failed the multiline cases. The head version failed the long command cases. The final change passed all six cases.

The workspace reports process IDs that differ from its /proc entries. The temporary fixtures were adapted to read the real process ID. The committed test has no such adaptation. A full ROOT build and a run on a normal Linux system are still unverified.

Codex with GPT 6 helped prepare the patch and tests.

A newline in a shell argument makes cut return text from later lines.
This can select the wrong setup path and leave ROOTSYS unset.

Read the first line after converting the NUL separators to newlines.
Keep reading the stream so long commands also work with pipefail.

Add regression coverage for bash and zsh.

Fixes root-project#23746

Assisted-by: codex:gpt-6
@dpiparo dpiparo assigned bellenot and unassigned dpiparo Oct 20, 2026
@dpiparo
dpiparo removed their request for review October 20, 2026 11:00
@github-actions

Copy link
Copy Markdown

Test Results

    24 files      24 suites   4d 1h 10m 10s ⏱️
 3 889 tests  3 888 ✅ 0 💤 1 ❌
83 262 runs  83 261 ✅ 0 💤 1 ❌

For more details on these failures, see this check.

Results for commit 8f8c0e7.

Comment thread config/thisroot.sh
# Determine the shell executable filename.
if [ -r "/proc/$$/cmdline" ]; then
trueExe=$(cut -d '' -f1 /proc/$$/cmdline 2>/dev/null) || trueExe=$(xargs -0 -n 1 < /proc/$$/cmdline | head -n 1) || return 1
trueExe=$(tr '\0' '\n' < /proc/$$/cmdline 2>/dev/null | sed -n '1p') || trueExe=$(xargs -0 -n 1 < /proc/$$/cmdline | head -n 1) || return 1

@ferdymercury ferdymercury Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Would it also work to do instead:

head -n 1 /proc/$$/cmdline 2>/dev/null | cut -d '' -f1

or does that file with long commands also?

as suggested by mkelement0 in https://stackoverflow.com/a/23011530/7471760

and what about basename -- "$(readlink /proc/$$/exe)"

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

thisroot.sh: wrong ROOTSYS in zsh when the shell's command line has a newline

5 participants