Skip to content

Make env.sh compatible with POSIX sh - #618

Open
RaphaelFakhri wants to merge 1 commit into
swiftlang:mainfrom
RaphaelFakhri:fix-posix-env-sh
Open

RaphaelFakhri wants to merge 1 commit into
swiftlang:mainfrom
RaphaelFakhri:fix-posix-env-sh

Conversation

@RaphaelFakhri

Copy link
Copy Markdown

Motivation

swiftly init adds . env.sh to ~/.profile for bash users without ~/.bash_profile or ~/.bash_login (the Debian and Ubuntu default) and for shells other than bash, zsh, and fish. POSIX sh login shells also read ~/.profile. On Debian and Ubuntu, /bin/sh is dash, which doesn't support the ${PATH//...} and ${PATH/#...} expansions that env.sh uses. dash reports Bad substitution and exits, so the rest of ~/.profile and the calling script don't run:

$ sh -c '. ~/.local/share/swiftly/env.sh; echo "$PATH"'
sh: 6: /root/.local/share/swiftly/env.sh: Bad substitution
$ echo $?
2

The substring removal also isn't anchored on both sides, so a PATH entry such as $SWIFTLY_BIN_DIR2 turns into 2.

Changes

  • Generate env.sh with POSIX parameter expansions that remove only whole PATH entries equal to SWIFTLY_BIN_DIR, then prepend it. The temporary variable is unset afterwards.
  • Recognize the previous env.sh format during the swiftly init upgrade path, so existing installs get the fixed file.
  • Add initEnvShIsPOSIXCompatible, which sources the generated env.sh with /bin/sh and checks the exit status and resulting PATH.
  • Add the previous format as a case in initUpgrade and update the expected output.

Testing

  • swift test --filter InitTests on Debian 13 (/bin/sh is dash), Swift 6.4.0: fails without the source change (initEnvShIsPOSIXCompatible and three initUpgrade cases) and passes with it (9 tests).
  • Sourced the new snippet with dash, bash, and busybox sh for PATH values with the bin directory at the start, middle, and end, repeated, absent, alone, empty, and with a similarly prefixed entry.
  • swift run swiftformat --lint --dryrun reports no changes.

Fixes #617

The generated env.sh used the bash-only ${PATH//...} and ${PATH/#...}
expansions. It is sourced from ~/.profile, which POSIX sh login shells
such as dash also read, and dash fails with "Bad substitution" and exits.
The substring removal also corrupted PATH entries that start with the
bin directory path.

Remove the bin directory from PATH with POSIX parameter expansions that
match whole entries only, and upgrade env.sh files written in the
previous format.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

env.sh uses bash-only syntax and breaks POSIX sh login shells

1 participant