Skip to content

GH#3931: fix pulse-wrapper.sh BASH_SOURCE undefined in zsh#3935

Merged
marcusquinn merged 2 commits intomainfrom
bugfix/bash-source-zsh-compat
Mar 9, 2026
Merged

GH#3931: fix pulse-wrapper.sh BASH_SOURCE undefined in zsh#3935
marcusquinn merged 2 commits intomainfrom
bugfix/bash-source-zsh-compat

Conversation

@marcusquinn
Copy link
Copy Markdown
Owner

@marcusquinn marcusquinn commented Mar 9, 2026

Summary

  • Replace BASH_SOURCE[0] with ${BASH_SOURCE[0]:-$0} for SCRIPT_DIR resolution — falls back to $0 when sourced from zsh (MCP shell)
  • Replace naive BASH_SOURCE[0] == $0 main guard with _pulse_is_sourced() function that uses ZSH_EVAL_CONTEXT in zsh — the naive check always evaluates true in zsh because $0 equals the sourced file path

Problem

When pulse-wrapper.sh is sourced from the MCP shell (zsh), BASH_SOURCE[0] is undefined, causing:

  • SCRIPT_DIR to resolve to the wrong directory
  • shared-constants.sh and worker-lifecycle-common.sh fail to source
  • 17 command not found errors for _validate_int, _sanitize_markdown, etc.
  • Degraded pulse functionality (exit code 0 but helper functions missing)

Verification

  • bash -n pulse-wrapper.sh — syntax OK
  • shellcheck --norc -S warning — clean (only pre-existing SC2034 warnings)
  • Tested _pulse_is_sourced() in all 4 scenarios: bash direct, bash sourced, zsh direct, zsh sourced — all correct

Closes #3931

Summary by CodeRabbit

  • Chores
    • Enhanced shell script reliability by improving portability across different shell environments.
    • Fixed script detection to properly handle both direct execution and sourcing scenarios.

…wrapper.sh (GH#3931)

BASH_SOURCE is a bash-specific variable undefined in zsh. When
pulse-wrapper.sh is sourced from the MCP shell (zsh), SCRIPT_DIR
resolved to the wrong path, causing shared-constants.sh and
worker-lifecycle-common.sh to fail to source. All functions depending
on _validate_int, _sanitize_markdown, etc. then failed with
'command not found'.

Changes:
- SCRIPT_DIR: use ${BASH_SOURCE[0]:-$0} fallback for zsh compat
- Main guard: replace simple BASH_SOURCE[0]==/bin/zsh check with
  _pulse_is_sourced() that uses ZSH_EVAL_CONTEXT in zsh, since
  zsh sets $0 to the sourced file path (making the naive check
  always true)

Closes #3931
@github-actions github-actions bot added the bug Auto-created from TODO.md tag label Mar 9, 2026
@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses critical compatibility issues within pulse-wrapper.sh when executed in a Zsh environment, specifically the MCP shell. The changes ensure that the script correctly resolves its own directory and accurately determines if it's being sourced or run directly, thereby preventing errors related to undefined variables and missing helper functions. This significantly improves the script's robustness and portability across different shell environments.

Highlights

  • Enhanced SCRIPT_DIR Resolution: Updated the SCRIPT_DIR variable assignment to use ${BASH_SOURCE[0]:-$0}, ensuring correct directory resolution in both Bash and Zsh environments by providing a fallback when BASH_SOURCE is undefined.
  • Portable Source Detection: Replaced the naive BASH_SOURCE[0] == $0 main guard with a new _pulse_is_sourced() function, which intelligently detects if the script is being sourced or executed directly, utilizing ZSH_EVAL_CONTEXT for Zsh compatibility.
Changelog
  • .agents/scripts/pulse-wrapper.sh
    • Modified the SCRIPT_DIR assignment to use a shell-portable expression for determining the script's directory.
    • Introduced a new function _pulse_is_sourced to reliably detect if the script is being sourced, accommodating both Bash and Zsh shell contexts.
    • Replaced the direct comparison "${BASH_SOURCE[0]}" == "${0}" with a call to the new _pulse_is_sourced function.
Activity
  • No human activity has been recorded for this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 9, 2026

Important

Review skipped

Auto reviews are limited based on label configuration.

🚫 Review skipped — only excluded labels are configured. (1)
  • no-review

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: ed5f5fa0-409c-409c-9c8c-19137b83209b

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Walkthrough

The PR fixes shell portability issues in pulse-wrapper.sh by introducing a fallback mechanism for BASH_SOURCE (undefined in zsh) and adding a portable source-detection helper. This ensures the script functions correctly when sourced from zsh environments without BASH_SOURCE support.

Changes

Cohort / File(s) Summary
Shell Portability Fixes
.agents/scripts/pulse-wrapper.sh
Adds ${BASH_SOURCE[0]:-$0} fallback pattern for SCRIPT_DIR computation and introduces _pulse_is_sourced helper function to detect sourcing across bash and zsh environments, replacing the previous direct BASH_SOURCE dependency.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

Poem

Bash and zsh now shake hands with glee 🤝
No BASH_SOURCE misery
Fallbacks cascade, sourcing flows right ✨
From any shell—morning, noon, or night
Portability achieved! 🚀

🚥 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 'GH#3931: fix pulse-wrapper.sh BASH_SOURCE undefined in zsh' directly and concisely describes the main change: fixing a bash-specific BASH_SOURCE issue in the pulse-wrapper.sh script when used in zsh environments.
Linked Issues check ✅ Passed All coding objectives from issue #3931 are met: SCRIPT_DIR now uses ${BASH_SOURCE[0]:-$0} fallback, _pulse_is_sourced() detects sourcing correctly in both bash and zsh via ZSH_EVAL_CONTEXT, and dependent files can source properly without BASH_SOURCE undefined errors.
Out of Scope Changes check ✅ Passed All changes in pulse-wrapper.sh are scoped to fixing the zsh BASH_SOURCE compatibility issue; no unrelated modifications to other files or extraneous logic are present.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ 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
  • Commit unit tests in branch bugfix/bash-source-zsh-compat

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.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 9, 2026

🔍 Code Quality Report

�[0;35m[MONITOR]�[0m Code Review Monitoring Report

�[0;34m[INFO]�[0m Latest Quality Status:
SonarCloud: 0 bugs, 0 vulnerabilities, 383 code smells

�[0;34m[INFO]�[0m Recent monitoring activity:
Mon Mar 9 00:16:49 UTC 2026: Code review monitoring started
Mon Mar 9 00:16:49 UTC 2026: SonarCloud - Bugs: 0, Vulnerabilities: 0, Code Smells: 383

📈 Current Quality Metrics

  • BUGS: 0
  • CODE SMELLS: 383
  • VULNERABILITIES: 0

Generated on: Mon Mar 9 00:16:51 UTC 2026


Generated by AI DevOps Framework Code Review Monitoring

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly addresses shell portability issues when sourcing pulse-wrapper.sh in zsh. The use of ${BASH_SOURCE[0]:-$0} is a good fix for resolving the script directory, and the new _pulse_is_sourced function is a robust way to detect whether the script is being executed directly or sourced across both bash and zsh. I have one minor suggestion to simplify the zsh context check for improved readability and robustness.

Note: Security Review did not run due to the size of the PR.

coderabbitai[bot]
coderabbitai bot previously requested changes Mar 9, 2026
Copy link
Copy Markdown
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

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In @.agents/scripts/pulse-wrapper.sh:
- Around line 45-49: Change the SCRIPT_DIR assignment fallback to safely handle
being sourced by replacing the final "|| exit" behavior with a sourcing-safe
pattern: after computing SCRIPT_DIR (the line that sets SCRIPT_DIR="$(cd
"$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"), use "return 2>/dev/null || exit"
so a failed directory resolution returns control when sourced and still exits
when executed; update the statement that currently uses "|| exit" to this
"return 2>/dev/null || exit" pattern to match the approach used in
config-helper.sh.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 534baf89-7e57-451b-9c62-2838bcfd2941

📥 Commits

Reviewing files that changed from the base of the PR and between 6f7ae01 and 02d7f9e.

📒 Files selected for processing (1)
  • .agents/scripts/pulse-wrapper.sh

- Use 'return 2>/dev/null || exit' for SCRIPT_DIR resolution to safely
  handle sourced context (matches config-helper.sh pattern)
- Simplify ZSH_EVAL_CONTEXT check using colon-padding idiom for cleaner
  single-pattern match across all list positions
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 9, 2026

🔍 Code Quality Report

�[0;35m[MONITOR]�[0m Code Review Monitoring Report

�[0;34m[INFO]�[0m Latest Quality Status:
SonarCloud: 0 bugs, 0 vulnerabilities, 383 code smells

�[0;34m[INFO]�[0m Recent monitoring activity:
Mon Mar 9 00:44:35 UTC 2026: Code review monitoring started
Mon Mar 9 00:44:36 UTC 2026: SonarCloud - Bugs: 0, Vulnerabilities: 0, Code Smells: 383

📈 Current Quality Metrics

  • BUGS: 0
  • CODE SMELLS: 383
  • VULNERABILITIES: 0

Generated on: Mon Mar 9 00:44:39 UTC 2026


Generated by AI DevOps Framework Code Review Monitoring

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Mar 9, 2026

@marcusquinn marcusquinn dismissed coderabbitai[bot]’s stale review March 9, 2026 02:07

Both suggestions addressed in commit 96d08c8: (1) SCRIPT_DIR uses 'return 2>/dev/null || exit' for safe sourced-context handling, (2) ZSH_EVAL_CONTEXT check simplified with colon-padding idiom.

@marcusquinn marcusquinn merged commit c95f555 into main Mar 9, 2026
18 checks passed
@marcusquinn marcusquinn deleted the bugfix/bash-source-zsh-compat branch March 9, 2026 04:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Auto-created from TODO.md tag needs-review-fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: pulse-wrapper.sh fails when sourced from zsh — BASH_SOURCE undefined

1 participant