[Feature] processledger 与 loginenv 仍手写固定 .tmp 文件改名写入,迁移到 atomicfile.Write

Open Beginner friendly
#2,376 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Refactor
Clarity
Clearly specified
Activity status
Active
Tech stack
go
Domain
desktop

Research direction

Read desktop/internal/atomicfile first to confirm the Write signature and its fsync/cleanup behaviour, then replace the hand-rolled temp-and-rename blocks in (*Ledger).write (desktop/internal/processledger/processledger.go, ~lines 228-241) and writeCache (desktop/internal/loginenv/loginenv_unix.go, ~lines 97-111). Decide how writeCache should report failures instead of swallowing them, then add tests showing no .tmp residue, preserved old contents on failure, and concurrent writers. Run go test ./desktop/internal/processledger/... ./desktop/internal/loginenv/... to verify; done means neither file calls os.Rename or builds a .tmp path itself.

Written by the indexing model from the issue text.

Description

refactor review: high
Problem to solve / 要解决的问题

在完成 #2257 调查的 10 处 CreateTemp 原子替换迁移后,通过全仓库搜索 os.Rename( 与 .tmp 发现,桌面端还有两处预存的整文件原子写入未使用 desktop/internal/atomicfile,而是各自手写固定 .tmp 后缀文件再 os.Rename:

  • desktop/internal/processledger/processledger.go 的 (*Ledger).write(第 228–241 行):使用固定文件名 l.path + ".tmp" 调用 os.WriteFile(temporary, data, 0o600) 后直接 os.Rename(temporary, l.path)。调用方为同文件第 130、153、197 行。
  • desktop/internal/loginenv/loginenv_unix.go 的 writeCache(第 97–111 行):使用固定文件名 path + ".tmp" 调用 os.WriteFile(temp, []byte(value+"\n"), 0o600) 后 os.Rename(temp, path)。调用方为同文件第 46、56 行。

判定依据:在 desktop/internal/ 下检索 os.Rename( 与 \.tmp(排除 *_test.go),核对全部单文件原子替换实现。保留现状的代价:

  1. 两处都使用固定 .tmp 路径而非 os.CreateTemp 独占临时文件;多个 Lithe 实例并发启动或刷新登录环境/进程账本时可能互相覆盖同一临时文件。
  2. 两处都不在改名前 fsync;崩溃或断电后目标文件可能为空或半截(processledger.json 损坏会导致孤儿进程无法识别与清理)。
  3. (*Ledger).write 在 os.Rename 失败时不清理 l.path + ".tmp";writeCache 静默丢弃 MkdirAll/WriteFile/Rename 的错误且无日志或豁免说明。
Proposed solution / 建议方案

将上述两处(均属于 Unix / !windows 启动辅助文件,目标为单文件原子替换)统一迁移到 desktop/internal/atomicfile.Write:

  • (*Ledger).write 改为调用 atomicfile.Write(l.path, data, 0o600),删除手写 os.MkdirAll、l.path + ".tmp" 与 os.Rename。
  • writeCache 改为调用 atomicfile.Write(path, []byte(value+"\n"), 0o600),并按统一日志规范决定失败时记录诊断或显式声明可丢弃原因,不再静默忽略错误。
  • 为两处补充并发/失败不留 .tmp 及保留旧文件内容的单元测试。
Target platform / 目标平台

Cross-platform / 跨平台

Area / 模块

Other / 其他

Priority / 优先级

P2 / 后续考虑:目前不着急,可安排到后续版本

Alternatives considered / 已考虑的替代方案

各自原地把 path + ".tmp" 改成 os.CreateTemp 并补 Sync() 与清理逻辑。这样会再次复制 desktop/internal/atomicfile 已提供的能力,后续维护仍需分别修复。

Additional context / 补充信息

在审核 PR #2351(#2257)时通过全仓库检索 os.Rename( 与 \.tmp 发现,属于改动前既有代码,按开发规范第九节独立建单跟踪。涉及路径仅在 Unix(macOS 与 Linux)执行写入,processledger 在 Windows 为只读空实现,loginenv_unix.go 为 //go:build !windows。

Checklist / 提交前检查
  • I searched existing issues for duplicates. / 我已搜索现有 Issue,确认没有重复建议。
Dominant language
TypeScript
Stars
1.8k
Forks
151
Avg merge
4h 4m
Merged PRs (30d)
558

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from 1lck/Lithe-IDEA

All issues in 1lck/Lithe-IDEA

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.