Repository navigation
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Code Review
This pull request refactors the cr-ci deployment scripts to ensure compatibility with Bash 3.2 (the default on macOS), removing the dependency on Bash 4+. This is achieved by replacing associative arrays with indirect variable expansion helpers and introducing a Go static analysis test to prevent the future introduction of Bash 4+ constructs. The review feedback identifies critical issues with the compatibility implementation: specifically, combining indirect expansion with parameter expansion operators (such as ${!_var+x}) causes a 'bad substitution' runtime error in Bash, which can be resolved using eval. Additionally, a variable shadowing bug in crci_cfg_into was flagged where the local variable _var could conflict with the caller's variable during printf -v operations.
| crci_is_known_key() { | ||
| [[ $1 =~ ^[A-Z][A-Z0-9_]*$ ]] || return 1 | ||
| local _var="CRCI_DEFAULT_$1" | ||
| [[ -n ${!_var+x} ]] | ||
| } |
There was a problem hiding this comment.
Combining indirect expansion with parameter expansion operators (such as ${!_var+x}) is not supported in Bash and results in a bad substitution runtime error. To check if an indirect variable is set in a way that is compatible with Bash 3.2 and newer, use eval with a standard parameter expansion check.
| crci_is_known_key() { | |
| [[ $1 =~ ^[A-Z][A-Z0-9_]*$ ]] || return 1 | |
| local _var="CRCI_DEFAULT_$1" | |
| [[ -n ${!_var+x} ]] | |
| } | |
| crci_is_known_key() { | |
| [[ $1 =~ ^[A-Z][A-Z0-9_]*$ ]] || return 1 | |
| local _var="CRCI_DEFAULT_$1" | |
| eval "[[ -n \${$_var+x} ]]" | |
| } |
| local _seen="CRCI_SEEN_$key" | ||
| [[ -n ${!_seen+x} ]] && crci_die "$CRCI_CONFIG:$n: $key is set twice" |
There was a problem hiding this comment.
Similar to crci_is_known_key, combining indirect expansion with alternative value testing (such as ${!_seen+x}) causes a bad substitution runtime error in Bash. Use eval to safely perform this check in a compatible manner.
| local _seen="CRCI_SEEN_$key" | |
| [[ -n ${!_seen+x} ]] && crci_die "$CRCI_CONFIG:$n: $key is set twice" | |
| local _seen="CRCI_SEEN_$key" | |
| eval "[[ -n \${$_seen+x} ]]" && crci_die "$CRCI_CONFIG:$n: $key is set twice" |
| crci_cfg_into() { | ||
| local _var="CRCI_CFG_$2" | ||
| printf -v "$1" '%s' "${!_var}" | ||
| } |
There was a problem hiding this comment.
If a caller passes a variable named _var to crci_cfg_into (e.g., crci_cfg_into _var "KEY"), the local variable _var inside crci_cfg_into will shadow the caller's variable, causing printf -v to write to the local variable instead of the caller's. Renaming the local variable to a highly unique name (like __cfg_var) prevents this shadowing bug.
| crci_cfg_into() { | |
| local _var="CRCI_CFG_$2" | |
| printf -v "$1" '%s' "${!_var}" | |
| } | |
| crci_cfg_into() { | |
| local __cfg_var="CRCI_CFG_$2" | |
| printf -v "$1" '%s' "${!__cfg_var}" | |
| } |
Part of ptone#9170
Staging PR: ptone#9789
declare -g/-A,local -A,[[ -v ... ]],${var,,}/${var^^},mapfile, bash >= 4 gate) across all 10cr-ci/scripts/*.shscripts so deploy/ops scripts run on macOS/bin/bash3.2.cr-ci/scripts/README.mdandcr-ci/docs/gcp-setup.mdto bash >= 3.2.TestScriptsBash32Compatibleincr-ci/scripts/bash32_test.go.cr-ci/scriptsand mediator parity tests pass).