From 169c3272c7525073de75cd11f0ffc9cb297bff74 Mon Sep 17 00:00:00 2001 From: Jean-Pierre Martineau Date: Thu, 24 Sep 2026 21:37:38 -0400 Subject: [PATCH] The wildcard scope reaches the command, and an empty slot can't be scoped On Windows, Click expands any argument holding `*` into the files it matches, quoted or not, so `chat send --scope '*'` arrived as the working directory's file names. The entry point turns the expansion off; nothing MeshTerm takes is a glob. `channels scope N REGION` on an empty slot did nothing and exited 5, which tells a script the scope was set and read back empty. A write there is now a usage error; a read still exits 5, as `share` does. Co-Authored-By: Claude Opus 5.5 (1M context) --- meshterm/cli.py | 6 +++++- meshterm/tools/channels.py | 8 +++++++- tests/test_send_scope.py | 22 +++++++++++++++++++++- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/meshterm/cli.py b/meshterm/cli.py index 086ec3f..c275652 100644 --- a/meshterm/cli.py +++ b/meshterm/cli.py @@ -889,7 +889,11 @@ def main() -> None: run from a shell leaves its output on the screen anyway. """ try: - app() + # No wildcard expansion on Windows. Click expands an argument holding `*` into + # the files it matches — even one the shell passed through quoted, which it can't + # tell apart — so `chat send --scope '*'` arrived as the working directory's file + # names. No MeshTerm argument is a file glob, and `*` is the wildcard region. + app(windows_expand_args=False) except SystemExit as exit_request: if exit_request.code and _owns_its_console(): _wait_before_the_window_closes() diff --git a/meshterm/tools/channels.py b/meshterm/tools/channels.py index bee5587..ad3fee6 100644 --- a/meshterm/tools/channels.py +++ b/meshterm/tools/channels.py @@ -215,14 +215,20 @@ class ChannelsTool(Tool): device = await ctx.device() idx = int(params["index"]) slot = next((s for s in await read_channel_slots(device) if s.idx == idx), None) + region = params.get("region") if slot is None: + if region is not None or params.get("clear"): + # A write names a channel that isn't there: a bad argument, not an empty + # answer — exit 5 would tell a script the scope was set and read back empty. + import typer + + raise typer.BadParameter(f"slot {idx} is empty; there is no channel to scope") return ToolResult( summary={"index": idx, "scope": None}, report=(_scoped(idx, None, None, None, changed=False),), exit_code=exitcodes.NO_RESULT, ) store = ctx.region_store - region = params.get("region") changed = bool(params.get("clear")) or region is not None if params.get("clear"): store.set_channel_scope(slot.identity, None) diff --git a/tests/test_send_scope.py b/tests/test_send_scope.py index 2087a60..3787b1e 100644 --- a/tests/test_send_scope.py +++ b/tests/test_send_scope.py @@ -577,9 +577,15 @@ async def test_cli_channel_scope_reads_sets_and_clears(tmp_path: Path) -> None: def test_cli_channel_scope_refuses_bad_arguments(run) -> None: # noqa: ANN001 - """A bad name or a name with --clear is a usage error; an empty slot is exit 5 + a doc.""" + """A bad name, a name with --clear, or a write to an empty slot is a usage error. + + Reading an empty slot is exit 5 with its document, the way ``share`` answers one; a + write there did nothing, and exit 5 would tell a script it had. + """ assert run("channels", "scope", "0", "two words").exit_code == 2 assert run("channels", "scope", "0", "yul", "--clear").exit_code == 2 + assert run("channels", "scope", "0", "yul").exit_code == 2 + assert run("channels", "scope", "0", "--clear").exit_code == 2 result = run("--json", "channels", "scope", "0") assert result.exit_code == 5 assert json.loads(result.stdout) == {"channel": None, "scope": None} @@ -595,6 +601,20 @@ def test_cli_send_scope_is_a_channel_option_and_reports_its_scope(run) -> None: assert run("chat", "send", "hi", "--channel", "0", "--scope", "$x").exit_code == 2 +def test_the_entry_point_never_expands_a_wildcard(monkeypatch: pytest.MonkeyPatch) -> None: + """``--scope '*'`` reaches the command as ``*``, not as the directory's file names. + + Click expands globs in the arguments on Windows, even quoted ones; the console-script + entry point turns that off, since ``*`` is the wildcard region and nothing takes a glob. + """ + import meshterm.cli as cli + + seen: dict = {} + monkeypatch.setattr(cli, "app", lambda **kwargs: seen.update(kwargs)) + cli.main() + assert seen == {"windows_expand_args": False} + + # -- Device config teaches the default ---------------------------------------------------------