Support linting/errorchecking lua code in zig build system
It would be very nice if we could configure ziglua to lint our lua code when we are running zig build. We would then report any errors like we would report zig compile errors.
This would turn runtime errors into compile time errors which is always nicer.
Example
build.zig
try ziglua.lintLuaFile(b.path("data/lua/init.lua"), .{.luadoc = true, .emit_compile_errors = true});
try ziglua.lintLuaFile(b.path("data/lua/main.lua"), .{.luadoc = true, .emit_compile_errors = true});
try ziglua.lintLuaFile(b.path("data/lua/other.lua"), .{.luadoc = true, .emit_compile_errors = true});
To do this we would probably want to look into embedding LDoc into ziglua.
This sounds really useful!
So if I understand correctly, this lintLuaFile would take place during the build phase? I wonder if we could make it a proper build step... and is zig build even meant to handle things like this during build time?
This sounds really useful!
So if I understand correctly, this
lintLuaFilewould take place during the build phase?
Yes, if we do it at runtime it doesn't offer any extra benefit over just running the lua code to see what it does
I wonder if we could make it a proper build step...
I remember seeing andrew talking about how custom build steps should be implemented using std.Build.Step.Run or something similar. I will have to do more research on custom build steps.
and is zig build even meant to handle things like this during build time?
From my understanding, build.zig is written in normal zig code specifically to make it extensible and customizable for different use cases like this
Edit: as for implementation, this might be exactly what we are looking for
I've realized this is relatively simple to implement in build.zig using system commands. Here is what I have worked out so far in one of my own projects.
build.zig
//===============LINT LUA===========================
const lua_files_path = "data/lua";
const lint_commands: []const []const u8 = &.{
"lua-language-server",
"--check",
b.path(lua_files_path).getPath(b),
"--logpath",
"--checklevel=Hint",
"--logpath",
b.path(".zig-cache").getPath(b),
};
var lint = b.addSystemCommand(lint_commands);
const log_commands: []const []const u8 = &.{
"pygmentize",
b.path(".zig-cache/check.json").getPath(b),
};
var log = b.addSystemCommand(log_commands);
log.step.dependOn(&lint.step);
var lint_step = b.step("lint", "lint lua");
lint_step.dependOn(&log.step);
output
{
"file:///Users/Robert/zig/dev/data/lua/init.lua": [
{
"code": "unused-local",
"message": "Unused local `a`.",
"range": {
"end": {
"character": 7,
"line": 76
},
"start": {
"character": 6,
"line": 76
}
},
"severity": 4,
"source": "Lua Diagnostics.",
"tags": [
1
]
}
],
"file:///Users/Robert/zig/dev/data/lua/main.lua": [
{
"code": "undefined-global",
"message": "Undefined global `ZigLoadFile`.",
"range": {
"end": {
"character": 11,
"line": 6
},
"start": {
"character": 0,
"line": 6
}
},
"severity": 2,
"source": "Lua Diagnostics."
},
{
"code": "undefined-global",
"message": "Undefined global `ZigLuaStatePtr`.",
"range": {
"end": {
"character": 26,
"line": 6
},
"start": {
"character": 12,
"line": 6
}
},
"severity": 2,
"source": "Lua Diagnostics."
}
]
}