Skip to content

Conversation

@headius
Copy link
Member

@headius headius commented Feb 21, 2025

A previous version of this script was incorrectly assuming that an uninitialized variable would be initialized to a "false" value, but uninitialized variables typically evaluate to a blank string which is considered true. As a result of this, no-argument calls behaved improperly, breaking the ability to pipe code into the jruby command.

This commit enables set -u for the JRuby launcher script, to trigger an error whenever an uninitialized variable is accessed. Variables we expect to possibly be uninitialized (such as various JAVA variables used to override the location of the java command) have been modified to use the ${variable-} format that does not trigger an error.

A previous version of this script was incorrectly assuming that
an uninitialized variable would be initialized to a "false" value,
but uninitialized variables typically evaluate to a blank string
which is considered true. As a result of this, no-argument calls
behaved improperly, breaking the ability to pipe code into the
jruby command.

This commit enables `set -u` for the JRuby launcher script, to
trigger an error whenever an uninitialized variable is accessed.
Variables we expect to possibly be uninitialized (such as various
JAVA variables used to override the location of the java command)
have been modified to use the ${variable-} format that does not
trigger an error.
At least one IRB spec clears env before running a subprocess, and
POSIX does not require $HOME, so avoid using it if unset.
Copy link
Contributor

@mrnoname1000 mrnoname1000 left a comment

Choose a reason for hiding this comment

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

In personal scripts I prefer to test that a variable is set with [ -z ${var+x} ] which is faster because it expands to either [ -z x ] or [ -z ] (which technically tests that -z isn't an empty string). There are performance implications of testing large variables with [ ] but probably not significant enough to overrule stylistic choices.

@headius
Copy link
Member Author

headius commented Feb 21, 2025

In personal scripts I prefer to test that a variable is set with [ -z ${var+x} ]

That's clever but I fear having to explain it to anyone new to shell. I suppose it is cleaner, since you are not actually consuming the contents of the variable there. Maybe we'll do it later.

@mrnoname1000
Copy link
Contributor

We could maintain quotes and use [ -z "${var+x}" ] since it's much more obviously checking if "is variable set" expands to anything

@headius
Copy link
Member Author

headius commented Feb 21, 2025

@mrnoname1000 Sure, we can do that as a separate PR. Probably should include at least a little doco to explain that pattern.

@headius headius merged commit e9b798c into jruby:master Feb 21, 2025
95 checks passed
@headius headius deleted the set_u branch February 21, 2025 23:47
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