From c1f9b0e324c2c47d832e21e5086f09e000d77eb5 Mon Sep 17 00:00:00 2001 From: Christophe Besson Date: Wed, 7 Oct 2026 09:41:18 +0200 Subject: fix(node): prefer the bundled ffmpeg over PATH in the frozen build shutil.which never looks beside meshbay-node.exe, and node-runtime is appended to the user PATH, so any earlier ffmpeg.exe on PATH ran instead of the pinned copy. Log the resolved media tools at startup. Co-Authored-By: Claude Opus 5.5 --- packages/meshbay-node/src/meshbay_node/platform.py | 25 +++++++++++++-- packages/meshbay-node/tests/test_platform.py | 36 ++++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) (limited to 'packages/meshbay-node') diff --git a/packages/meshbay-node/src/meshbay_node/platform.py b/packages/meshbay-node/src/meshbay_node/platform.py index 2255036..88db83d 100644 --- a/packages/meshbay-node/src/meshbay_node/platform.py +++ b/packages/meshbay-node/src/meshbay_node/platform.py @@ -171,25 +171,46 @@ _ffmpeg_path: str = "ffmpeg" _ffprobe_path: str = "ffprobe" +def _resolve_media_tool(name: str) -> str | None: + """ + The copy shipped next to a frozen meshbay-node.exe first, then PATH. + + ``shutil.which`` alone never looks in the executable's own directory: it + tries the current directory, then PATH, where the installer appends + node-runtime to the *user* PATH, which Windows places after the system one. + So any ffmpeg.exe earlier on PATH, from whatever application put it there, + ran instead of the pinned, checksummed copy we ship. + A name with a directory in it is the user's explicit choice and is left alone. + """ + if getattr(sys, "frozen", False) and os.path.basename(name) == name: + exe = name if name.lower().endswith(".exe") or sys.platform != "win32" \ + else f"{name}.exe" + bundled = Path(sys.executable).parent / exe + if bundled.is_file(): + return str(bundled) + return shutil.which(name) + + def check_media_tools( ffmpeg: str = "ffmpeg", ffprobe: str = "ffprobe", ) -> None: """Resolve ffmpeg/ffprobe at daemon startup. Raises RuntimeError if not found.""" global _ffmpeg_path, _ffprobe_path - resolved = shutil.which(ffmpeg) + resolved = _resolve_media_tool(ffmpeg) if not resolved: raise RuntimeError( f"{ffmpeg!r} not found in PATH. " "Install ffmpeg or set [node] ffmpeg_path in node.toml." ) _ffmpeg_path = resolved - resolved = shutil.which(ffprobe) + resolved = _resolve_media_tool(ffprobe) if not resolved: raise RuntimeError( f"{ffprobe!r} not found in PATH. " "Install ffmpeg or set [node] ffprobe_path in node.toml." ) _ffprobe_path = resolved + log.info("media tools: ffmpeg=%s ffprobe=%s", _ffmpeg_path, _ffprobe_path) def ffmpeg_cmd() -> str: diff --git a/packages/meshbay-node/tests/test_platform.py b/packages/meshbay-node/tests/test_platform.py index 8856962..4805d8a 100644 --- a/packages/meshbay-node/tests/test_platform.py +++ b/packages/meshbay-node/tests/test_platform.py @@ -83,6 +83,42 @@ def test_check_media_tools_stores_the_resolved_paths(monkeypatch): assert plat.ffprobe_cmd() == "/opt/bin/ffprobe.exe" + +def _frozen_with_bundled_tools(monkeypatch, tmp_path, platform="win32"): + suffix = ".exe" if platform == "win32" else "" + for tool in ("ffmpeg", "ffprobe"): + (tmp_path / f"{tool}{suffix}").write_bytes(b"") + monkeypatch.setattr(sys, "frozen", True, raising=False) + monkeypatch.setattr(sys, "executable", str(tmp_path / f"meshbay-node{suffix}")) + monkeypatch.setattr(sys, "platform", platform) + monkeypatch.setattr(plat.shutil, "which", lambda n: f"/elsewhere/{n}.exe") + + +@pytest.mark.parametrize("platform", ["win32", "linux"]) +def test_a_frozen_node_prefers_the_ffmpeg_shipped_beside_it_over_path( + monkeypatch, tmp_path, platform): + _frozen_with_bundled_tools(monkeypatch, tmp_path, platform) + plat.check_media_tools("ffmpeg", "ffprobe") + suffix = ".exe" if platform == "win32" else "" + assert plat.ffmpeg_cmd() == str(tmp_path / f"ffmpeg{suffix}") + assert plat.ffprobe_cmd() == str(tmp_path / f"ffprobe{suffix}") + + +def test_a_frozen_node_keeps_an_explicit_tool_path(monkeypatch, tmp_path): + _frozen_with_bundled_tools(monkeypatch, tmp_path) + plat.check_media_tools("C:/tools/ffmpeg.exe", "ffprobe") + assert plat.ffmpeg_cmd().startswith("/elsewhere/") + assert plat.ffprobe_cmd() == str(tmp_path / "ffprobe.exe") + + +def test_a_frozen_node_without_bundled_tools_falls_back_to_path(monkeypatch, tmp_path): + monkeypatch.setattr(sys, "frozen", True, raising=False) + monkeypatch.setattr(sys, "executable", str(tmp_path / "meshbay-node.exe")) + monkeypatch.setattr(plat.shutil, "which", lambda n: f"/opt/bin/{n}.exe") + plat.check_media_tools("ffmpeg", "ffprobe") + assert plat.ffmpeg_cmd() == "/opt/bin/ffmpeg.exe" + + # ── Event loop ─────────────────────────────────────────────────────────────── def test_configure_event_loop_is_a_noop_off_windows(monkeypatch): -- cgit v1.2.3