Skip to content

Commit 8123714

Browse files
authored
Fix build_data::get_previous() discarding stored false values (#2077)
The jq `//` (alternative) operator treats both `null` and `false` as falsy, so a stored boolean `false` would return empty string instead of `"false"`. Fixed by replacing with an explicit `has()` check. Though in practice, no callers of `get_previously()` retrieve boolean fields, so currently nothing was affected by this. Also use `--arg` for the jq key parameter in both `build_data::has()` and `build_data::get_previous()`, instead of string interpolation, for consistency with `build_data::_set()`, and to avoid issues if the key name contained any special characters (though in practice they shouldn't given the names aren't user controlled). GUS-W-21977968. GUS-W-21977978.
1 parent ab1d03f commit 8123714

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

lib/build_data.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ function build_data::_set() {
135135
function build_data::has() {
136136
local key="${1}"
137137

138-
jq --exit-status "has(\"${key}\")" "${BUILD_DATA_FILE}" >/dev/null
138+
jq --exit-status --arg key "${key}" 'has($key)' "${BUILD_DATA_FILE}" >/dev/null
139139
}
140140

141141
# Retrieve the value of an entry in the build data store from the previous successful build.
@@ -158,9 +158,9 @@ function build_data::get_previous() {
158158
# last matching entry in the file. The empty string is returned if the key wasn't found.
159159
tac "${LEGACY_BUILD_DATA_FILE}" | { grep --perl-regexp --only-matching --max-count=1 "^${key}=\K.*$" || true; }
160160
elif [[ -f "${PREVIOUS_BUILD_DATA_FILE}" ]]; then
161-
# The `// empty` ensures we return the empty string rather than `null` if the key doesn't exist.
161+
# By default jq will return `null` if the key isn't found, so we must handle this explicitly.
162162
# We don't use `--exit-status` since `false` is a valid value for us to retrieve.
163-
jq --raw-output ".${key} // empty" "${PREVIOUS_BUILD_DATA_FILE}"
163+
jq --raw-output --arg key "${key}" 'if has($key) then .[$key] else empty end' "${PREVIOUS_BUILD_DATA_FILE}"
164164
fi
165165
}
166166

0 commit comments

Comments
 (0)