Skip to content

Commit d6c4589

Browse files
lpcoxCopilotCopilotgithub-advanced-security[bot]Copilot
authored
fix: load schema via require() for pkg/esbuild compat (#2396)
* fix: load schema via require() for pkg/esbuild compat The release binary failed because pkg couldn't find awf-config-schema.json at runtime — the fs.readFileSync approach didn't work in pkg's snapshot filesystem. Switch to require() which both esbuild (inlines into bundle) and pkg (includes in snapshot via static analysis) handle natively. Also add dist/awf-config-schema.json to pkg.assets as a fallback. Fixes: Release workflow 'Smoke test binary (x64)' failure Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Potential fix for pull request finding 'CodeQL / Assignment to constant' Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> * Potential fix for pull request finding 'CodeQL / Unused variable, import, function or class' Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> * fix: remove unused schemaJson import causing CodeQL alerts Agent-Logs-Url: https://github.com/github/gh-aw-firewall/sessions/f2ea8212-e983-4cfc-9d6f-8bee57e01657 --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 08cd698 commit d6c4589

2 files changed

Lines changed: 8 additions & 25 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@
8989
"scripts": "dist/**/*.js",
9090
"assets": [
9191
"node_modules/chalk/**/*",
92-
"containers/agent/seccomp-profile.json"
92+
"containers/agent/seccomp-profile.json",
93+
"dist/awf-config-schema.json"
9394
],
9495
"targets": [
9596
"node18-linux-x64",

src/schema-validator.ts

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,21 @@
11
/**
22
* Runtime config validation using the published JSON Schema.
33
*
4-
* The schema JSON is loaded from disk so the emitted JS does not contain a
5-
* fragile runtime `require('./awf-config-schema.json')`. The canonical source
6-
* is generated by `scripts/generate-schema.mjs` and kept in sync via CI.
4+
* The schema is loaded via a static `import` so tsc emits the JSON to
5+
* dist/, esbuild inlines it in the bundle, and pkg includes it in the snapshot.
6+
* The canonical source is generated by `scripts/generate-schema.mjs`.
77
*/
88

99
import Ajv2020, { ErrorObject } from 'ajv/dist/2020';
10-
import { existsSync, readFileSync } from 'fs';
11-
import { join } from 'path';
12-
13-
function loadSchema(): Record<string, unknown> {
14-
const candidatePaths = [
15-
join(__dirname, 'awf-config-schema.json'),
16-
join(__dirname, '../src/awf-config-schema.json'),
17-
];
18-
19-
for (const candidatePath of candidatePaths) {
20-
if (existsSync(candidatePath)) {
21-
return JSON.parse(readFileSync(candidatePath, 'utf8')) as Record<string, unknown>;
22-
}
23-
}
10+
import schemaJson from './awf-config-schema.json';
2411

25-
throw new Error(
26-
`Unable to locate awf-config-schema.json. Checked: ${candidatePaths.join(', ')}`,
27-
);
28-
}
12+
const schema = schemaJson as Record<string, unknown>;
2913

30-
// Compile once (module-level singleton). allErrors collects every violation.
31-
// verbose=true provides parentSchema on errors for richer formatting.
3214
const ajv = new Ajv2020({ allErrors: true, verbose: true });
3315
// 'version' is a metadata keyword (not a standard JSON Schema keyword); register
3416
// it so Ajv strict mode does not reject the schema.
3517
ajv.addKeyword({ keyword: 'version' });
36-
const validate = ajv.compile(loadSchema());
18+
const validate = ajv.compile(schema);
3719

3820
/**
3921
* Check if a schema path points to an array-of-strings field by inspecting

0 commit comments

Comments
 (0)