@@ -5,6 +5,7 @@ const Ast = std.zig.Ast;
55const log = std .log .scoped (.zls_diag );
66
77const Server = @import ("../Server.zig" );
8+ const Config = @import ("../Config.zig" );
89const DocumentStore = @import ("../DocumentStore.zig" );
910const types = @import ("lsp" ).types ;
1011const Analyser = @import ("../analysis.zig" );
@@ -191,7 +192,9 @@ pub fn generateDiagnostics(server: *Server, arena: std.mem.Allocator, handle: *D
191192}
192193
193194pub fn generateBuildOnSaveDiagnostics (
194- server : * Server ,
195+ allocator : std.mem.Allocator ,
196+ store : * DocumentStore ,
197+ config : Config ,
195198 workspace_uri : types.URI ,
196199 arena : std.mem.Allocator ,
197200 diagnostics : * std .StringArrayHashMapUnmanaged (std .ArrayListUnmanaged (types.Diagnostic )),
@@ -200,19 +203,19 @@ pub fn generateBuildOnSaveDiagnostics(
200203 defer tracy_zone .end ();
201204 comptime std .debug .assert (std .process .can_spawn );
202205
203- const zig_exe_path = server . config .zig_exe_path orelse return ;
204- const zig_lib_path = server . config .zig_lib_path orelse return ;
206+ const zig_exe_path = config .zig_exe_path orelse return ;
207+ const zig_lib_path = config .zig_lib_path orelse return ;
205208
206- const workspace_path = URI .parse (server . allocator , workspace_uri ) catch | err | {
209+ const workspace_path = URI .parse (allocator , workspace_uri ) catch | err | {
207210 log .err ("failed to parse invalid uri '{s}': {}" , .{ workspace_uri , err });
208211 return ;
209212 };
210- defer server . allocator .free (workspace_path );
213+ defer allocator .free (workspace_path );
211214
212215 std .debug .assert (std .fs .path .isAbsolute (workspace_path ));
213216
214- const build_zig_path = try std .fs .path .join (server . allocator , &.{ workspace_path , "build.zig" });
215- defer server . allocator .free (build_zig_path );
217+ const build_zig_path = try std .fs .path .join (allocator , &.{ workspace_path , "build.zig" });
218+ defer allocator .free (build_zig_path );
216219
217220 std .fs .accessAbsolute (build_zig_path , .{}) catch | err | switch (err ) {
218221 error .FileNotFound = > return ,
@@ -222,8 +225,8 @@ pub fn generateBuildOnSaveDiagnostics(
222225 },
223226 };
224227
225- const build_zig_uri = try URI .fromPath (server . allocator , build_zig_path );
226- defer server . allocator .free (build_zig_uri );
228+ const build_zig_uri = try URI .fromPath (allocator , build_zig_path );
229+ defer allocator .free (build_zig_uri );
227230
228231 const base_args = &[_ ][]const u8 {
229232 zig_exe_path ,
@@ -235,21 +238,21 @@ pub fn generateBuildOnSaveDiagnostics(
235238 "none" ,
236239 };
237240
238- var argv = try std .ArrayListUnmanaged ([]const u8 ).initCapacity (arena , base_args .len + server . config .build_on_save_args .len );
241+ var argv = try std .ArrayListUnmanaged ([]const u8 ).initCapacity (arena , base_args .len + config .build_on_save_args .len );
239242 defer argv .deinit (arena );
240243 argv .appendSliceAssumeCapacity (base_args );
241- argv .appendSliceAssumeCapacity (server . config .build_on_save_args );
244+ argv .appendSliceAssumeCapacity (config .build_on_save_args );
242245
243- const has_explicit_steps = for (server . config .build_on_save_args ) | extra_arg | {
246+ const has_explicit_steps = for (config .build_on_save_args ) | extra_arg | {
244247 if (! std .mem .startsWith (u8 , extra_arg , "-" )) break true ;
245248 } else false ;
246249
247250 var has_check_step : bool = false ;
248251
249252 blk : {
250- server . document_store .lock .lockShared ();
251- defer server . document_store .lock .unlockShared ();
252- const build_file = server . document_store . build_files .get (build_zig_uri ) orelse break :blk ;
253+ store .lock .lockShared ();
254+ defer store .lock .unlockShared ();
255+ const build_file = store . build_files .get (build_zig_path ) orelse break :blk ;
253256
254257 no_build_config : {
255258 const build_associated_config = build_file .build_associated_config orelse break :no_build_config ;
@@ -263,9 +266,9 @@ pub fn generateBuildOnSaveDiagnostics(
263266
264267 no_check : {
265268 if (has_explicit_steps ) break :no_check ;
266- const config = build_file .tryLockConfig () orelse break :no_check ;
269+ const build_config = build_file .tryLockConfig () orelse break :no_check ;
267270 defer build_file .unlockConfig ();
268- for (config .top_level_steps ) | tls | {
271+ for (build_config .top_level_steps ) | tls | {
269272 if (std .mem .eql (u8 , tls , "check" )) {
270273 has_check_step = true ;
271274 break ;
@@ -274,7 +277,7 @@ pub fn generateBuildOnSaveDiagnostics(
274277 }
275278 }
276279
277- if (! (server . config .enable_build_on_save orelse has_check_step )) {
280+ if (! (config .enable_build_on_save orelse has_check_step )) {
278281 return ;
279282 }
280283
@@ -283,30 +286,30 @@ pub fn generateBuildOnSaveDiagnostics(
283286 try argv .append (arena , "check" );
284287 }
285288
286- const extra_args_joined = try std .mem .join (server . allocator , " " , argv .items [base_args .len .. ]);
287- defer server . allocator .free (extra_args_joined );
289+ const extra_args_joined = try std .mem .join (allocator , " " , argv .items [base_args .len .. ]);
290+ defer allocator .free (extra_args_joined );
288291
289292 log .info ("Running build-on-save: {s} ({s})" , .{ build_zig_uri , extra_args_joined });
290293
291294 const result = std .process .Child .run (.{
292- .allocator = server . allocator ,
295+ .allocator = allocator ,
293296 .argv = argv .items ,
294297 .cwd = workspace_path ,
295298 .max_output_bytes = 1024 * 1024 ,
296299 }) catch | err | {
297- const joined = std .mem .join (server . allocator , " " , argv .items ) catch return ;
298- defer server . allocator .free (joined );
300+ const joined = std .mem .join (allocator , " " , argv .items ) catch return ;
301+ defer allocator .free (joined );
299302 log .err ("failed zig build command:\n {s}\n error:{}\n " , .{ joined , err });
300303 return err ;
301304 };
302- defer server . allocator .free (result .stdout );
303- defer server . allocator .free (result .stderr );
305+ defer allocator .free (result .stdout );
306+ defer allocator .free (result .stderr );
304307
305308 switch (result .term ) {
306309 .Exited = > | code | if (code == 0 ) return ,
307310 else = > {
308- const joined = std .mem .join (server . allocator , " " , argv .items ) catch return ;
309- defer server . allocator .free (joined );
311+ const joined = std .mem .join (allocator , " " , argv .items ) catch return ;
312+ defer allocator .free (joined );
310313 log .err ("failed zig build command:\n {s}\n stderr:{s}\n\n " , .{ joined , result .stderr });
311314 },
312315 }
0 commit comments