fix: tolerate non-utf8 filenames in file discovery

This commit is contained in:
Dennis Soong 2026-04-20 17:09:01 +08:00
parent d990fa52ed
commit a7df9f0245
4 changed files with 75 additions and 5 deletions

View file

@ -991,11 +991,14 @@ class SlashCommandCompleter(Completer):
continue
try:
proc = subprocess.run(
cmd, capture_output=True, text=True, timeout=2,
cmd,
capture_output=True,
text=False,
timeout=2,
cwd=cwd,
)
if proc.returncode == 0 and proc.stdout.strip():
raw = proc.stdout.strip().split("\n")
raw = _decode_fs_lines(proc.stdout)
if proc.returncode == 0 and raw:
# Store relative paths
for p in raw[:5000]:
rel = os.path.relpath(p, cwd) if os.path.isabs(p) else p
@ -1324,6 +1327,14 @@ class SlashCommandAutoSuggest(AutoSuggest):
return None
def _decode_fs_lines(data: bytes) -> list[str]:
"""Decode subprocess file listings using filesystem semantics.
This preserves non-UTF-8 filenames via surrogateescape instead of raising.
"""
return [os.fsdecode(line) for line in data.splitlines() if line]
def _file_size_label(path: str) -> str:
"""Return a compact human-readable file size, or '' on error."""
try: