diff options
| author | Christophe Besson <cbesson@gmail.com> | 2026-09-04 02:35:41 +0200 |
|---|---|---|
| committer | Christophe Besson <cbesson@gmail.com> | 2026-09-04 02:35:41 +0200 |
| commit | c2620a5b269db75fcadb772e3ae5250886e8814c (patch) | |
| tree | 0920ec826c222bb29978cff9440779ade1fbbc45 | |
| parent | d11e571c5b6c24b586ef5b8fb2cfcf6a6bfa6d6d (diff) | |
| download | meshbay-c2620a5b269db75fcadb772e3ae5250886e8814c.tar.gz | |
fix(node): make init, node.toml editing and CLI output work on Windows
Found by running the daemon on Windows for the first time:
- `meshbay-node init` wrote `unlock_file = "C:\Users\..."`, and
attach_group / add_root write `path = "C:\..."` — a raw Windows path in a
TOML basic string is a parse error (`\U`, `\a`, ... are escape sequences),
so the config would not load. All now write `Path(...).as_posix()`;
pathlib reads the forward-slash form fine on Windows.
- any `print()` carrying a `->` arrow or em dash (the CLI help and messages
are full of them) raised UnicodeEncodeError on a cp1252 console and took
the command down. New `platform.force_utf8_stdio()` reconfigures
stdout/stderr to UTF-8, called at the top of `main()`.
Verified on Windows: init writes parseable LF node.toml, the keystore
Argon2-decrypts, the loopback control API binds 127.0.0.1, and
`_update_node_toml` reads a CRLF file and rewrites it LF-only with its
standalone comments intact. Two regression tests added in test_ops.py.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
| -rw-r--r-- | packages/meshbay-node/src/meshbay_node/daemon.py | 7 | ||||
| -rw-r--r-- | packages/meshbay-node/src/meshbay_node/ops.py | 8 | ||||
| -rw-r--r-- | packages/meshbay-node/src/meshbay_node/platform.py | 18 | ||||
| -rw-r--r-- | packages/meshbay-node/tests/test_ops.py | 42 |
4 files changed, 68 insertions, 7 deletions
diff --git a/packages/meshbay-node/src/meshbay_node/daemon.py b/packages/meshbay-node/src/meshbay_node/daemon.py index 72f7fa4..6bc3bef 100644 --- a/packages/meshbay-node/src/meshbay_node/daemon.py +++ b/packages/meshbay-node/src/meshbay_node/daemon.py @@ -1634,7 +1634,8 @@ def _systemctl_user(verb: str, unit: str, *, not_running_hint: str, def main() -> None: import argparse - from meshbay_node.platform import use_compatible_event_loop + from meshbay_node.platform import force_utf8_stdio, use_compatible_event_loop + force_utf8_stdio() use_compatible_event_loop() parser = argparse.ArgumentParser(description="MeshBay Node daemon") @@ -1738,7 +1739,9 @@ def main() -> None: "ui_port = 18000", "", "[keystore]", - f'unlock_file = "{unlock_file}"', + # Forward slashes: a Windows path in a TOML basic string is a + # parse error (`\U`, `\a`, ... are escape sequences). + f'unlock_file = "{unlock_file.as_posix()}"', "", ] cfg_path.write_text("\n".join(toml_lines) + "\n", encoding="utf-8", newline="\n") diff --git a/packages/meshbay-node/src/meshbay_node/ops.py b/packages/meshbay-node/src/meshbay_node/ops.py index 40de755..4c20c2a 100644 --- a/packages/meshbay-node/src/meshbay_node/ops.py +++ b/packages/meshbay-node/src/meshbay_node/ops.py @@ -447,9 +447,11 @@ async def attach_group(state: dict, name: str, shared_dir: str, upload_path.mkdir(parents=True, exist_ok=True) except OSError as e: raise OpError(f"Cannot create {upload_path}: {e}") from e - block += f'upload_dir = "{upload_path}"\n' + block += f'upload_dir = "{upload_path.as_posix()}"\n' block += (f'\n [[groups.roots]]\n' - f' path = "{path}"\n') + # Forward slashes: a Windows path in a TOML basic string is a + # parse error (`\U`, `\a`, ... are escapes). pathlib reads `/`. + f' path = "{path.as_posix()}"\n') if not separate_upload: block += f' upload = true\n' try: @@ -667,7 +669,7 @@ async def add_root(state: dict, group_id: str, path: str, *, raise OpError(f"Cannot create {added.path}: {e}") from e conf_path = Path(state.get("config_path") or DEFAULT_CONFIG_PATH) - root_block = f' [[groups.roots]]\n path = "{added.path}"' + root_block = f' [[groups.roots]]\n path = "{added.path.as_posix()}"' if name: root_block += f'\n name = "{added.name}"' if kind != "generic": diff --git a/packages/meshbay-node/src/meshbay_node/platform.py b/packages/meshbay-node/src/meshbay_node/platform.py index 4bd8e35..80aa6ae 100644 --- a/packages/meshbay-node/src/meshbay_node/platform.py +++ b/packages/meshbay-node/src/meshbay_node/platform.py @@ -6,6 +6,24 @@ import shutil import sys from pathlib import Path +# ── Console ────────────────────────────────────────────────────────────────── + + +def force_utf8_stdio() -> None: + """ + Make stdout/stderr UTF-8. A Windows console is cp1252 by default, so any + ``print()`` carrying a character outside it — the ``->`` arrows and em + dashes the CLI help and messages are full of — raises UnicodeEncodeError + and takes the command down with it. No effect where the streams are + already UTF-8 or cannot be reconfigured. + """ + for stream in (sys.stdout, sys.stderr): + try: + stream.reconfigure(encoding="utf-8") + except (AttributeError, ValueError, OSError): + pass + + # ── Event loop ─────────────────────────────────────────────────────────────── diff --git a/packages/meshbay-node/tests/test_ops.py b/packages/meshbay-node/tests/test_ops.py index 83758ae..92e32bf 100644 --- a/packages/meshbay-node/tests/test_ops.py +++ b/packages/meshbay-node/tests/test_ops.py @@ -15,12 +15,11 @@ from pathlib import Path import pytest from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey - -from conftest import one_root from meshbay_node import ops from meshbay_node.indexer.group_index import GroupIndex from meshbay_node.transport.quic_server import Denylist +from conftest import one_root def _state(tmp_path: Path) -> dict: @@ -251,3 +250,42 @@ async def test_start_reload_without_fn_is_refused(tmp_path): state = _state(tmp_path) with pytest.raises(ops.OpError, match="Reload not available"): await ops.start_reload(state) + + +# ── node.toml editing survives Windows (LF, comments, backslash paths) ──────── + +def test_update_node_toml_forces_lf_and_keeps_standalone_comments(tmp_path): + p = tmp_path / "node.toml" + p.write_bytes("\r\n".join([ + "[hub]", 'url = "http://x"', "", + "[node]", + "# how long an invitation lives", + "invite_ttl_hours = 168", + "ui_port = 18000", + "", + "[[groups]]", 'id = "g1"', + ]).encode()) + + ops._update_node_toml(p, {"invite_ttl_hours": 24, "max_concurrent_streams": 4}) + + raw = p.read_bytes() + assert b"\r\n" not in raw, "must be rewritten LF-only, whatever it was read as" + text = raw.decode("utf-8") + assert "# how long an invitation lives" in text + import tomllib + node = tomllib.loads(text)["node"] + assert node["invite_ttl_hours"] == 24 + assert node["max_concurrent_streams"] == 4 + + +def test_a_backslash_path_written_into_node_toml_stays_parseable(): + # attach_group / add_root / init embed a directory into a TOML basic string. + # A raw Windows path there (drive + backslash + "Users" + ...) is a parse + # error since backslash sequences are escapes; the code writes as_posix(). + import tomllib + bs = chr(92) + win_dir = f"C:{bs}Users{bs}alice{bs}Media" + assert tomllib.loads(f'path = "{Path(win_dir).as_posix()}"\n')["path"] == \ + "C:/Users/alice/Media" + with pytest.raises(tomllib.TOMLDecodeError): + tomllib.loads(f'path = "{win_dir}"\n') # the bug this guards against |