fix(executor): worktree 软链主仓 node_modules,修 verify exit 127 误判
git worktree 是干净检出不带 gitignored 的 node_modules,导致 verify (tsc/tsx 等)与执行 agent 跑 npm 脚本时 command not found(exit 127)被误判为失败。 建 worktree 后软链主仓 node_modules:零网络、同平台 native 兼容、被 gitignore 忽略不污染 diff; 非 Node 项目(无 node_modules)自动跳过。removeWorktree 只移除软链不动主仓依赖。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+33
-1
@@ -1,7 +1,7 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { execFileSync } from 'node:child_process';
|
||||
import { existsSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { existsSync, lstatSync, mkdirSync, mkdtempSync, readlinkSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { createWorktree, worktreeDiff, removeWorktree, worktreeDirFor, branchFor } from '../src/executor/worktree.js';
|
||||
@@ -73,6 +73,38 @@ test('worktree:create / diff / remove 在真实 git repo 上工作', async (t)
|
||||
assert.ok(existsSync(join(repo, 'README.md')), '主 repo 不受影响');
|
||||
});
|
||||
|
||||
test('worktree:主仓有 node_modules 时软链进 worktree(让 .bin/tsc 等可解析),无则不报错', async (t) => {
|
||||
const dataDir = mkdtempSync(join(tmpdir(), 'maestro-wt-data-nm-'));
|
||||
const prevDataDir = process.env.MAESTRO_DATA_DIR;
|
||||
process.env.MAESTRO_DATA_DIR = dataDir;
|
||||
const repo = makeRepo();
|
||||
t.after(() => {
|
||||
if (prevDataDir === undefined) delete process.env.MAESTRO_DATA_DIR;
|
||||
else process.env.MAESTRO_DATA_DIR = prevDataDir;
|
||||
rmSync(dataDir, { recursive: true, force: true });
|
||||
rmSync(repo, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
// 无 node_modules:建 worktree 不应报错,也不应凭空造出 node_modules
|
||||
const wtBare = await createWorktree(repo, 'tsk_nm_none', 'main');
|
||||
assert.ok(!existsSync(join(wtBare.dir, 'node_modules')), '主仓无 node_modules 时不应造软链');
|
||||
|
||||
// 主仓装一个 node_modules/.bin/tsc 占位 → 重建 worktree 应能解析到它
|
||||
const binDir = join(repo, 'node_modules', '.bin');
|
||||
mkdirSync(binDir, { recursive: true });
|
||||
writeFileSync(join(binDir, 'tsc'), '#!/bin/sh\necho tsc\n');
|
||||
const wt = await createWorktree(repo, 'tsk_nm', 'main');
|
||||
const link = join(wt.dir, 'node_modules');
|
||||
assert.ok(existsSync(link), 'worktree 应有 node_modules');
|
||||
assert.ok(lstatSync(link).isSymbolicLink(), 'node_modules 应是软链而非真实拷贝');
|
||||
assert.equal(readlinkSync(link), join(repo, 'node_modules'), '软链应指向主仓 node_modules');
|
||||
assert.ok(existsSync(join(link, '.bin', 'tsc')), '经软链可解析到 .bin/tsc');
|
||||
|
||||
// remove 不应误删主仓 node_modules(软链被移除,目标保留)
|
||||
await removeWorktree(repo, wt.dir);
|
||||
assert.ok(existsSync(join(repo, 'node_modules', '.bin', 'tsc')), '主仓 node_modules 不受 remove 影响');
|
||||
});
|
||||
|
||||
test('worktree:错误透传(不存在的 baseBranch)', async (t) => {
|
||||
const dataDir = mkdtempSync(join(tmpdir(), 'maestro-wt-data2-'));
|
||||
const prevDataDir = process.env.MAESTRO_DATA_DIR;
|
||||
|
||||
Reference in New Issue
Block a user