feat(core): auto-detect UTF-8 BOM for PowerShell scripts on Windows

- Add needsUtf8Bom() to detect when UTF-8 BOM is needed based on file
  extension and system code page
- PowerShell 5.1 on non-UTF-8 Windows systems (e.g. GBK) requires BOM
  to read scripts correctly
- Remove default UTF8 encoding; undefined now triggers auto-detection
- Add tests for needsUtf8Bom() covering Windows/non-Windows scenarios

This ensures PowerShell scripts are written with UTF-8 BOM on systems
that need it, fixing character encoding issues for non-ASCII content.

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
tanzhenxin 2026-03-16 22:44:53 +08:00
parent 82e0064871
commit 17939baa66
6 changed files with 142 additions and 13 deletions

View file

@ -37,7 +37,6 @@ import {
type FileSystemService,
StandardFileSystemService,
type FileEncodingType,
FileEncoding,
} from '../services/fileSystemService.js';
import { GitService } from '../services/gitService.js';
@ -523,7 +522,7 @@ export class Config {
private readonly truncateToolOutputLines: number;
private readonly eventEmitter?: EventEmitter;
private readonly channel: string | undefined;
private readonly defaultFileEncoding: FileEncodingType;
private readonly defaultFileEncoding: FileEncodingType | undefined;
private readonly enableHooks: boolean;
private readonly hooks?: Record<string, unknown>;
private readonly hooksConfig?: Record<string, unknown>;
@ -641,7 +640,7 @@ export class Config {
this.truncateToolOutputLines =
params.truncateToolOutputLines ?? DEFAULT_TRUNCATE_TOOL_OUTPUT_LINES;
this.channel = params.channel;
this.defaultFileEncoding = params.defaultFileEncoding ?? FileEncoding.UTF8;
this.defaultFileEncoding = params.defaultFileEncoding;
this.storage = new Storage(this.targetDir);
this.inputFormat = params.inputFormat ?? InputFormat.TEXT;
this.fileExclusions = new FileExclusions(this);
@ -1647,7 +1646,7 @@ export class Config {
* Get the default file encoding for new files.
* @returns FileEncodingType
*/
getDefaultFileEncoding(): FileEncodingType {
getDefaultFileEncoding(): FileEncodingType | undefined {
return this.defaultFileEncoding;
}