Skip to content

fix(completions): remove dead branch referencing undefined variable in bash completion - #28744

Closed
JakubPecenka wants to merge 1 commit into
oven-sh:mainfrom
JakubPecenka:fix-bash-completion-empty-regex
Closed

fix(completions): remove dead branch referencing undefined variable in bash completion#28744
JakubPecenka wants to merge 1 commit into
oven-sh:mainfrom
JakubPecenka:fix-bash-completion-empty-regex

Conversation

@JakubPecenka

@JakubPecenka JakubPecenka commented Apr 1, 2026

Copy link
Copy Markdown

What does this PR do?

Fixes bash tab completion error: -bash: [[: invalid regular expression: empty (sub)expression.

re_comp_word_script is referenced on line 60 of completions/bun.bash but was never defined anywhere in the script (since the original PR #403 in 2022). This causes [[ ... =~ ${re_comp_word_script} ]] to fail with the above error every time tab completion triggers _read_scripts_in_package_json (e.g. bun run <TAB> or bun <TAB> in a directory with a package.json).

Rather than guarding the undefined variable with -n, this removes the dead branch entirely. Since re_comp_word_script was never defined, the condition always evaluated to false — the branch was unreachable dead code. The first branch already correctly handles filtering scripts from completions once a script has been selected as the previous word.

How did you verify your code works?

Tested with a bash test suite that simulates completion by setting COMP_WORDS/COMP_CWORD and calling _bun_completions directly — 39 tests covering:

Scenario Original (brew 1.3.11) Fixed
bun run <TAB> no stderr FAILinvalid regular expression: empty (sub)expression PASS
bun <TAB> no stderr FAIL — same error PASS
bun dev <TAB> no stderr FAIL — same error PASS
bun run dev <TAB> no stderr PASS (first branch matches, dead branch skipped) PASS
Script completion (bun run <TAB> shows scripts) PASS PASS
Prefix filtering (bun run d<TAB>dev) PASS PASS
Script dedup (bun run dev <TAB> filters other scripts) PASS PASS
Subcommands, options, --cwd, no package.json, etc. (36 tests) PASS PASS

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@coderabbitai

coderabbitai Bot commented Apr 1, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: afb12732-1f34-41c8-9842-b6b11a1bc368

📥 Commits

Reviewing files that changed from the base of the PR and between 3756d6e1c6d06f1ced816793b6595e31772ba911 and 3e5d275.

📒 Files selected for processing (1)
  • completions/bun.bash

Walkthrough

Simplified _read_scripts_in_package_json conditional: removed the re_comp_word_script-based branch so the script-replacement block now only runs when COMPREPLY[*] matches re_prev_script and COMP_WORDS[2] is non-empty.

Changes

Cohort / File(s) Summary
Completion conditional simplification
completions/bun.bash
Removed the alternative regex match against re_comp_word_script and retained only the re_prev_script + -n "${COMP_WORDS[2]}" condition in _read_scripts_in_package_json, reducing conditional complexity and narrowing when the script-replacement block triggers.
🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: removing a dead branch that references an undefined variable in bash completion.
Description check ✅ Passed The description includes both required sections with detailed context, verification steps, and a comprehensive test table demonstrating the fix.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
completions/bun.bash (1)

56-66: ⚠️ Potential issue | 🟡 Minor

The guard prevents the error, but re_comp_word_script is never defined—leaving dead code.

The -n guard on line 60 correctly prevents the regex error. However, since re_comp_word_script is never defined anywhere in this file, the condition will always evaluate to false, making the entire second branch of the OR unreachable dead code.

Choose one of these approaches:

  1. If the variable was intended to be defined, add the missing definition (similar to re_prev_script on line 57)—possibly local re_comp_word_script="(^| )${cur_word}($| )";
  2. If the branch is unnecessary, remove the dead code entirely for clarity
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@completions/bun.bash` around lines 56 - 66, The conditional uses an undefined
variable re_comp_word_script, leaving dead/unreachable code; either define
re_comp_word_script (e.g., mirror re_prev_script using cur_word: create a local
re_comp_word_script="(^| )${cur_word}($| )" before the conditional) so the
second branch can work, or remove the entire second branch that references
re_comp_word_script and its related logic (the “-n \"${re_comp_word_script}\" &&
\"${COMPREPLY[*]}\" =~ ${re_comp_word_script}” part and any code only relevant
to that branch) to eliminate the dead code while keeping behavior around
re_prev_script, prev, cur_word, package_json_compreply, COMPREPLY and
replaced_script unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@completions/bun.bash`:
- Around line 56-66: The conditional uses an undefined variable
re_comp_word_script, leaving dead/unreachable code; either define
re_comp_word_script (e.g., mirror re_prev_script using cur_word: create a local
re_comp_word_script="(^| )${cur_word}($| )" before the conditional) so the
second branch can work, or remove the entire second branch that references
re_comp_word_script and its related logic (the “-n \"${re_comp_word_script}\" &&
\"${COMPREPLY[*]}\" =~ ${re_comp_word_script}” part and any code only relevant
to that branch) to eliminate the dead code while keeping behavior around
re_prev_script, prev, cur_word, package_json_compreply, COMPREPLY and
replaced_script unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 3e272042-1857-4a7b-bb8f-9176797f065b

📥 Commits

Reviewing files that changed from the base of the PR and between 3ed4186 and 42c00dc8ae7a8e7bf3c3b3f1da62107f6bb79316.

📒 Files selected for processing (1)
  • completions/bun.bash

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
completions/bun.bash (1)

1-1: 🧹 Nitpick | 🔵 Trivial

Pre-existing: Shebang is missing the ! character.

The shebang line should be #!/usr/bin/env bash instead of #/usr/bin/env bash. This is a pre-existing issue unrelated to this PR, but worth noting for a future fix.

Suggested fix
-#/usr/bin/env bash
+#!/usr/bin/env bash
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@completions/bun.bash` at line 1, The shebang line at the top of
completions/bun.bash is missing the exclamation mark; update the first line from
"#/usr/bin/env bash" to the correct shebang "#!/usr/bin/env bash" so the script
is executed by the system's bash interpreter when run directly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@completions/bun.bash`:
- Line 1: The shebang line at the top of completions/bun.bash is missing the
exclamation mark; update the first line from "#/usr/bin/env bash" to the correct
shebang "#!/usr/bin/env bash" so the script is executed by the system's bash
interpreter when run directly.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: ec064ca4-a58d-443d-8bf5-c1f127e1c7f2

📥 Commits

Reviewing files that changed from the base of the PR and between 42c00dc8ae7a8e7bf3c3b3f1da62107f6bb79316 and 3756d6e1c6d06f1ced816793b6595e31772ba911.

📒 Files selected for processing (1)
  • completions/bun.bash

…rd_script

`re_comp_word_script` is referenced on line 60 of `completions/bun.bash`
but was never defined anywhere in the script (since the original PR oven-sh#403
in 2022). This causes `[[ ... =~ ${re_comp_word_script} ]]` to fail with:

  -bash: [[: invalid regular expression: empty (sub)expression

every time tab completion triggers `_read_scripts_in_package_json`
(e.g. `bun run <TAB>` or `bun <TAB>` in a directory with a package.json).

Rather than guarding the undefined variable with `-n`, this removes the
dead branch entirely. Since `re_comp_word_script` was never defined, the
condition always evaluated to false — the branch was unreachable dead
code. The first branch already correctly handles filtering scripts from
completions once a script has been selected as the previous word.
@JakubPecenka
JakubPecenka force-pushed the fix-bash-completion-empty-regex branch from 3756d6e to 3e5d275 Compare April 1, 2026 17:44
@JakubPecenka JakubPecenka changed the title fix(completions): guard against undefined variable in bash completion fix(completions): remove dead branch referencing undefined variable in bash completion Apr 1, 2026
@robobun

robobun commented Aug 13, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the fix, and sorry it sat this long. #26743, opened in February, removes the same dead =~ ${re_comp_word_script} arm, so we are keeping that one as the fix for #24847 and closing this as a duplicate. This branch also conflicts with main now that a8d0692 (#33072) rewrote the body of the block, while #26743 still merges cleanly. Details of the comparison are in #26743 (comment).

@robobun robobun closed this Aug 13, 2026
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