Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ jobs:
run: pnpm errors:normalize:test && pnpm errors:normalize:check
- name: Validate workflow pnpm version matches packageManager
run: pnpm ci:pnpm-version:test && pnpm ci:pnpm-version:check
- name: Validate Node version file, engines, and workflows
run: pnpm ci:node-version:test && pnpm ci:node-version:check

- name: Validate pinned Rust toolchain alignment
run: pnpm ci:rust-toolchain:test && pnpm ci:rust-toolchain:check
Expand Down
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
22
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Do not invent newer requirements.

| Tool | Version | Notes |
| --- | --- | --- |
| Node.js | `>= 22` | pinned by `engines.node` |
| Node.js | `>= 22` | `nvm use` reads `.nvmrc` to select Node 22; `engines.node` requires at least 22 |
| pnpm | `10.13.1` | pinned by `packageManager`; `corepack enable` picks it up |
| Rust | `1.85.0` + `wasm32v1-none` target | pinned by `rust-toolchain.toml` |
| Stellar CLI | latest | `cargo install stellar-cli --locked` |
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@
"errors:normalize:test": "pnpm --filter @sub-rosa/logging test && node --test scripts/check-error-normalization.test.mjs",
"ci:pnpm-version:check": "node scripts/check-pnpm-version.mjs",
"ci:pnpm-version:test": "node --test scripts/check-pnpm-version.test.mjs",
"ci:node-version:check": "node scripts/check-node-version.mjs",
"ci:node-version:test": "node --test scripts/check-node-version.test.mjs",
"ci:rust-toolchain:check": "node scripts/check-rust-toolchain.mjs",
"ci:rust-toolchain:test": "node --test scripts/check-rust-toolchain.test.mjs",
"line-lengths:check": "node scripts/check-line-lengths.mjs",
Expand Down
69 changes: 69 additions & 0 deletions scripts/check-node-version.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { readFileSync, readdirSync } from 'node:fs';
import { join } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';

const root = fileURLToPath(new URL('..', import.meta.url));
const NODE_MAJOR = '22';

export function validateNodeVersions(versionFile, packageJsonText, workflows) {
const errors = [];
const version = versionFile.trim();
if (version !== NODE_MAJOR) {
errors.push(`.nvmrc must contain ${NODE_MAJOR}; found ${JSON.stringify(version)}`);
}

const engine = JSON.parse(packageJsonText).engines?.node;
if (engine !== `>=${NODE_MAJOR}`) {
errors.push(`package.json engines.node must be >=${NODE_MAJOR}; found ${JSON.stringify(engine)}`);
}

if (Object.keys(workflows).length === 0) {
errors.push('No GitHub Actions workflows found');
}
for (const [name, content] of Object.entries(workflows)) {
const lines = content.split(/\r?\n/);
let setupCount = 0;
for (let i = 0; i < lines.length; i++) {
const step = lines[i].match(/^(\s*)-\s+uses:\s*actions\/setup-node@v\d+\s*$/);
if (!step) continue;
setupCount++;
let versionInStep;
for (let j = i + 1; j < lines.length; j++) {
const line = lines[j];
if (line.trim() && line.match(/^\s*/)[0].length <= step[1].length) break;
const match = line.trim().match(/^node-version:\s*(.*)$/);
if (match) {
versionInStep = match[1].replace(/\s+#.*$/, '').trim().replace(/^['"]|['"]$/g, '');
break;
}
}
if (versionInStep !== NODE_MAJOR) {
errors.push(`${name}: actions/setup-node must use node-version: ${NODE_MAJOR}; found ${JSON.stringify(versionInStep)}`);
}
}
if (setupCount === 0) {
errors.push(`${name}: missing actions/setup-node`);
}
}
return errors;
}

if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
const workflowsDir = join(root, '.github/workflows');
const workflows = Object.fromEntries(
readdirSync(workflowsDir)
.filter((name) => /\.ya?ml$/.test(name))
.map((name) => [name, readFileSync(join(workflowsDir, name), 'utf8')]),
);
const errors = validateNodeVersions(
readFileSync(join(root, '.nvmrc'), 'utf8'),
readFileSync(join(root, 'package.json'), 'utf8'),
workflows,
);
if (errors.length > 0) {
process.stderr.write(`${errors.join('\n')}\n`);
process.exitCode = 1;
} else {
process.stdout.write('Node 22 matches .nvmrc, engines.node, and every workflow\n');
}
}
43 changes: 43 additions & 0 deletions scripts/check-node-version.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { validateNodeVersions } from './check-node-version.mjs';

const packageJson = JSON.stringify({ engines: { node: '>=22' } });
const workflow = `jobs:
check:
steps:
- uses: actions/setup-node@v4
with:
node-version: '22'
`;

test('accepts Node 22 in the version file, engines, and every workflow', () => {
assert.deepEqual(validateNodeVersions('22\n', packageJson, { 'check.yml': workflow }), []);
});

test('rejects a different version file or engine minimum', () => {
const errors = validateNodeVersions('20\n', JSON.stringify({ engines: { node: '>=20' } }), {
'check.yml': workflow,
});
assert.match(errors[0], /\.nvmrc must contain 22/);
assert.match(errors[1], /engines\.node must be >=22/);
});

test('checks every setup-node step and reports missing versions', () => {
const workflows = {
'good.yml': workflow,
'wrong.yml': workflow.replace("node-version: '22'", 'node-version: "20"'),
'missing.yml': workflow.replace("node-version: '22'", 'cache: pnpm'),
};
const errors = validateNodeVersions('22\n', packageJson, workflows);
assert.equal(errors.length, 2);
assert.match(errors[0], /wrong\.yml.*found "20"/);
assert.match(errors[1], /missing\.yml.*found undefined/);
});

test('rejects a workflow without setup-node', () => {
const errors = validateNodeVersions('22\n', packageJson, {
'rust.yml': 'jobs:\n check:\n steps:\n - uses: actions/checkout@v4\n',
});
assert.deepEqual(errors, ['rust.yml: missing actions/setup-node']);
});
Loading