Repository navigation
Expand file tree
/
Copy pathdebug.lua
More file actions
345 lines (326 loc) · 10.3 KB
/
Copy pathdebug.lua
File metadata and controls
345 lines (326 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
-- Debug configurations for JS/TS projects, all through vscode-js-debug.
-- Folders are resolved when a session starts, from the current file upward,
-- so monorepo packages find their own vitest/jest/electron.
local M = {}
---Nearest folder that contains `rel`, from `from` (by default the current file) upward
---@param rel string
---@param from? string a file or folder to start at
---@return string?
function M.find_up(rel, from)
local file = vim.api.nvim_buf_get_name(0)
local start = from and vim.fs.joinpath(from, '_') or (file ~= '' and file or vim.fs.joinpath(vim.fn.getcwd(), '_'))
for dir in vim.fs.parents(start) do
if vim.uv.fs_stat(vim.fs.joinpath(dir, rel)) then
return dir
end
end
end
---@return string
function M.package_root()
return vim.fs.root(0, 'package.json') or vim.fn.getcwd()
end
---Current file with symlinks resolved: test runners compare against real paths
---@return string
function M.real_file()
local file = vim.api.nvim_buf_get_name(0)
return vim.uv.fs_realpath(file) or file
end
---Playwright reads file arguments as regular expressions
---@param s string
---@return string
function M.regex_escape(s)
return (s:gsub('[%^%$%(%)%.%[%]%*%+%?%{%}%|\\]', '\\%0'))
end
local locks = {
{ 'pnpm-lock.yaml', 'pnpm' },
{ 'yarn.lock', 'yarn' },
{ 'bun.lock', 'bun' },
{ 'bun.lockb', 'bun' },
{ 'package-lock.json', 'npm' },
}
---The package manager of the package in `dir` (by default the current file's):
---package.json's packageManager, else the nearest lockfile, which in a monorepo
---is at the root, above the package
---@param dir? string
---@return 'npm'|'pnpm'|'yarn'|'bun'
function M.package_manager(dir)
dir = type(dir) == 'string' and dir or M.package_root()
local read, lines = pcall(vim.fn.readfile, vim.fs.joinpath(dir, 'package.json'))
local ok, pkg = pcall(vim.json.decode, read and table.concat(lines, '\n') or '')
local declared = ok
and type(pkg) == 'table'
and type(pkg.packageManager) == 'string'
and pkg.packageManager:match('^(%a+)@')
if declared then
return declared
end
for parent in vim.fs.parents(vim.fs.joinpath(dir, '_')) do
for _, lock in ipairs(locks) do
if vim.uv.fs_stat(vim.fs.joinpath(parent, lock[1])) then
return lock[2]
end
end
end
return 'npm'
end
---Where `runner` runs the current file's tests (config/test_files.lua: its
---config's folder, else the package's), and where `rel` is installed: there or
---above, as in a monorepo with its tools at the top
---@param runner TestRunner
---@param rel string
---@return string dir, string? installed
local function runner_dir(runner, rel)
local file = vim.api.nvim_buf_get_name(0)
local dir = require('config.test_files').folder(file ~= '' and file or vim.fs.joinpath(vim.fn.getcwd(), '_'), runner)
return dir, M.find_up(rel, dir)
end
-- nvim-dap evaluates every function field before it checks for ABORT, so only
-- `cwd` reports a missing tool; the other fields abort quietly
local function cwd_of(runner, rel, what)
return function()
local dir, installed = runner_dir(runner, rel)
if not installed then
vim.notify(('%s not found: no %s in or above %s'):format(what, rel, dir), vim.log.levels.ERROR)
return require('dap').ABORT
end
return dir
end
end
local function bin(runner, rel)
return function()
local _, installed = runner_dir(runner, rel)
return installed and vim.fs.joinpath(installed, rel) or require('dap').ABORT
end
end
local skip = { '<node_internals>/**', '**/node_modules/**' }
local forge_script = 'node_modules/@electron-forge/cli/script/vscode.sh'
local electron_bin = 'node_modules/.bin/electron'
---Forge's Vite plugin builds the main process without source maps unless the
---project asks for them, and then breakpoints in src/ never bind
---@param root string
local function check_forge_sourcemaps(root)
for _, ext in ipairs({ 'mts', 'ts', 'mjs', 'js' }) do
local name = 'vite.main.config.' .. ext
local fd = io.open(vim.fs.joinpath(root, name))
if fd then
local text = fd:read('*a')
fd:close()
if not text:find('sourcemap', 1, true) then
vim.notify(
('Main-process breakpoints need source maps: add `build: { sourcemap: true }` to %s'):format(name),
vim.log.levels.WARN
)
end
return
end
end
end
---How to start Electron for the current file: Forge projects through Forge's
---debug script (builds with its Vite/Webpack plugin first), others directly
---@return { cwd: string, exe: string, args: string[], forge?: boolean }?
local function electron()
local forge = M.find_up(forge_script)
if forge then
-- Forge passes these on to the Electron app
return {
cwd = forge,
exe = vim.fs.joinpath(forge, forge_script),
args = { '--remote-debugging-port=9222' },
forge = true,
}
end
local plain = M.find_up(electron_bin)
if plain then
return { cwd = plain, exe = vim.fs.joinpath(plain, electron_bin), args = { '.', '--remote-debugging-port=9222' } }
end
end
M.electron_renderer = {
name = 'Electron: renderer (attach :9222)',
type = 'pwa-chrome',
request = 'attach',
port = 9222,
timeout = 60000,
webRoot = M.package_root,
}
local electron_main = {
type = 'pwa-node',
request = 'launch',
cwd = function()
local e = electron()
if not e then
vim.notify(
'Electron not found: no Electron Forge or node_modules/.bin/electron above this file',
vim.log.levels.ERROR
)
return require('dap').ABORT
end
if e.forge then
check_forge_sourcemaps(e.cwd)
end
return e.cwd
end,
runtimeExecutable = function()
local e = electron()
return e and e.exe or require('dap').ABORT
end,
runtimeArgs = function()
local e = electron()
return e and e.args or require('dap').ABORT
end,
sourceMaps = true,
skipFiles = skip,
console = 'integratedTerminal',
}
M.electron_both = 'Electron: main + renderer'
---@return dap.Configuration[]
function M.configurations()
return {
{
name = 'Node: current file',
type = 'pwa-node',
request = 'launch',
program = '${file}',
cwd = M.package_root,
sourceMaps = true,
skipFiles = skip,
console = 'integratedTerminal',
},
{
name = 'Node: attach to :9229 (node/next --inspect)',
type = 'pwa-node',
request = 'attach',
port = 9229,
restart = true,
cwd = M.package_root,
sourceMaps = true,
skipFiles = skip,
},
{
name = 'Node: attach to a process',
type = 'pwa-node',
request = 'attach',
processId = function()
return require('dap.utils').pick_process({ filter = 'node' })
end,
cwd = M.package_root,
sourceMaps = true,
skipFiles = skip,
},
{
name = 'Vitest: current file',
type = 'pwa-node',
request = 'launch',
cwd = cwd_of('vitest', 'node_modules/vitest/vitest.mjs', 'Vitest'),
program = bin('vitest', 'node_modules/vitest/vitest.mjs'),
args = function()
return { 'run', '--no-file-parallelism', M.real_file() }
end,
autoAttachChildProcesses = true,
smartStep = true,
skipFiles = skip,
console = 'integratedTerminal',
},
{
name = 'Jest: current file',
type = 'pwa-node',
request = 'launch',
cwd = cwd_of('jest', 'node_modules/jest/bin/jest.js', 'Jest'),
program = bin('jest', 'node_modules/jest/bin/jest.js'),
-- exact path, not a regex: [slug].test.tsx must match itself
args = function()
return { '--runInBand', '--watchAll=false', '--runTestsByPath', M.real_file() }
end,
sourceMaps = true,
skipFiles = skip,
console = 'integratedTerminal',
},
{
name = 'Playwright: current file',
type = 'pwa-node',
request = 'launch',
cwd = cwd_of('playwright', 'node_modules/@playwright/test/cli.js', 'Playwright'),
program = bin('playwright', 'node_modules/@playwright/test/cli.js'),
-- no test timeout while paused at a breakpoint
args = function()
return { 'test', M.regex_escape(M.real_file()), '--workers=1', '--timeout=0' }
end,
skipFiles = skip,
console = 'integratedTerminal',
},
{
-- VS Code's 'node-terminal' type only exists inside VS Code; standalone js-debug
-- debugs the same way through a launch: every Node child of the script is attached
name = 'Next.js: server (dev script)',
type = 'pwa-node',
request = 'launch',
cwd = M.package_root,
runtimeExecutable = M.package_manager,
runtimeArgs = { 'run', 'dev' },
autoAttachChildProcesses = true,
sourceMaps = true,
skipFiles = skip,
console = 'integratedTerminal',
},
{
name = 'Next.js: client in Chrome (:3000)',
type = 'pwa-chrome',
request = 'launch',
url = 'http://localhost:3000',
webRoot = M.package_root,
},
{
name = 'Vite: client in Chrome (:5173)',
type = 'pwa-chrome',
request = 'launch',
url = 'http://localhost:5173',
webRoot = M.package_root,
},
vim.tbl_extend('force', electron_main, { name = 'Electron: main process' }),
M.electron_renderer,
vim.tbl_extend('force', electron_main, { name = M.electron_both }),
}
end
---Call `cb` once something listens on `port` (or give up after `timeout_ms`).
---Checks both loopbacks: Vite and other dev servers often bind only to ::1.
---@param port integer
---@param timeout_ms integer
---@param cb fun(ok: boolean)
function M.wait_for_port(port, timeout_ms, cb)
local deadline = vim.uv.now() + timeout_ms
local function try()
local hosts = { '127.0.0.1', '::1' }
local pending, done = #hosts, false
local function settle(ok)
pending = pending - 1
if done then
return
elseif ok then
done = true
vim.schedule(function()
cb(true)
end)
elseif pending == 0 then
if vim.uv.now() > deadline then
vim.schedule(function()
cb(false)
end)
else
vim.defer_fn(try, 500)
end
end
end
for _, host in ipairs(hosts) do
local tcp = assert(vim.uv.new_tcp())
local started = tcp:connect(host, port, function(err)
tcp:close()
settle(not err)
end)
if not started then
tcp:close()
settle(false)
end
end
end
try()
end
return M