Skip to content

Commit 039e6a9

Browse files
authored
Fix E2BIG error in pre-commit (#1712)
When pre-commit runs a command, it tries to avoid putting too many arguments on the command-line by splitting up the arg list. To do so, it needs to take into account the number of arguments that are on the command itself, in addition to the args it will add for the files it needs to process. If the command in `.pre-commit-config.yaml` calls a subcommand that has more arguments, then pre-commit's calculation of how many args it needs to leave for the call itself will be wrong, and we get E2BIG. The hack is to just inline whatever subcommand gets called into `.pre-commit-config.yaml`. Quite annoying and duplicative, but it appears there's no way around it. See pre-commit/pre-commit#2841 ## Checklist - [ ] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [ ] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [ ] I have not broken the cheatsheet
1 parent c825703 commit 039e6a9

File tree

4 files changed

+69
-4
lines changed

4 files changed

+69
-4
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ repos:
6262
name: format-recorded-tests
6363
files: ^packages/cursorless-vscode-e2e/src/suite/fixtures/recorded/.*/[^/]*\.yml$
6464
language: system
65-
entry: pnpm transform-recorded-tests
65+
entry: pnpm exec tsx --conditions=cursorless:bundler packages/cursorless-engine/src/scripts/transformRecordedTests/index.ts
6666
- repo: local
6767
hooks:
6868
- id: check-recorded-test-marks
6969
name: check-recorded-test-marks
7070
files: ^packages/cursorless-vscode-e2e/src/suite/fixtures/recorded/.*/[^/]*\.yml$
7171
language: system
72-
entry: pnpm transform-recorded-tests --check-marks
72+
entry: pnpm exec tsx --conditions=cursorless:bundler packages/cursorless-engine/src/scripts/transformRecordedTests/index.ts --check-marks
7373
- repo: https://github.com/ikamensh/flynt/
7474
rev: "0.78"
7575
hooks:

packages/meta-updater/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"@pnpm/logger": "^5.0.0",
1616
"@pnpm/types": "8.9.0",
1717
"@types/normalize-path": "^3.0.0",
18+
"js-yaml": "^4.1.0",
1819
"normalize-path": "^3.0.0",
1920
"path-exists": "^4.0.0",
2021
"type-fest": "3.6.1"
@@ -29,6 +30,7 @@
2930
}
3031
},
3132
"devDependencies": {
33+
"@types/js-yaml": "^4.0.2",
3234
"esbuild": "^0.17.11"
3335
}
3436
}

packages/meta-updater/src/updatePackageJson.ts

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import * as yaml from "js-yaml";
12
import type { FormatPluginFnOptions } from "@pnpm/meta-updater";
23
import { PackageJson } from "type-fest";
34
import { Context } from "./Context";
45
import { getCursorlessVscodeFields } from "./getCursorlessVscodeFields";
6+
import { readFile } from "fs/promises";
7+
import { join } from "path";
58

69
/**
710
* Given a package.json, update it to match our conventions. This function is
@@ -66,14 +69,30 @@ export async function updatePackageJson(
6669
...input,
6770
name,
6871
license: "MIT",
69-
scripts: getScripts(input.scripts, isRoot, isCursorlessVscode),
72+
scripts: await getScripts(
73+
input.scripts,
74+
packageDir,
75+
isRoot,
76+
isCursorlessVscode,
77+
),
7078
...exportFields,
7179
...extraFields,
7280
} as PackageJson;
7381
}
7482

75-
function getScripts(
83+
interface PreCommitConfig {
84+
repos: {
85+
hooks: {
86+
id: string;
87+
name: string;
88+
entry: string;
89+
}[];
90+
}[];
91+
}
92+
93+
async function getScripts(
7694
inputScripts: PackageJson.Scripts | undefined,
95+
packageDir: string,
7796
isRoot: boolean,
7897
isCursorlessVscode: boolean,
7998
) {
@@ -84,6 +103,12 @@ function getScripts(
84103
};
85104

86105
if (isRoot) {
106+
// Ensure that `pnpm transform-recorded-tests` mirrors what is in pre-commit
107+
// config
108+
scripts["transform-recorded-tests"] = await getTransformRecordedTestsScript(
109+
packageDir,
110+
);
111+
87112
return scripts;
88113
}
89114

@@ -97,3 +122,35 @@ function getScripts(
97122

98123
return scripts;
99124
}
125+
126+
async function getTransformRecordedTestsScript(packageDir: string) {
127+
const preCommitConfig = yaml.load(
128+
await readFile(join(packageDir, ".pre-commit-config.yaml"), "utf8"),
129+
) as PreCommitConfig;
130+
131+
const formatRecordedTestsEntry = preCommitConfig.repos
132+
.flatMap(({ hooks }) => hooks)
133+
.find(({ id }) => id === "format-recorded-tests")?.entry;
134+
135+
if (!formatRecordedTestsEntry?.startsWith("pnpm exec ")) {
136+
throw new Error(
137+
'Expected pre-commit transform-recorded-tests entry to start with "pnpm exec "',
138+
);
139+
}
140+
141+
const checkRecordedTestMarksEntry = preCommitConfig.repos
142+
.flatMap(({ hooks }) => hooks)
143+
.find(({ id }) => id === "check-recorded-test-marks")?.entry;
144+
145+
if (!checkRecordedTestMarksEntry?.startsWith(formatRecordedTestsEntry)) {
146+
throw new Error(
147+
"Expected pre-commit check-recorded-test-marks entry to mirror format-recorded-test-marks",
148+
);
149+
}
150+
151+
const transformRecordedTestsScript = formatRecordedTestsEntry.slice(
152+
"pnpm exec ".length,
153+
);
154+
155+
return transformRecordedTestsScript;
156+
}

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)