3b385ef575
- scripts/lint-staged-rustfmt.mjs:12 [maintainability medium] 改为读取 lint-staged 追加的暂存 .rs 路径,只对真正命中暂存文件的 workspace 跑 cargo fmt --all -- --check:此前永远全量跑两个 workspace,只暂存 server-rs 干净文件的提交会被别人未暂存的 src-tauri 改动挡下来;没收到暂存列表(手工执行)时回退全量检查并在 stderr 说明,避免假装检查过。 - scripts/lint-staged-rustfmt.mjs:7 [maintainability low] apps/desktop-shell/src-tauri 显式记为「有意排除」而不是静默通过:实测该 workspace 当前整体不过 cargo fmt --check(host_bridge/mod.rs 等),直接纳入会让任何 Rust 提交都失败;命中该前缀的暂存文件会打印跳过提示,纳入步骤写在模块注释里(同时要补 package.json 的 check:rustfmt / format:rust,package.json 不在本批次改动范围)。 - 新增 scripts/lint-staged-rustfmt-workspaces.mjs:把「暂存路径 → 待检查 workspace」的纯映射逻辑独立出来,附 Windows 反斜杠/绝对路径归一化。 - 新增 scripts/lint-staged-rustfmt.test.ts(vitest,随 npm test 运行):覆盖只暂存单侧 workspace 的回归判据、手工执行回退全量、排除项与未登记路径必须被点名、路径归一化,以及与 package.json check:rustfmt 的 workspace 表同集交叉校验。
61 lines
2.5 KiB
JavaScript
61 lines
2.5 KiB
JavaScript
import { spawnSync } from 'node:child_process';
|
|
import { dirname, resolve } from 'node:path';
|
|
import process from 'node:process';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
import { selectRustfmtWorkspaces } from './lint-staged-rustfmt-workspaces.mjs';
|
|
|
|
// lint-staged 会把命中的暂存文件路径追加到命令末尾,而 `cargo fmt` 只按 workspace 粒度格式化、
|
|
// 不接受文件参数(也做不到「只格式化某个文件」),所以这里用暂存路径筛出**需要检查的
|
|
// workspace**,再对它们跑 `--check`。只查不改:pre-commit 不应该自动改写别人正在改的 Rust 文件。
|
|
const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..');
|
|
const { workspaces, excluded, unmanaged, usedStagedList } =
|
|
selectRustfmtWorkspaces({
|
|
stagedFiles: process.argv.slice(2),
|
|
repoRoot,
|
|
});
|
|
|
|
// 静默通过是这里最贵的失败模式:glob 命中了文件、但没有任何 workspace 检查到它。
|
|
if (excluded.length > 0) {
|
|
process.stderr.write(
|
|
`Rust 格式检查跳过(有意排除的 workspace):${excluded.join(', ')}\n` +
|
|
`该 workspace 当前整体未过 \`cargo fmt --check\`,详见 scripts/lint-staged-rustfmt-workspaces.mjs。\n`,
|
|
);
|
|
}
|
|
if (unmanaged.length > 0) {
|
|
process.stderr.write(
|
|
`Rust 格式检查未覆盖以下暂存文件(不在任何已登记 workspace 内):${unmanaged.join(', ')}\n` +
|
|
`如果是新 workspace,请登记到 scripts/lint-staged-rustfmt-workspaces.mjs。\n`,
|
|
);
|
|
}
|
|
|
|
// 一条都没筛出来又不曾拿到暂存列表,说明调用方式变了(例如 lint-staged 不再追加参数):
|
|
// 宁可回退到全量检查,也不要假装检查过了。
|
|
if (!usedStagedList) {
|
|
process.stderr.write(
|
|
'Rust 格式检查未收到 lint-staged 的暂存文件列表,回退为全量检查所有 workspace。\n',
|
|
);
|
|
}
|
|
|
|
for (const { manifestPath } of workspaces) {
|
|
const result = spawnSync(
|
|
'cargo',
|
|
['fmt', '--all', '--manifest-path', manifestPath, '--', '--check'],
|
|
{ stdio: 'inherit', shell: process.platform === 'win32' },
|
|
);
|
|
if (result.error) {
|
|
process.stderr.write(
|
|
`Rust 格式检查无法执行:${manifestPath}: ${result.error.message}\n`,
|
|
);
|
|
process.exit(1);
|
|
}
|
|
if (result.status !== 0) {
|
|
process.stderr.write(
|
|
`Rust 格式检查未通过:${manifestPath}\n` +
|
|
`本地复现:npm run check:rustfmt\n` +
|
|
`自动修复:cargo fmt --all --manifest-path ${manifestPath}\n`,
|
|
);
|
|
process.exit(result.status ?? 1);
|
|
}
|
|
}
|