Skip to content

fix(portfolio-import): 剥掉 CSV 表头 UTF-8 BOM - #415

Closed
wannT-1 wants to merge 1 commit into
helsome:mainfrom
wannT-1:fix/portfolio-import-bom
Closed

wannT-1 wants to merge 1 commit into
helsome:mainfrom
wannT-1:fix/portfolio-import-bom

Conversation

@wannT-1

@wannT-1 wannT-1 commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

改动说明

What

normalizeHeaderCell 在别名匹配前剥掉表头单元格开头的 UTF-8 BOM(U+FEFF)。英文表头(Symbol,Quantity,Cost)和中文表头(代码,数量,成本)各补一条回归:带 BOM 的 CSV 与同内容无 BOM 文件解析结果一致,symbol / quantity / cost 映射正确。

Why

Windows / Excel「CSV UTF-8」会在文件开头写入 BOM。BOM 留在第一列表头时,Symbol 或 代码 对不上 HEADER_ALIASES,symbol 列匹配失败。只改表头归一化,不改 UI。

Closes #407

关联 Issue

Closes #407

测试报告(正式审核前必填)

环境

  • Bun:1.4.2
  • OS:Linux 6.12.94+ x86_64

实际执行命令与结果

bun test packages/shared/src/portfolio-import
→ 103 pass / 0 fail
→ 270 expect() calls
→ Ran 103 tests across 4 files

bun run --filter @finagent/shared typecheck
→ @finagent/shared typecheck: Exited with code 0

已知失败 / Baseline(如有)

无失败。

  • 已提供实际测试命令与 pass/fail 结果
  • 已说明测试环境
  • 如果存在已知 baseline / 环境失败,已提供 main 对照或说明
  • 核心改动已有对应 focused test / smoke / integration 验证

UI 截图(仅可见 UI 变化时必填)

  • 本 PR 无可见 UI 变化(无需截图)

Scope / 后续

仅 portfolio-import 表头 BOM。不包含 #356–#360 的币种、会计格式、全角数字、零宽字符或符号长度。

@helsome helsome left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请先提供真正的修复前失败证据:JavaScript String.prototype.trim() 本身会移除 U+FEFF,原 normalizeHeaderCell(cell).trim() 很可能已经支持行首 BOM。因此当前补丁可能只增加重复逻辑,新增的等价性测试在 main 上也可能已通过。请在原 main SHA 上执行新增测试并贴出失败结果,或给出仍会失败的真实 CSV 样本。

wannT-1 commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

感谢 review。我按你说的在原 main 上核过了:只加入这两条新测试、不改 parsers.ts,测试是通过的,没有修复前失败。

在原 main f5822f0cb27b1f5843fe3ed5cc2f3f5baecc75de 上,normalizeHeaderCell 当时就是:

return cell.trim().toLowerCase().replace(/[\s_\-]/g, '')

表头单元格在别名匹配前都会走它。Bun 1.4.4 里 '\uFEFF'.trim() === '',并且 /\s/ 也匹配 U+FEFF,所以行首、行尾和单元格中间的 BOM 都会被去掉。PR 里新增的 replace(/^\uFEFF+/, '') 和现有 trim() 在包含 BOM 的 2395 个字符串上结果完全一致。

命令:

git worktree add --detach /tmp/folio-main-f5822f0 f5822f0cb27b1f5843fe3ed5cc2f3f5baecc75de
cd /tmp/folio-main-f5822f0
bun test packages/shared/src/portfolio-import/parsers.test.ts -t "UTF-8 BOM"
bun test v1.4.4 (38afee6c8)
(pass) parseCsv > strips a leading UTF-8 BOM from English headers
(pass) parseCsv > strips a leading UTF-8 BOM from 中文 headers
 2 pass
 0 fail

当前 main fbdb900c75c3039d05c09ba121b4e27a3402871c 上同一函数、同一测试同样是 2 pass / 0 fail。

另外,ImportDialog 用 FileReader.readAsText(file)。Chrome 157 读取以 EF BB BF 开头的 CSV 时,BOM 在进 parseCsv 之前就已经被解码掉(首字符是 S,不是 U+FEFF)。即便用 Buffer.toString('utf8') 把 U+FEFF 留在字符串里,未打补丁的 parseCsv('\uFEFFSymbol,Quantity,Cost\nAAPL.US,10,100') 仍然得到 AAPL.US / 10 / 100 / confidence 1。

带引号、BOM 前有空格、BOM 在表头中间、CRLF、BOM 单独占一行、显式 headerMapping、TSV,以及当前 main 上的分号分隔,都与去掉 BOM 的同一文件结果相同。没有找到一条真实 CSV 是 trim() 处理不了、而这条补丁能修好的。

结论:这条生产改动是重复逻辑。我会关闭本 PR,不再推进这个补丁;相关 issue #407 一并关闭。

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.

bug(portfolio-import): UTF-8 BOM 导致首列表头无法匹配别名

2 participants