Skip to content

[Bug]: Windows exec tool produces garbled Chinese characters due to hardcoded UTF-8 encoding #50519

@zyj19970716-art

Description

@zyj19970716-art

Bug type

Behavior bug (incorrect output/state without crash)

Summary

The runExec method in OpenClaw's exec tool hardcodes encoding: "utf8" when executing commands on Windows. This causes Chinese characters (and other non-ASCII characters) to become garbled (乱码) because Windows cmd.exe outputs in GBK/CP936 encoding by default, not UTF-8.

Steps to reproduce

  1. Use OpenClaw's exec tool on Windows
  2. Execute any command that outputs Chinese characters:
    await exec("dir", ["D:\\测试文件夹"]);
    // or
    await exec("echo", ["你好世界"]);
  3. Observe garbled output

Expected behavior

Chinese characters should display correctly.

Actual behavior

Chinese characters appear as garbled text (e.g., ���IJ��� instead of 测试).

OpenClaw version

2026.3.13

Operating system

windows10

Install method

npm dev

Model

qwen-3.5

Provider / routing chain

openclaw

Additional provider/model setup details

No response

Logs, screenshots, and evidence

Impact and severity

No response

Additional information

Proposed Solutions

Option 1: Auto-detect Windows encoding (Recommended)

async function runExec(command, args, opts = 1e4) {
	// Detect encoding based on platform
	const defaultEncoding = process.platform === "win32" ? "gbk" : "utf8";
	
	const options = typeof opts === "number" ? {
		timeout: opts,
		encoding: defaultEncoding
	} : {
		timeout: opts.timeoutMs,
		maxBuffer: opts.maxBuffer,
		cwd: opts.cwd,
		encoding: opts.encoding ?? defaultEncoding  // Allow override
	};
	// ...
}

Option 2: Set UTF-8 code page before execution

async function runExec(command, args, opts = 1e4) {
	// On Windows, prepend chcp 65001 to set UTF-8 code page
	if (process.platform === "win32" && isWindowsBatchCommand(execCommand)) {
		const cmdLine = `chcp 65001>nul && ${buildCmdExeCommandLine(execCommand, execArgs)}`;
		// Execute with UTF-8 code page
	}
	// ...
}

Option 3: Allow encoding configuration

async function runExec(command, args, opts = 1e4) {
	const options = typeof opts === "number" ? {
		timeout: opts,
		encoding: "utf8"
	} : {
		timeout: opts.timeoutMs,
		maxBuffer: opts.maxBuffer,
		cwd: opts.cwd,
		encoding: opts.encoding ?? "utf8"  // Allow user to specify encoding
	};
	// ...
}

Impact

This issue affects:

  • All Windows users working with non-ASCII characters
  • Chinese, Japanese, Korean, and other non-Latin script users
  • File paths with non-ASCII characters
  • Command output containing non-ASCII text

Workaround

Currently, users must:

  1. Avoid using exec tool for commands with Chinese characters
  2. Use alternative tools (list_files, read_file, etc.)
  3. Use PowerShell with JSON output: powershell -Command "Get-ChildItem | ConvertTo-Json"

Additional Context

This is a common issue with Node.js child_process on Windows. The encoding option in execFile only affects how Node.js interprets the output buffer, but doesn't change the actual encoding used by cmd.exe.

Related Node.js documentation:

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingbug:behaviorIncorrect behavior without a crash

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions