Skip to content

Conversation

richcarl
Copy link
Contributor

@richcarl richcarl commented May 10, 2025

The sys.config.src file may since OTP 21 contain env variables with defaults using the standard shell syntax ${FOO:-some_default}. The only place where Rebar itself needs to worry about this is in the shell command which wants to load the sys.config, and must then perform the substitutions first. This commit implements support for such env variable defaults. Previously, Rebar would crash if it encountered this syntax.

By some simple refactoring, this also makes the rebar3 ct --sys_config option work on sys.config.src files.

@richcarl
Copy link
Contributor Author

Would be nice to get this merged - it allows you to write sys.config.src files like this:

 [{https_port, ${HTTPS_PORT:-8443}}
 , {https_cert_dir, "${CERT_DIR:-certs}"}

and get the variables replaced at build time if set, and with reasonable default values if not.

Copy link
Collaborator

@ferd ferd left a comment

Choose a reason for hiding this comment

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

Should be good to go, but I'm proposing a bit of a simplification/encapsulation change to the parsing rule.

nomatch ->
error;
{match, [Name,Default]} ->
%% the Default part will include the ":-" prefix if present
Copy link
Collaborator

Choose a reason for hiding this comment

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

using ([a-zA-Z_]+[a-zA-Z0-9_]*)(:-([^}]*))?} for the regex would let you capture [1,3] and not have to handle the :- operator in the output, and simplify replace_varname/2.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. When testing this, it turned out that the regex also needed to be left-anchored, otherwise it will match on the later part of a variable with a bad character, e.g. "HELLO-FERD}" would return a match on FERD as the variable name.

The sys.config.src file may since OTP 21 contain env variables with
defaults using the standard shell syntax `${FOO:-some_default}`.  The
only place where Rebar itself needs to worry about this is in the
`shell` command which wants to load the sys.config, and must then
perform the substitutions first. This commit implements support for
such env variable defaults. Previously, Rebar would crash if it
encountered this syntax.
Also ensures match is left-anchored.
@richcarl richcarl force-pushed the shell-env-subst-defaults branch from 6fb2b3e to d1606ef Compare September 19, 2025 11:42
@ferd ferd merged commit 2235876 into erlang:main Sep 23, 2025
6 checks passed
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