Skip to content

fix(binary-installer): correct grep -q usage to avoid duplicate PATH / duplicate PATH entries#13410

Open
gaurav0909-max wants to merge 2 commits intoserverless:mainfrom
gaurav0909-max:fix-binary-installer-path-check
Open

fix(binary-installer): correct grep -q usage to avoid duplicate PATH / duplicate PATH entries#13410
gaurav0909-max wants to merge 2 commits intoserverless:mainfrom
gaurav0909-max:fix-binary-installer-path-check

Conversation

@gaurav0909-max
Copy link

@gaurav0909-max gaurav0909-max commented Mar 14, 2026

Problem

The installer script incorrectly checks for .serverless/bin
in shell configuration files using command substitution with grep -q.

grep -q returns an exit status but produces no output, so using
command substitution ($() or backticks) always results in an empty
string. This causes the condition to always evaluate true and results
in duplicate PATH entries being appended on repeated installs.

Solution

Check the exit status of grep -q directly instead of using command
substitution. Also added proper quoting for $SHELL_CONFIG.

Result

The installer now correctly avoids adding duplicate .serverless/bin
entries to shell configuration files.

Fixes #13394

Summary by CodeRabbit

  • Refactor
    • Improved shell script syntax and quoting for grep and path variables to prevent word-splitting/globbing and ensure consistent behavior across zsh, bash variants, and fish.
    • Normalized conditional/test expressions and removed a duplicated nested check, simplifying logic while preserving installer behavior.

@Mmarzex
Copy link
Contributor

Mmarzex commented Mar 14, 2026

Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues
Code Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@github-actions
Copy link

github-actions bot commented Mar 14, 2026

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 14, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ebc48335-fa01-4fb6-a175-9b93db29328c

📥 Commits

Reviewing files that changed from the base of the PR and between 218c280 and 031225c.

📒 Files selected for processing (1)
  • binary-installer/install.sh
🚧 Files skipped from review as they are similar to previous changes (1)
  • binary-installer/install.sh

📝 Walkthrough

Walkthrough

Replaces command-substitution checks using grep -q with direct grep exit-status tests, adds quoting around $SHELL_CONFIG in grep invocations, and removes a duplicated nested if in binary-installer/install.sh to ensure idempotent PATH insertion.

Changes

Cohort / File(s) Summary
Shell Script Installation Logic
binary-installer/install.sh
Replaces $(grep -q ...) patterns with ! grep -q "..." exit-status checks; quotes $SHELL_CONFIG in all grep calls; simplifies duplicated nested if [[ -r $SHELL_CONFIG ]] into a single condition; retains PATH append behavior for zsh/bash/fish with corrected conditionals.

Estimated Code Review Effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐇 I hopped through lines of shell and cheer,
Found backticks that caused duplicate fear.
I swapped to direct grep, quoted the trail,
Now PATHs stay tidy — no repeating tale. ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main fix: correcting grep -q usage to prevent duplicate PATH entries in the binary-installer script.
Linked Issues check ✅ Passed The PR addresses all key requirements from issue #13394: fixing grep -q command substitution misuse, preventing duplicate PATH entries, making the installer idempotent, and adding proper quoting for shell configuration paths.
Out of Scope Changes check ✅ Passed All changes are scope-limited to correcting grep usage and quoting in binary-installer/install.sh as required by issue #13394, with no unrelated modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
📝 Coding Plan
  • Generate coding plan for human review comments

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.

Tip

You can validate your CodeRabbit configuration file in your editor.

If your editor has YAML language server, you can enable auto-completion and validation by adding # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json at the top of your CodeRabbit configuration file.

@gaurav0909-max
Copy link
Author

I have read the CLA Document and I hereby sign the CLA

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

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

⚠️ Outside diff range comments (1)
binary-installer/install.sh (1)

72-90: ⚠️ Potential issue | 🟠 Major

Remove the duplicated if on line 72; it breaks fallback control flow.

The double condition (if [[ -r $SHELL_CONFIG ]]; then if [[ -r $SHELL_CONFIG ]]; then) causes the else on line 77 to bind to the inner if instead of the outer one. When .bash_profile is unreadable, the entire else block (lines 78–88) that handles fallback to .bash_login and .profile is skipped, breaking the intended cascade through shell startup files.

Proposed fix
  SHELL_CONFIG=$HOME/.bash_profile
- if [[ -r $SHELL_CONFIG ]]; then  if [[ -r $SHELL_CONFIG ]]; then
+ if [[ -r $SHELL_CONFIG ]]; then
 
     if ! grep -q '.serverless/bin' "$SHELL_CONFIG"; then
       add_to_path $SHELL_CONFIG
     fi
   else
     SHELL_CONFIG=$HOME/.bash_login
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@binary-installer/install.sh` around lines 72 - 90, Remove the duplicated
nested "if [[ -r $SHELL_CONFIG ]]" so the fallback else branch correctly pairs
with the outer test; locate the block using the SHELL_CONFIG variable and
add_to_path function references, delete the inner redundant if that starts the
duplicate check, and ensure the remaining if/else cascade checks
$HOME/.bash_profile then falls back to $HOME/.bash_login and $HOME/.profile
while calling add_to_path when a target file does not already contain
'.serverless/bin'.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@binary-installer/install.sh`:
- Around line 63-69: The script uses unquoted SHELL_CONFIG in assignments,
conditional tests and when calling add_to_path which can break on spaces or glob
chars; update all occurrences to quote the variable (e.g., use "SHELL_CONFIG" in
assignments, "[ ! -r "$SHELL_CONFIG" ]" and "grep -q '.serverless/bin'
"$SHELL_CONFIG"" tests, and call add_to_path "$SHELL_CONFIG") so every reference
to SHELL_CONFIG is wrapped in double quotes; ensure you update every use in the
if/else blocks and any other occurrences in the file (references: SHELL_CONFIG,
add_to_path, grep -q).

---

Outside diff comments:
In `@binary-installer/install.sh`:
- Around line 72-90: Remove the duplicated nested "if [[ -r $SHELL_CONFIG ]]" so
the fallback else branch correctly pairs with the outer test; locate the block
using the SHELL_CONFIG variable and add_to_path function references, delete the
inner redundant if that starts the duplicate check, and ensure the remaining
if/else cascade checks $HOME/.bash_profile then falls back to $HOME/.bash_login
and $HOME/.profile while calling add_to_path when a target file does not already
contain '.serverless/bin'.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 892ffadd-d22e-48b4-8c0e-572ca6c0453d

📥 Commits

Reviewing files that changed from the base of the PR and between f022a9b and 218c280.

📒 Files selected for processing (1)
  • binary-installer/install.sh

Comment on lines 63 to 69
if [ ! -r $SHELL_CONFIG ] || ! grep -q '.serverless/bin' "$SHELL_CONFIG"; then
add_to_path $SHELL_CONFIG
fi
else
SHELL_CONFIG=$HOME/.bashrc
if [ ! -r $SHELL_CONFIG ] || (! `grep -q '.serverless/bin' $SHELL_CONFIG`); then
if [ ! -r $SHELL_CONFIG ] || ! grep -q '.serverless/bin' "$SHELL_CONFIG"; then
add_to_path $SHELL_CONFIG
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify unquoted SHELL_CONFIG occurrences in file tests and add_to_path calls.
rg -nP '\[\s*! -r \$SHELL_CONFIG\s*\]|add_to_path \$SHELL_CONFIG' binary-installer/install.sh

Repository: serverless/serverless

Length of output: 495


🏁 Script executed:

# Check the file around the affected lines to understand context
cat -n binary-installer/install.sh | sed -n '55,95p'

Repository: serverless/serverless

Length of output: 1556


🏁 Script executed:

# Check the shebang and overall script compatibility
head -5 binary-installer/install.sh

Repository: serverless/serverless

Length of output: 136


🏁 Script executed:

# Find the add_to_path function definition to understand its parameter handling
rg -n 'add_to_path' binary-installer/install.sh -A 3 | head -30

Repository: serverless/serverless

Length of output: 737


Quote $SHELL_CONFIG in variable assignments, tests, and function calls.

Unquoted $SHELL_CONFIG can cause word-splitting or pathname expansion if the path contains spaces or special characters. This occurs at:

  • Lines 62, 67, 71, 78, 84: Variable assignments
  • Lines 63, 68, 85: Conditional tests
  • Lines 64, 69, 75, 81, 86: Function calls to add_to_path

Quote the assignments and use the variable quoted in all contexts for consistency and correctness.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@binary-installer/install.sh` around lines 63 - 69, The script uses unquoted
SHELL_CONFIG in assignments, conditional tests and when calling add_to_path
which can break on spaces or glob chars; update all occurrences to quote the
variable (e.g., use "SHELL_CONFIG" in assignments, "[ ! -r "$SHELL_CONFIG" ]"
and "grep -q '.serverless/bin' "$SHELL_CONFIG"" tests, and call add_to_path
"$SHELL_CONFIG") so every reference to SHELL_CONFIG is wrapped in double quotes;
ensure you update every use in the if/else blocks and any other occurrences in
the file (references: SHELL_CONFIG, add_to_path, grep -q).

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.

binary-installer/install.sh appends duplicate PATH entries due to grep -q command substitution

2 participants