Skip to content

feat: add --check_format=json|pretty #3051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 6, 2025
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
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
cfgs[2] = {} -- only warns missing `b`
```
This enables the previous missing field check behavior before [#2970](https://github.com/LuaLS/lua-language-server/issues/2970)
* `NEW` Added `--check_format=json|pretty` for use with `--check` to output diagnostics in a human readable format.

## 3.13.5
`2024-12-20`
Expand Down
4 changes: 3 additions & 1 deletion locale/en-us/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,10 @@ CLI_CHECK_SUCCESS =
'Diagnosis completed, no problems found'
CLI_CHECK_PROGRESS =
'Found {} problems in {} files'
CLI_CHECK_RESULTS =
CLI_CHECK_RESULTS_OUTPATH =
'Diagnosis complete, {} problems found, see {}'
CLI_CHECK_RESULTS_PRETTY =
'Diagnosis complete, {} problems found'
CLI_CHECK_MULTIPLE_WORKERS =
'Starting {} worker tasks, progress output will be disabled. This may take a few minutes.'
CLI_DOC_INITING =
Expand Down
4 changes: 3 additions & 1 deletion locale/ja-jp/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,10 @@ CLI_CHECK_SUCCESS =
'診断が完了しました。問題は見つかりませんでした'
CLI_CHECK_PROGRESS =
'{} ファイルに渡り、{} 個の問題が発見されました'
CLI_CHECK_RESULTS =
CLI_CHECK_RESULTS_OUTPATH =
'診断が完了しました。{} 個の問題が発見されました。詳しくは {} をご確認ください'
CLI_CHECK_RESULTS_PRETTY =
'診断が完了しました。{} 個の問題が発見されました'
CLI_CHECK_MULTIPLE_WORKERS =
'{} 個のワーカータスクを開始しているため、進行状況の出力が無効になります。完了まで数分かかることがあります。'
CLI_DOC_INITING =
Expand Down
4 changes: 3 additions & 1 deletion locale/pt-br/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,10 @@ CLI_CHECK_SUCCESS =
'Diagnóstico completo, nenhum problema encontrado'
CLI_CHECK_PROGRESS = -- TODO: need translate!
'Found {} problems in {} files'
CLI_CHECK_RESULTS =
CLI_CHECK_RESULTS_OUTPATH =
'Diagnóstico completo, {} problemas encontrados, veja {}'
CLI_CHECK_RESULTS_PRETTY =
'Diagnóstico completo, {} problemas encontrados'
CLI_CHECK_MULTIPLE_WORKERS = -- TODO: need translate!
'Starting {} worker tasks, progress output will be disabled. This may take a few minutes.'
CLI_DOC_INITING = -- TODO: need translate!
Expand Down
4 changes: 3 additions & 1 deletion locale/zh-cn/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,10 @@ CLI_CHECK_SUCCESS =
'诊断完成,没有发现问题'
CLI_CHECK_PROGRESS =
'检测到问题 {} 在文件 {} 中'
CLI_CHECK_RESULTS =
CLI_CHECK_RESULTS_OUTPATH =
'诊断完成,共有 {} 个问题,请查看 {}'
CLI_CHECK_RESULTS_PRETTY =
'诊断完成,共有 {} 个问题'
CLI_CHECK_MULTIPLE_WORKERS =
'开启 {} 个工作任务,进度输出将会被禁用。这可能会花费几分钟。'
CLI_DOC_INITING =
Expand Down
4 changes: 3 additions & 1 deletion locale/zh-tw/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,10 @@ CLI_CHECK_SUCCESS =
'診斷完成,沒有發現問題'
CLI_CHECK_PROGRESS = -- TODO: need translate!
'Found {} problems in {} files'
CLI_CHECK_RESULTS =
CLI_CHECK_RESULTS_OUTPATH =
'診斷完成,共有 {} 個問題,請查看 {}'
CLI_CHECK_RESULTS_PRETTY =
'診斷完成,共有 {} 個問題'
CLI_CHECK_MULTIPLE_WORKERS = -- TODO: need translate!
'Starting {} worker tasks, progress output will be disabled. This may take a few minutes.'
CLI_DOC_INITING = -- TODO: need translate!
Expand Down
105 changes: 58 additions & 47 deletions script/cli/check.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,46 @@ local util = require 'utility'

local export = {}

local function logFileForThread(threadId)
return LOGPATH .. '/check-partial-' .. threadId .. '.json'
end

local function buildArgs(exe, numThreads, threadId, format, quiet)
local args = {exe}
local skipNext = false
for i = 1, #arg do
local arg = arg[i]
-- --check needs to be transformed into --check_worker
if arg:lower():match('^%-%-check$') or arg:lower():match('^%-%-check=') then
args[#args + 1] = arg:gsub('%-%-%w*', '--check_worker')
-- --check_out_path needs to be removed if we have more than one thread
elseif arg:lower():match('%-%-check_out_path') and numThreads > 1 then
if not arg:match('%-%-%w*=') then
skipNext = true
end
else
if skipNext then
skipNext = false
else
args[#args + 1] = arg
end
end
end
args[#args + 1] = '--thread_id'
args[#args + 1] = tostring(threadId)
if numThreads > 1 then
if quiet then
args[#args + 1] = '--quiet'
end
if format then
args[#args + 1] = '--check_format=' .. format
end
args[#args + 1] = '--check_out_path'
args[#args + 1] = logFileForThread(threadId)
end
return args
end

function export.runCLI()
local numThreads = tonumber(NUM_THREADS or 1)

Expand All @@ -21,48 +61,13 @@ function export.runCLI()
exe = exe..'.exe'
end

local function logFileForThread(threadId)
return LOGPATH .. '/check-partial-' .. threadId .. '.json'
end

local function buildArgs(threadId)
local args = {exe}
local skipNext = false
for i = 1, #arg do
local arg = arg[i]
-- --check needs to be transformed into --check_worker
if arg:lower():match('^%-%-check$') or arg:lower():match('^%-%-check=') then
args[#args + 1] = arg:gsub('%-%-%w*', '--check_worker')
-- --check_out_path needs to be removed if we have more than one thread
elseif arg:lower():match('%-%-check_out_path') and numThreads > 1 then
if not arg:match('%-%-%w*=') then
skipNext = true
end
else
if skipNext then
skipNext = false
else
args[#args + 1] = arg
end
end
end
args[#args + 1] = '--thread_id'
args[#args + 1] = tostring(threadId)
if numThreads > 1 then
args[#args + 1] = '--quiet'
args[#args + 1] = '--check_out_path'
args[#args + 1] = logFileForThread(threadId)
end
return args
end

if numThreads > 1 then
if not QUIET and numThreads > 1 then
print(lang.script('CLI_CHECK_MULTIPLE_WORKERS', numThreads))
end

local procs = {}
for i = 1, numThreads do
local process, err = subprocess.spawn({buildArgs(i)})
local process, err = subprocess.spawn({buildArgs(exe, numThreads, i, CHECK_FORMAT, QUIET)})
if err then
print(err)
end
Expand All @@ -76,11 +81,6 @@ function export.runCLI()
checkPassed = process:wait() == 0 and checkPassed
end

local outpath = CHECK_OUT_PATH
if outpath == nil then
outpath = LOGPATH .. '/check.json'
end

if numThreads > 1 then
local mergedResults = {}
local count = 0
Expand All @@ -95,11 +95,22 @@ function export.runCLI()
end
end
end
util.saveFile(outpath, jsonb.beautify(mergedResults))
if count == 0 then
print(lang.script('CLI_CHECK_SUCCESS'))
else
print(lang.script('CLI_CHECK_RESULTS', count, outpath))

local outpath = nil

if CHECK_FORMAT == 'json' or CHECK_OUT_PATH then
outpath = CHECK_OUT_PATH or LOGPATH .. '/check.json'
util.saveFile(outpath, jsonb.beautify(mergedResults))
end

if not QUIET then
if count == 0 then
print(lang.script('CLI_CHECK_SUCCESS'))
elseif outpath then
print(lang.script('CLI_CHECK_RESULTS_OUTPATH', count, outpath))
else
print(lang.script('CLI_CHECK_RESULTS_PRETTY', count))
end
end
end
return checkPassed and 0 or 1
Expand Down
Loading