Skip to content

fix(hooks): initialize PUA_LANGUAGE to avoid unbound variable under set -u#145

Open
zhengjxu wants to merge 1 commit intotanweai:mainfrom
zhengjxu:fix/flavor-helper-unbound-language
Open

fix(hooks): initialize PUA_LANGUAGE to avoid unbound variable under set -u#145
zhengjxu wants to merge 1 commit intotanweai:mainfrom
zhengjxu:fix/flavor-helper-unbound-language

Conversation

@zhengjxu
Copy link
Copy Markdown

Problem

When ~/.pua/config.json does not exist (fresh install — user never ran /pua:survey or customized flavor), hooks/flavor-helper.sh fails with:

flavor-helper.sh: line 180: PUA_LANGUAGE: unbound variable

PUA_LANGUAGE is only assigned inside the if [ -f "$config" ] block (line 12), but referenced unconditionally at line 180 (the Chinese-language override check). Hooks running under set -u / set -eu abort on the unbound variable.

Repro

rm -f ~/.pua/config.json
bash -c 'set -eu; source hooks/flavor-helper.sh; get_flavor'
# → line 180: PUA_LANGUAGE: unbound variable

Fix

One line at the top of get_flavor():

 get_flavor() {
   local config="${HOME:-~}/.pua/config.json"
   local raw_flavor=""
+  PUA_LANGUAGE="${PUA_LANGUAGE:-}"

${PUA_LANGUAGE:-} respects any externally-set value and defaults to empty string otherwise. An empty value falls through all the Chinese-override branches correctly (they compare against "zh" / "中文").

Verification

$ rm -f ~/.pua/config.json
$ bash -c 'set -eu; source hooks/flavor-helper.sh; get_flavor; echo "flavor=$PUA_FLAVOR lang=[$PUA_LANGUAGE] OK"'
flavor=alibaba lang=[] OK

$ HOME=/tmp/nonexistent bash -c 'set -eu; source hooks/flavor-helper.sh; get_flavor; echo "no-config: flavor=$PUA_FLAVOR lang=[$PUA_LANGUAGE] OK"'
no-config: flavor=alibaba lang=[] OK

Both fresh-install (no config file) and normal paths pass under set -eu.

…et -u

PUA_LANGUAGE is only assigned inside the 'if [ -f $config ]' block, but
referenced unconditionally at line 180 (the Chinese-language override).
When ~/.pua/config.json does not exist (fresh install) and the hook runs
under set -u, flavor-helper.sh aborts with:

  flavor-helper.sh: line 180: PUA_LANGUAGE: unbound variable

Initialize to empty string at the top of get_flavor(); respects any
externally-set value and falls through all Chinese-override branches
correctly when empty.
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.

1 participant