Skip to content

Commit 64a7cf9

Browse files
committed
fix: validate lua before build
1 parent f220e65 commit 64a7cf9

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
},
3838
"scripts": {
3939
"build": "tsdown src/index.ts --format esm --format cjs --dts --sourcemap && node scripts/copy-lua.mjs",
40+
"prebuild": "pnpm test:lua",
4041
"test": "vitest run --no-file-parallelism",
42+
"test:lua": "node scripts/validate-lua-scripts.js",
4143
"test:many": "for i in {1..200}; do pnpm run test; done",
4244
"dev:test": "vitest --watch",
4345
"monitor": "jiti cli.ts",

scripts/validate-lua-scripts.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Lua Script Syntax Validator
5+
* Validates all Lua scripts for syntax errors before deployment
6+
*/
7+
8+
import { execSync } from 'child_process';
9+
import { readdirSync, readFileSync, unlinkSync, writeFileSync } from 'fs';
10+
import { join } from 'path';
11+
12+
const LUA_DIR = 'src/lua';
13+
const REDIS_CLI = 'redis-cli';
14+
15+
console.log('🔍 Validating Lua scripts for syntax errors...\n');
16+
17+
// Get all Lua files
18+
const luaFiles = readdirSync(LUA_DIR)
19+
.filter((file) => file.endsWith('.lua'))
20+
.sort();
21+
22+
let hasErrors = false;
23+
24+
for (const file of luaFiles) {
25+
const filePath = join(LUA_DIR, file);
26+
const content = readFileSync(filePath, 'utf8');
27+
28+
console.log(`📄 Validating ${file}...`);
29+
30+
try {
31+
// Write content to temporary file to avoid shell escaping issues
32+
const tempFile = `/tmp/validate-${file}`;
33+
writeFileSync(tempFile, content);
34+
35+
// Use redis-cli to validate the script with dummy arguments
36+
// This ensures the script is actually compiled, not just parsed
37+
const result = execSync(`${REDIS_CLI} --eval ${tempFile} dummy`, {
38+
encoding: 'utf8',
39+
stdio: 'pipe',
40+
timeout: 5000,
41+
});
42+
43+
// Clean up temp file
44+
unlinkSync(tempFile);
45+
46+
// Check if the result contains error messages
47+
if (
48+
result.includes('ERR Error compiling script') ||
49+
result.includes('syntax error')
50+
) {
51+
throw new Error(`Lua syntax error: ${result.trim()}`);
52+
}
53+
54+
console.log(`✅ ${file} - OK`);
55+
} catch (error) {
56+
console.log(`❌ ${file} - SYNTAX ERROR`);
57+
console.log(` Error: ${error.message}`);
58+
hasErrors = true;
59+
}
60+
}
61+
62+
if (hasErrors) {
63+
console.log('\n🚨 Lua script validation failed!');
64+
process.exit(1);
65+
} else {
66+
console.log('\n✅ All Lua scripts are syntactically valid!');
67+
process.exit(0);
68+
}

src/lua/reserve-batch.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ for i = 1, #groups, 2 do
6262
table.insert(out, id .. "||DELIMITER||" .. groupId .. "||DELIMITER||" .. payload .. "||DELIMITER||" .. attempts .. "||DELIMITER||" .. maxAttempts .. "||DELIMITER||" .. seq .. "||DELIMITER||" .. enq .. "||DELIMITER||" .. orderMs .. "||DELIMITER||" .. score .. "||DELIMITER||" .. deadline)
6363
table.insert(processedGroups, gid)
6464
end
65-
end
6665
end
6766
end
6867
-- Note: Locked groups remain in ready queue from original ZRANGE

0 commit comments

Comments
 (0)