Skip to content

feat: add Windows GB18030 text encoding support and native cmd.exe process creation#131

Merged
shayne-snap merged 2 commits into
mainfrom
fix/windows-compat-20260525
May 25, 2026
Merged

feat: add Windows GB18030 text encoding support and native cmd.exe process creation#131
shayne-snap merged 2 commits into
mainfrom
fix/windows-compat-20260525

Conversation

@shayne-snap
Copy link
Copy Markdown
Contributor

Summary

This PR adds comprehensive Windows text-encoding support and fixes cmd.exe process creation for non-ASCII paths.

Key Changes

Platform-conditional process creation ()

  • New shell.Command(Spec) wrapper returns a platform-appropriate *exec.Cmd
  • Windows: uses SysProcAttr.CmdLine for KindCmd to bypass Go's quoting and handle paths with Chinese characters correctly
  • Non-Windows: delegates to exec.Command unchanged
  • Removed /s from cmd.exe args — it interfered with quoted paths containing non-ASCII characters

GB18030 text encoding round-trip (internal/tools/text_*.go)

  • New textEncoding type and constants
  • text_decode.go / text_decode_windows.go: on Windows with ANSI CP 936/54936, non-UTF8 file bytes are decoded from GB18030 on read and re-encoded on write
  • normalizeTextFileBytes / restoreTextFileBytes now preserve encoding metadata through the round-trip
  • decodeShellOutput consolidated to use the shared decodeTextBytes

File tool improvements

  • readFile: added directory check returning a stable not_file error (no OS-specific text)
  • grepFile: switched from bufio.Scanner to os.ReadFile + normalizeTextFileBytes to support encoding-normalized searching on Windows
  • All exec.Command(spec.Bin, spec.Args...) call sites updated to shell.Command(spec)

Testing

  • New Windows-specific tests: GB18030 decode, read_file, edit_file (preserving bytes), edit_file (matching visible text), grep fallback
  • New cmd.exe quoted Chinese directory test
  • New go-search fallback tests for empty/blank-line edge cases
  • New directory-read-error test
  • Updated test pattern in cmd/dev/main.go to include all new Windows tests

Review Notes

One minor area to watch: command_windows.go passes a CmdLine without argv[0] prefix — this is correct for cmd.exe (which parses flags directly) but should be documented for future maintainers.

…ocess creation

- Add shell.Command() wrapper for platform-conditional process creation
  - Windows: use SysProcAttr.CmdLine for cmd.exe to handle quoted paths
  - Non-Windows: delegate to exec.Command
- Replace /s flag with /c in cmd.exe args to fix quoted-path handling
- Add text encoding layer for Windows GB18030 (CP 936/54936) round-trip:
  - text_encoding.go: type and constants
  - text_decode.go / text_decode_windows.go: decode/encode functions
  - normalizeTextFileBytes/restoreTextFileBytes now preserve encoding
- Consolidate decodeShellOutput on Windows to use shared decodeTextBytes
- Update grepFile to normalize encoding (bufio.Scanner replaced with
  os.ReadFile + normalizeTextFileBytes)
- Add readFile directory check with stable not_file error
- Update all exec.Command(spec.Bin, spec.Args...) call sites to use
  shell.Command(spec) for consistency
- Add comprehensive tests: GB18030 read/edit/search, cmd.exe quoted
  Chinese paths, directory error, grep empty/blank-line edge cases
Add an explicit exit statement after the & { ... } wrapper so
PowerShell propagates the native command's exit code instead of
always returning 0.
@shayne-snap shayne-snap merged commit b83185a into main May 25, 2026
2 checks passed
@shayne-snap shayne-snap deleted the fix/windows-compat-20260525 branch May 25, 2026 09:12
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.

1 participant