|
59 | 59 |
|
60 | 60 | ---@param args RADebuggableArgs |
61 | 61 | function M.start(args) |
62 | | - if not pcall(require, 'plenary.job') then |
63 | | - scheduled_error('plenary.nvim not found.') |
64 | | - return |
65 | | - end |
66 | | - |
67 | | - local Job = require('plenary.job') |
68 | | - |
69 | 62 | local cargo_args = get_cargo_args_from_runnables_args(args) |
70 | 63 |
|
71 | 64 | vim.notify('Compiling a debug build for debugging. This might take some time...') |
72 | 65 |
|
73 | | - Job |
74 | | - :new({ |
75 | | - command = 'cargo', |
76 | | - args = cargo_args, |
77 | | - cwd = args.workspaceRoot, |
78 | | - on_exit = function(j, code) |
79 | | - if code and code > 0 then |
80 | | - scheduled_error('An error occurred while compiling. Please fix all compilation issues and try again.') |
81 | | - return |
| 66 | + local cmd = vim.list_extend({ 'cargo' }, cargo_args) |
| 67 | + compat.system(cmd, { cwd = args.workspaceRoot }, function(sc) |
| 68 | + ---@cast sc vim.SystemCompleted |
| 69 | + local output = sc.stdout |
| 70 | + if sc.code ~= 0 or output == nil then |
| 71 | + scheduled_error( |
| 72 | + 'An error occurred while compiling. Please fix all compilation issues and try again' |
| 73 | + .. (sc.stderr and ': ' .. sc.stderr or '.') |
| 74 | + ) |
| 75 | + return |
| 76 | + end |
| 77 | + vim.schedule(function() |
| 78 | + local executables = {} |
| 79 | + for value in output:gmatch('([^\n]*)\n?') do |
| 80 | + local is_json, artifact = pcall(vim.fn.json_decode, value) |
| 81 | + if not is_json then |
| 82 | + goto loop_end |
82 | 83 | end |
83 | 84 |
|
84 | | - vim.schedule(function() |
85 | | - local executables = {} |
86 | | - |
87 | | - for _, value in pairs(j:result()) do |
88 | | - local artifact = vim.fn.json_decode(value) |
89 | | - |
90 | | - -- only process artifact if it's valid json object and it is a compiler artifact |
91 | | - if type(artifact) ~= 'table' or artifact.reason ~= 'compiler-artifact' then |
92 | | - goto loop_end |
93 | | - end |
94 | | - |
95 | | - local is_binary = compat.list_contains(artifact.target.crate_types, 'bin') |
96 | | - local is_build_script = compat.list_contains(artifact.target.kind, 'custom-build') |
97 | | - local is_test = ((artifact.profile.test == true) and (artifact.executable ~= nil)) |
98 | | - or compat.list_contains(artifact.target.kind, 'test') |
99 | | - -- only add executable to the list if we want a binary debug and it is a binary |
100 | | - -- or if we want a test debug and it is a test |
101 | | - if |
102 | | - (cargo_args[1] == 'build' and is_binary and not is_build_script) |
103 | | - or (cargo_args[1] == 'test' and is_test) |
104 | | - then |
105 | | - table.insert(executables, artifact.executable) |
106 | | - end |
107 | | - |
108 | | - ::loop_end:: |
109 | | - end |
110 | | - |
111 | | - -- only 1 executable is allowed for debugging - error out if zero or many were found |
112 | | - if #executables <= 0 then |
113 | | - scheduled_error('No compilation artifacts found.') |
114 | | - return |
115 | | - end |
116 | | - if #executables > 1 then |
117 | | - scheduled_error('Multiple compilation artifacts are not supported.') |
118 | | - return |
119 | | - end |
120 | | - |
121 | | - -- create debug configuration |
122 | | - local dap_config = { |
123 | | - name = 'Rust tools debug', |
124 | | - type = 'rt_lldb', |
125 | | - request = 'launch', |
126 | | - program = executables[1], |
127 | | - args = args.executableArgs or {}, |
128 | | - cwd = args.workspaceRoot, |
129 | | - stopOnEntry = false, |
130 | | - |
131 | | - -- if you change `runInTerminal` to true, you might need to change the yama/ptrace_scope setting: |
132 | | - -- |
133 | | - -- echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope |
134 | | - -- |
135 | | - -- Otherwise you might get the following error: |
136 | | - -- |
137 | | - -- Error on launch: Failed to attach to the target process |
138 | | - -- |
139 | | - -- But you should be aware of the implications: |
140 | | - -- https://www.kernel.org/doc/html/latest/admin-guide/LSM/Yama.html |
141 | | - runInTerminal = false, |
142 | | - } |
143 | | - -- start debugging |
144 | | - dap.run(dap_config) |
145 | | - end) |
146 | | - end, |
147 | | - }) |
148 | | - :start() |
| 85 | + -- only process artifact if it's valid json object and it is a compiler artifact |
| 86 | + if type(artifact) ~= 'table' or artifact.reason ~= 'compiler-artifact' then |
| 87 | + goto loop_end |
| 88 | + end |
| 89 | + |
| 90 | + local is_binary = compat.list_contains(artifact.target.crate_types, 'bin') |
| 91 | + local is_build_script = compat.list_contains(artifact.target.kind, 'custom-build') |
| 92 | + local is_test = ((artifact.profile.test == true) and (artifact.executable ~= nil)) |
| 93 | + or compat.list_contains(artifact.target.kind, 'test') |
| 94 | + -- only add executable to the list if we want a binary debug and it is a binary |
| 95 | + -- or if we want a test debug and it is a test |
| 96 | + if |
| 97 | + (cargo_args[1] == 'build' and is_binary and not is_build_script) |
| 98 | + or (cargo_args[1] == 'test' and is_test) |
| 99 | + then |
| 100 | + table.insert(executables, artifact.executable) |
| 101 | + end |
| 102 | + |
| 103 | + ::loop_end:: |
| 104 | + end |
| 105 | + -- only 1 executable is allowed for debugging - error out if zero or many were found |
| 106 | + if #executables <= 0 then |
| 107 | + scheduled_error('No compilation artifacts found.') |
| 108 | + return |
| 109 | + end |
| 110 | + if #executables > 1 then |
| 111 | + scheduled_error('Multiple compilation artifacts are not supported.') |
| 112 | + return |
| 113 | + end |
| 114 | + |
| 115 | + -- create debug configuration |
| 116 | + local dap_config = { |
| 117 | + name = 'Rust tools debug', |
| 118 | + type = 'rt_lldb', |
| 119 | + request = 'launch', |
| 120 | + program = executables[1], |
| 121 | + args = args.executableArgs or {}, |
| 122 | + cwd = args.workspaceRoot, |
| 123 | + stopOnEntry = false, |
| 124 | + |
| 125 | + -- if you change `runInTerminal` to true, you might need to change the yama/ptrace_scope setting: |
| 126 | + -- |
| 127 | + -- echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope |
| 128 | + -- |
| 129 | + -- Otherwise you might get the following error: |
| 130 | + -- |
| 131 | + -- Error on launch: Failed to attach to the target process |
| 132 | + -- |
| 133 | + -- But you should be aware of the implications: |
| 134 | + -- https://www.kernel.org/doc/html/latest/admin-guide/LSM/Yama.html |
| 135 | + runInTerminal = false, |
| 136 | + } |
| 137 | + -- start debugging |
| 138 | + dap.run(dap_config) |
| 139 | + end) |
| 140 | + end) |
149 | 141 | end |
150 | 142 |
|
151 | 143 | return M |
0 commit comments