fix: prevent output-language.md from being overwritten on startup

The initializeLlmOutputLanguage function was overwriting the file every
time the language differed from the system locale. This fix changes the
behavior to only create the file if it doesn't exist, preserving any
user modifications.

Resolves #2830

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
DennisYu07 2026-04-03 10:13:15 +08:00
parent de66ee198e
commit 3b05a027c4
3 changed files with 21 additions and 25 deletions

View file

@ -750,7 +750,7 @@ describe('languageCommand', () => {
expect(fs.writeFileSync).not.toHaveBeenCalled();
});
it('should overwrite existing file when output language setting differs', () => {
it('should NOT overwrite existing file even when output language setting differs', () => {
vi.mocked(fs.existsSync).mockReturnValue(true);
vi.mocked(fs.readFileSync).mockReturnValue(
`# Output language preference: English
@ -760,11 +760,8 @@ describe('languageCommand', () => {
initializeLlmOutputLanguage('Japanese');
expect(fs.writeFileSync).toHaveBeenCalledWith(
expect.stringContaining('output-language.md'),
expect.stringContaining('Japanese'),
'utf-8',
);
// Should NOT overwrite - user's existing file takes precedence
expect(fs.writeFileSync).not.toHaveBeenCalled();
});
it('should resolve auto setting to detected system language', () => {

View file

@ -309,12 +309,12 @@ describe('languageUtils', () => {
);
});
it('should NOT overwrite file when content matches resolved language', () => {
it('should NOT overwrite file when it already exists with valid content', () => {
vi.mocked(fs.existsSync).mockReturnValue(true);
vi.mocked(i18n.detectSystemLanguage).mockReturnValue('en');
vi.mocked(fs.readFileSync).mockReturnValue(
`# Output language preference: English
<!-- qwen-code:llm-output-language: English -->
`# Output language preference: French
<!-- qwen-code:llm-output-language: French -->
`,
);
@ -323,21 +323,18 @@ describe('languageUtils', () => {
expect(fs.writeFileSync).not.toHaveBeenCalled();
});
it('should overwrite file when language setting differs', () => {
it('should NOT overwrite file even when setting differs from existing content', () => {
vi.mocked(fs.existsSync).mockReturnValue(true);
vi.mocked(fs.readFileSync).mockReturnValue(
`# Output language preference: English
<!-- qwen-code:llm-output-language: English -->
`# Output language preference: French
<!-- qwen-code:llm-output-language: French -->
`,
);
initializeLlmOutputLanguage('Japanese');
expect(fs.writeFileSync).toHaveBeenCalledWith(
expect.stringContaining('output-language.md'),
expect.stringContaining('Japanese'),
'utf-8',
);
// Should NOT overwrite - user's existing file takes precedence
expect(fs.writeFileSync).not.toHaveBeenCalled();
});
it('should resolve "auto" to detected system language', () => {

View file

@ -175,17 +175,19 @@ export function updateOutputLanguageFile(settingValue: string): void {
* @param outputLanguage - The output language setting value (e.g., 'auto', 'Chinese', etc.)
*
* Behavior:
* - Resolves the setting value ('auto' -> detected system language, or use as-is)
* - Ensures the rule file matches the resolved language
* - Creates the file if it doesn't exist
* - If the rule file already exists and contains a valid language setting, do nothing (preserve user modifications)
* - If the rule file doesn't exist, create it with the resolved language ('auto' -> detected system language, or use as-is)
*/
export function initializeLlmOutputLanguage(outputLanguage?: string): void {
// Resolve 'auto' or undefined to the detected system language
const resolved = resolveOutputLanguage(outputLanguage);
// Check if the file already exists and has valid content
const currentFileLanguage = readOutputLanguageFromFile();
// Only write if the file doesn't match the resolved language
if (currentFileLanguage !== resolved) {
writeOutputLanguageFile(resolved);
// If file exists with valid language, preserve user's setting - do nothing
if (currentFileLanguage) {
return;
}
// File doesn't exist or has invalid content, create it with resolved language
const resolved = resolveOutputLanguage(outputLanguage);
writeOutputLanguageFile(resolved);
}