Skip to content

fix(snapshot): handle permission-denied files in workspace snapshot init#1907

Merged
piorpua merged 1 commit intomainfrom
fix/sentry-ELECTRON-G6
Mar 30, 2026
Merged

fix(snapshot): handle permission-denied files in workspace snapshot init#1907
piorpua merged 1 commit intomainfrom
fix/sentry-ELECTRON-G6

Conversation

@kaizhou-lab
Copy link
Copy Markdown
Collaborator

Summary

  • Sentry issue: ELECTRON-G6 — 113 events, escalating on v1.9.3
  • WorkspaceSnapshotService.createWorkingTreeSnapshot runs git add . which fails entirely when any file in the workspace has permission denied (e.g., open Office files on Windows). Added --ignore-errors flag so git continues indexing other files, and catch the non-zero exit for known indexing failures.
  • Added unit test that verifies init succeeds when a file is not readable.

Test plan

  • New test: init succeeds when a file is not readable (permission denied) — passes
  • All existing WorkspaceSnapshotService tests pass (22/22)
  • Type check passes (bunx tsc --noEmit)
  • Verify on Windows with a locked file in workspace

When creating a working tree snapshot, `git add .` fails entirely if any
file in the workspace is locked or permission-denied (common on Windows
with open Office files). Add --ignore-errors flag so git continues
indexing other files, and catch the non-zero exit for known indexing
failures.

Fixes ELECTRON-G6
@kaizhou-lab kaizhou-lab marked this pull request as ready for review March 30, 2026 07:01
@piorpua piorpua added the bot:reviewing Review in progress (mutex) label Mar 30, 2026
@piorpua
Copy link
Copy Markdown
Contributor

piorpua commented Mar 30, 2026

Code Review:fix(snapshot): handle permission-denied files in workspace snapshot init (#1907)

变更概述

本 PR 修复了 Sentry 问题 ELECTRON-G6:WorkspaceSnapshotService.createWorkingTreeSnapshot 在执行 git add . 时,如果 workspace 内存在权限拒绝的文件(如 Windows 上被占用的 Office 文件),整个快照操作会失败。修复方案是为 git add 添加 --ignore-errors 参数,并在 catch 中区分预期的索引失败与真正的意外错误,同时新增了单元测试。


方案评估

结论:✅ 方案合理

git add --ignore-errors 是处理此场景的标准 git 做法,catch 块的逻辑也正确区分了"部分文件索引失败"和"git 命令本身不可用"。后续的 commit --allow-empty 保证了即使有文件被跳过,快照仍可成功创建。方案简洁,与现有代码风格一致。


问题清单

🔵 LOW — 错误字符串匹配存在潜在脆弱性

文件src/process/services/WorkspaceSnapshotService.ts,第 357 行

问题代码

if (!stderr.includes('Permission denied') && !stderr.includes('unable to index file')) {
  throw error;
}

问题说明:通过字符串匹配 stderr 来判断错误类型存在边界情况:

  1. 非英语系统上的 git(如中文系统安装了本地化 git)会输出翻译后的错误信息,导致两个条件均不匹配,最终错误被 re-throw,等同于没有修复。
  2. Windows 上 git 的某些版本可能将同类错误表述为 Access is deniederror: open(...): Permission denied,后者虽然仍包含 Permission denied,但前者不会。

这是 LOW 而非 HIGH,因为:实践中绝大多数 Windows git 安装均为英文版,且错误已涵盖 unable to index file 这一 git 的统一前缀;但若将来遇到非英文环境,此修复会静默失效。

改善建议(可选,不阻塞合并):

// 更宽松地检查:只要是部分索引失败(git 本身运行成功),就不 re-throw
// git add --ignore-errors 失败时 exit code 为 1-2;git not found 为 127
const exitCode = (error as { status?: number }).status ?? -1;
if (exitCode !== 1 && exitCode !== 2) {
  throw error;
}

🔵 LOW — Windows 平台上测试无法实际覆盖修复路径

文件tests/unit/WorkspaceSnapshotService.test.ts,第 38 行

问题代码

await fs.chmod(unreadablePath, 0o000);

问题说明chmod(0o000) 在 macOS/Linux 上可有效阻止 git 读取文件,触发修复路径。但在 Windows 上,Node.js 的 fs.chmod 不会实际阻止文件所有者读取文件,导致 git add 能正常成功,修复后的 catch 块实际上不会被执行。这意味着 Windows CI(Unit Tests (windows-2022))通过并不代表 Windows 修复路径被测试到了。

改善建议(可选):

it('init succeeds when a file is not readable (permission denied)', async () => {
  // chmod(0o000) only prevents file access on Unix; skip on Windows
  if (process.platform === 'win32') return;
  // ... rest of test
});

汇总

# 严重级别 文件 问题
1 🔵 LOW WorkspaceSnapshotService.ts:357 错误字符串匹配存在非英文风险
2 🔵 LOW WorkspaceSnapshotService.test.ts:38 Windows 平台测试无法覆盖修复路径

结论

批准合并 — 两个问题均为 LOW,不阻塞合并;核心修复逻辑正确,单元测试覆盖了 Unix 平台的主要场景。


本报告由本地 pr-review skill 生成,包含完整项目上下文,无截断限制。

CONCLUSION: APPROVED
IS_CRITICAL_PATH: false
PR_NUMBER: 1907

@piorpua
Copy link
Copy Markdown
Contributor

piorpua commented Mar 30, 2026

✅ 已自动 review,无阻塞性问题,正在触发自动合并。

@piorpua piorpua added bot:done Auto-merged by bot and removed bot:reviewing Review in progress (mutex) labels Mar 30, 2026
@piorpua piorpua merged commit 334583b into main Mar 30, 2026
17 checks passed
@piorpua piorpua deleted the fix/sentry-ELECTRON-G6 branch March 30, 2026 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:done Auto-merged by bot

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants