Skip to content

fix: use shutil.which so play() and stream() work on Windows - #163

Merged
lumpinif merged 3 commits into
fishaudio:mainfrom
Dhevenddra:fix-which-windows
Nov 24, 2026
Merged

lumpinif merged 3 commits into
fishaudio:mainfrom
Dhevenddra:fix-which-windows

Conversation

@Dhevenddra

@Dhevenddra Dhevenddra commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

Problem

_is_installed() in utils/play.py and utils/stream.py checks for a binary by shelling out to which:

try:
    subprocess.run(["which", command], capture_output=True, check=True)
    return True
except subprocess.CalledProcessError:
    return False

Windows has no which. Trying to run it raises FileNotFoundError, which is not a CalledProcessError, so it escapes the handler and propagates to the caller.

The result is that play() and stream() both fail on Windows regardless of whether ffplay or mpv is installed:

>>> from fishaudio import stream
>>> stream(iter([b"..."]))
  File "src/fishaudio/utils/stream.py", line 49, in stream
    if not _is_installed("mpv"):
FileNotFoundError: [WinError 2] The system cannot find the file specified

Reproduced on Windows 11 with Python 3.9.25, against main. Both helpers raise, and both play(b"audio") and stream(iter([b"audio"])) surface the WinError rather than the DependencyError they are supposed to raise.

That error message is the part that stings: it is the one telling Windows users where to get the tool.

raise DependencyError(
    "mpv",
    "brew install mpv  # macOS\n"
    "sudo apt install mpv  # Linux\n"
    "https://mpv.io/installation/  # Windows",   # unreachable on Windows
)

The same helper is duplicated verbatim in both modules, so both are affected.

Fix

Use shutil.which(), the standard-library equivalent. It works on every supported platform and does not spawn a process just to test for one.

def _is_installed(command: str) -> bool:
    """Check if a command is available in PATH."""
    return shutil.which(command) is not None

This also removes a latent failure on Linux and macOS: minimal container images often ship without which, and those would have hit the same unhandled FileNotFoundError.

Tests

The four existing tests mocked subprocess.run to simulate which succeeding or exiting non-zero. Neither is what Windows does, which is why CI stayed green: the workflow only runs ubuntu-latest, where which exists. They now patch shutil.which instead.

Added TestIsInstalled covering both modules:

  • a missing binary is reported absent rather than raising
  • the lookup does not shell out (subprocess.run is patched to fail the test if called)
  • with no which binary available and the tool absent, play() and stream() raise DependencyError as documented

Verified the tests actually catch this: with the two _is_installed bodies reverted and the tests left alone, 9 tests fail, including all 6 new ones, with the original FileNotFoundError.

poe lint, poe format, poe typecheck all pass. pytest tests/unit/ is 163 passed, up from 157.

Summary by CodeRabbit

  • Bug Fixes

    • Improved detection of playback and streaming tools required by the app.
    • Added clearer handling when required tools are unavailable, including on Windows.
    • Avoided unnecessary shell activity during command availability checks.
  • Tests

    • Expanded coverage for missing playback and streaming tools and platform-specific error handling.

_is_installed shelled out to `which`, which does not exist on Windows. The
resulting FileNotFoundError is not a CalledProcessError, so it escaped the
handler and propagated. On Windows both play() and stream() therefore died
with "[WinError 2] The system cannot find the file specified" instead of
running, or of raising the DependencyError whose message documents how to
install the missing tool on Windows.

shutil.which is the standard-library equivalent, works on every supported
platform, and avoids spawning a process just to test for one. The same helper
was duplicated in both modules, so both are updated.

The existing tests mocked subprocess.run, so they described `which` succeeding
or exiting non-zero, neither of which is what Windows does. They now patch
shutil.which, and cover the case where no `which` binary exists at all.
@coderabbitai

coderabbitai Bot commented Jul 31, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The play and stream utility modules now use shutil.which to check command availability. Unit tests mock the lookup and cover missing commands and Windows-like error handling.

Changes: Command Availability Check Migration

Layer / File(s) Summary
Playback and stream command detection
src/fishaudio/utils/play.py, src/fishaudio/utils/stream.py
Both _is_installed helpers use shutil.which instead of running which through subprocess and catching CalledProcessError.
Test updates for shutil.which mocking
tests/unit/test_utils.py
Tests mock shutil.which and check the playback invocation. Parameterized tests cover missing-command detection, lookups without shelling out, and DependencyError when subprocess.run raises FileNotFoundError.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: lumpinif

Merge Risk: Minimal · up to 2f91b

This change makes the playback and streaming dependency checks work on platforms without a which executable, such as Windows. It also keeps the documented missing-dependency guidance reachable. The tests cover the updated behavior and no merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the core change: replacing the subprocess-based which check with shutil.which so that play() and stream() can reach their DependencyError guidance on Windows. The raw summary…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the core change: replacing the subprocess-based which check with shutil.which so that play() and stream() can reach their DependencyError guidance on Windows. The raw summary and objectives confirm this is the main change in src/fishaudio/utils/play.py and src/fishaudio/utils/stream.py, with matching test updates. The title uses the fix: prefix and stays under one line.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f5de6439-b291-47fd-9c43-7b84ee942cd3
Commits

Reviewing files that changed from the base of the PR and between 7dbe656 and 2f91b70.

Files selected for processing: 2

  • src/fishaudio/utils/play.py
  • tests/unit/test_utils.py
Files skipped from review as they are similar to previous changes: 1

  • src/fishaudio/utils/play.py

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

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 current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/unit/test_utils.py`:
- Around line 52-53: Replace the two assertions checking mock_run.call_count and
the "ffplay" string presence with a single assert_called_once_with invocation on
mock_run. This will verify that the mock was called exactly once with the
expected ffplay command and subprocess options, eliminating both the risk of
multiple calls and false positives from "ffplay" appearing elsewhere in the
arguments.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: edeebe02-8b6b-437c-acd0-80a668c0c24e

📥 Commits

Reviewing files that changed from the base of the PR and between 999c59d and 7dbe656.

📒 Files selected for processing (3)
  • src/fishaudio/utils/play.py
  • src/fishaudio/utils/stream.py
  • tests/unit/test_utils.py

Comment thread tests/unit/test_utils.py Resolved
Checking call_count and a substring of the repr let an unrelated call through and would match 'ffplay' appearing anywhere in the arguments. Pin the call instead.
@Dhevenddra

Copy link
Copy Markdown
Contributor Author

Good call, that assertion was loose in both directions. Replaced it with assert_called_once_with pinning the exact ffplay argv, input bytes and subprocess options, so an extra call or an incidental ffplay substring can no longer pass.

@lumpinif lumpinif 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.

Thanks, this is a real bug and shutil.which is the right fix.

On main, _is_installed() shells out to which and only catches CalledProcessError. Where which does not exist (Windows, minimal containers) that raises FileNotFoundError, so users never see the intended DependencyError. The change is minimal and the tests now check the actual call instead of a loose string match.

CI has not run on this PR yet, so it still needs a maintainer to trigger it. It may also need a rebase now that #168 has landed.

Copilot AI lite review requested due to automatic review settings November 24, 2026 06:01
@codecov

codecov Bot commented Nov 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
src/fishaudio/utils/play.py 85.29% <100.00%> (-1.20%) ⬇️
src/fishaudio/utils/stream.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI 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.

🟢 Approval recommended

The review found no active issues in the cross-platform dependency-detection changes.

No open issues 🎉

What changed in this PR

This pull request fixes cross-platform detection of playback dependencies by replacing shell-based which calls with Python's platform-independent shutil.which(), allowing play() and stream() to raise the intended DependencyError on Windows and minimal Unix environments.

Changes:

  • Replaced subprocess-based command detection in both utility modules.
  • Updated tests to mock shutil.which() instead of subprocess.run.
  • Added coverage for missing commands and environments without a which executable.
File Description
tests/​unit/​test_utils.py Updates command-detection mocks and adds tests covering missing dependencies and cross-platform behavior.
src/​fishaudio/​utils/​stream.py Uses shutil.which() to detect whether mpv is available.
src/​fishaudio/​utils/​play.py Uses shutil.which() to detect whether ffplay is available.

Review effort: Lite

@lumpinif
lumpinif merged commit 8575af8 into fishaudio:main Nov 24, 2026
15 checks passed
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.

3 participants