Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(scripts): GLOSSARY.md 同步校验忽略行尾差异 (#717)

zhanghengxdf979a3343c0d
diff --git a/scripts/__tests__/glossary-rules.test.mjs b/scripts/__tests__/glossary-rules.test.mjs
index 6edf71f93b..bdd6e11e91 100644
--- a/scripts/__tests__/glossary-rules.test.mjs
+++ b/scripts/__tests__/glossary-rules.test.mjs
@@ -29,7 +29,7 @@ import {
makeSourceTermMatcher,
} from '../shared/glossary-rules.mjs';
import { validateAgainstSchema } from '../shared/json-schema-lite.mjs';
-import { renderGlossaryDoc } from '../shared/glossary-doc.mjs';
+import { normalizeDocEol, renderGlossaryDoc } from '../shared/glossary-doc.mjs';
const ROOT = path.resolve(fileURLToPath(new URL('../..', import.meta.url)));
@@ -342,12 +342,26 @@ test('glossary.json: locales 与 desktop SUPPORTED_LOCALES 一致', () => {
test('GLOSSARY.md 与 glossary.json 同步', () => {
const doc = fs.readFileSync(path.join(ROOT, 'i18n', 'GLOSSARY.md'), 'utf8');
assert.equal(
- doc,
- renderGlossaryDoc(glossary),
+ normalizeDocEol(doc),
+ normalizeDocEol(renderGlossaryDoc(glossary)),
'i18n/GLOSSARY.md 已过期,运行 pnpm i18n:glossary-doc 重新生成',
);
});
+// 契约:同步校验只看内容,不看行尾。autocrlf=true 的 Windows checkout 会把
+// GLOSSARY.md 转成 CRLF,而渲染结果恒为 LF;若比较不归一化,门禁在 Windows 上
+// 必红且无法自愈(重新生成写 LF,下次 checkout 又变 CRLF)。
+test('GLOSSARY.md 同步校验对 CRLF 检出不误报', () => {
+ const rendered = renderGlossaryDoc(glossary);
+ const asCrlf = rendered.replace(/\n/g, '\r\n');
+ assert.notEqual(asCrlf, rendered, '前置条件:CRLF 版本应与 LF 版本逐字符不同');
+ assert.equal(
+ normalizeDocEol(asCrlf),
+ normalizeDocEol(rendered),
+ 'CRLF 检出必须被判为同步——比较前要归一化行尾',
+ );
+});
+
// ---------------------------------------------------------------------------
// baseline 完整性
// ---------------------------------------------------------------------------
diff --git a/scripts/check-i18n-glossary.mjs b/scripts/check-i18n-glossary.mjs
index d00d8a59a5..18feb3bf5c 100644
--- a/scripts/check-i18n-glossary.mjs
+++ b/scripts/check-i18n-glossary.mjs
@@ -40,7 +40,7 @@ import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
-import { renderGlossaryDoc } from './shared/glossary-doc.mjs';
+import { normalizeDocEol, renderGlossaryDoc } from './shared/glossary-doc.mjs';
import { validateAgainstSchema } from './shared/json-schema-lite.mjs';
import {
ELLIPSIS_LOCALES,
@@ -504,7 +504,7 @@ if (stale.length > 0) {
// GLOSSARY.md 是给人和 AI 查阅的入口,过期比不存在更糟——大家会照着过期的表写文案。
// 用与生成器完全相同的渲染函数比对,不做「差不多就行」的模糊校验。
const docStale = fs.existsSync(DOC_PATH)
- ? fs.readFileSync(DOC_PATH, 'utf8') !== renderGlossaryDoc(glossary)
+ ? normalizeDocEol(fs.readFileSync(DOC_PATH, 'utf8')) !== normalizeDocEol(renderGlossaryDoc(glossary))
: true;
if (docStale) {
console.error(
diff --git a/scripts/generate-glossary-doc.mjs b/scripts/generate-glossary-doc.mjs
index bc76c455f1..87a67354b4 100644
--- a/scripts/generate-glossary-doc.mjs
+++ b/scripts/generate-glossary-doc.mjs
@@ -14,7 +14,7 @@ import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
-import { renderGlossaryDoc } from './shared/glossary-doc.mjs';
+import { normalizeDocEol, renderGlossaryDoc } from './shared/glossary-doc.mjs';
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const GLOSSARY_PATH = path.join(repoRoot, 'i18n', 'glossary.json');
@@ -27,7 +27,7 @@ const rendered = renderGlossaryDoc(glossary);
if (checkOnly) {
const existing = fs.existsSync(DOC_PATH) ? fs.readFileSync(DOC_PATH, 'utf8') : '';
- if (existing !== rendered) {
+ if (normalizeDocEol(existing) !== normalizeDocEol(rendered)) {
console.error(
'[generate-glossary-doc] ❌ i18n/GLOSSARY.md 与 i18n/glossary.json 不同步。\n' +
' 运行 pnpm i18n:glossary-doc 重新生成。',
diff --git a/scripts/shared/glossary-doc.mjs b/scripts/shared/glossary-doc.mjs
index fc4c531ef1..a38665a008 100644
--- a/scripts/shared/glossary-doc.mjs
+++ b/scripts/shared/glossary-doc.mjs
@@ -40,6 +40,23 @@ function forbiddenLabel(entry) {
return `${entry.text}(仅当英文含 ${entry.whenEn})`;
}
+/**
+ * 归一化行尾,供「已落盘的 GLOSSARY.md」与「渲染结果」比较时使用。
+ *
+ * renderGlossaryDoc 恒以 LF 收尾(见函数末尾的 join('\n')),但 .gitattributes
+ * 未给 *.md 固定 eol——`core.autocrlf=true` 的 Windows checkout(Git for Windows
+ * 安装时的默认选项)会把 GLOSSARY.md 转成 CRLF。此时逐字符比较会假报「文档过期」,
+ * 而且无法自愈:重新生成写出的是 LF,下次 checkout 又被转回 CRLF,门禁永远红。
+ *
+ * 与 apps/desktop/scripts/help-kb-guard.mjs 的 norm() 同款处理。放在共享模块里
+ * 是为了让三处比较点(本文件的消费方 generate-glossary-doc.mjs --check、
+ * check-i18n-glossary.mjs、glossary-rules.test.mjs)用同一份逻辑,不会出现某处
+ * 漏加归一化又把 Windows 拦回去。
+ */
+export function normalizeDocEol(text) {
+ return text.replace(/\r\n/g, '\n');
+}
+
export function renderGlossaryDoc(glossary) {
const terms = sortTerms(glossary.terms);
const decided = terms.filter((t) => t.status === 'decided');