Skip to content
6 changes: 3 additions & 3 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ const zls_version = std.SemanticVersion.parse(@import("build.zig.zon").version)
const minimum_build_zig_version = @import("build.zig.zon").minimum_zig_version;

/// Specify the minimum Zig version that is usable with ZLS:
/// std.Io.Threaded: performance enhancements, bugfixes, and better Windows and NetBSD support
/// std: delete `os.environ`, `os.argv`, add new parameter to `main`, move process API to `std.Io`
///
/// A breaking change to the Zig Build System should be handled by updating ZLS's build runner (see src\build_runner)
const minimum_runtime_zig_version = "0.16.0-dev.1910+8226d706e";
const minimum_runtime_zig_version = "0.16.0-dev.1974+de25a6ffe";

const release_targets = [_]std.Target.Query{
.{ .cpu_arch = .aarch64, .os_tag = .linux },
Expand Down Expand Up @@ -345,7 +345,7 @@ fn getVersion(b: *Build) std.SemanticVersion {
"git", "-C", b.pathFromRoot("."), "--git-dir", ".git", "describe", "--match", "*.*.*", "--tags",
};
var code: u8 = undefined;
const git_describe_untrimmed = b.runAllowFail(argv, &code, .Ignore) catch |err| {
const git_describe_untrimmed = b.runAllowFail(argv, &code, .ignore) catch |err| {
const argv_joined = std.mem.join(b.allocator, " ", argv) catch @panic("OOM");
std.log.warn(
\\Failed to run git describe to resolve ZLS version: {}
Expand Down
10 changes: 5 additions & 5 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// nix flake update --commit-lock-file
// ```
// If you do not use Nix, a ZLS maintainer can take care of this.
.minimum_zig_version = "0.16.0-dev.1912+0cbaaa5eb",
.minimum_zig_version = "0.16.0-dev.1976+8e091047b",
// If you do not use Nix, a ZLS maintainer can take care of this.
// Whenever the dependencies are updated, run the following command:
// ```bash
Expand All @@ -19,16 +19,16 @@
// The `deps.nix` should also be updated to not fetch `tracy`.
.dependencies = .{
.known_folders = .{
.url = "https://github.com/ziglibs/known-folders/archive/ffe1e44e1e7fa5dcac0ce5da1cd91e52cf054107.tar.gz",
.hash = "known_folders-0.0.0-Fy-PJoPNAACH-fVI5tF-Z9GpkQke98LCVU01rxv09Kwy",
.url = "https://github.com/ziglibs/known-folders/archive/84bfe726c8f94bb8e90ac2e96a6949b794a185a0.tar.gz",
.hash = "known_folders-0.0.0-Fy-PJs_KAAAI2L9hC8gAY7Zwc_-QkeC6nN2n5xuSOaUQ",
},
.diffz = .{
.url = "https://github.com/ziglibs/diffz/archive/669e6ed7470100bfd9d2aa9f6f96b93c45996179.tar.gz",
.hash = "diffz-0.0.1-G2tlISLPAQDzXkUIRWWUfdgIsIb1dAyVwRPXMt5kRpui",
},
.lsp_kit = .{
.url = "https://github.com/zigtools/lsp-kit/archive/1d4a41092286029f541082fe15fc45f0bbf1d210.tar.gz",
.hash = "lsp_kit-0.1.0-bi_PL7stDADM03s6vUGZPk4GsWNC7BruKNsrn3ukhFI_",
.url = "https://github.com/zigtools/lsp-kit/archive/f34a177f3a9bd7c0328c468889316868b884e84e.tar.gz",
.hash = "lsp_kit-0.1.0-bi_PL8gtDADzuOHLTfjrx-LNFVzWF5GIx-lMKjWMjQAU",
},
.tracy = .{
.url = "https://github.com/wolfpld/tracy/archive/refs/tags/v0.13.1.tar.gz",
Expand Down
12 changes: 6 additions & 6 deletions deps.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ linkFarm "zig-packages" [
};
}
{
name = "known_folders-0.0.0-Fy-PJoPNAACH-fVI5tF-Z9GpkQke98LCVU01rxv09Kwy";
name = "known_folders-0.0.0-Fy-PJs_KAAAI2L9hC8gAY7Zwc_-QkeC6nN2n5xuSOaUQ";
path = fetchzip {
url = "https://github.com/ziglibs/known-folders/archive/ffe1e44e1e7fa5dcac0ce5da1cd91e52cf054107.tar.gz";
hash = "sha256-PJZXupqVmzDKWX07f0h6Z7x4d0U2pXCNHlASsIj8ssA=";
url = "https://github.com/ziglibs/known-folders/archive/84bfe726c8f94bb8e90ac2e96a6949b794a185a0.tar.gz";
hash = "sha256-KB2zvnQPF4Lk9oGNModmqdWhHZBSO2VIpsbhj8/5FxA=";
};
}
{
name = "lsp_kit-0.1.0-bi_PL7stDADM03s6vUGZPk4GsWNC7BruKNsrn3ukhFI_";
name = "lsp_kit-0.1.0-bi_PL8gtDADzuOHLTfjrx-LNFVzWF5GIx-lMKjWMjQAU";
path = fetchzip {
url = "https://github.com/zigtools/lsp-kit/archive/1d4a41092286029f541082fe15fc45f0bbf1d210.tar.gz";
hash = "sha256-ko8SvkpOt1H+9DDDVzrRZdiZRXcG4TjK9D/78PL0Lk0=";
url = "https://github.com/zigtools/lsp-kit/archive/f34a177f3a9bd7c0328c468889316868b884e84e.tar.gz";
hash = "sha256-97Qf3v3v6jwwxfFsgdgkKQtI9TAd8BYQuq4r5v5GTLo=";
};
}
]
12 changes: 6 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions src/DocumentStore.zig
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub fn computeHash(bytes: []const u8) Hash {
}

pub const Config = struct {
environ_map: *const std.process.Environ.Map,
zig_exe_path: ?[]const u8,
zig_lib_dir: ?std.Build.Cache.Directory,
build_runner_path: ?[]const u8,
Expand Down Expand Up @@ -1108,7 +1109,7 @@ fn loadBuildConfiguration(self: *DocumentStore, build_file_uri: Uri, build_file_
const zig_run_result = blk: {
const tracy_zone2 = tracy.trace(@src());
defer tracy_zone2.end();
break :blk try std.process.Child.run(
break :blk try std.process.run(
self.allocator,
self.io,
.{
Expand All @@ -1122,7 +1123,7 @@ fn loadBuildConfiguration(self: *DocumentStore, build_file_uri: Uri, build_file_
defer self.allocator.free(zig_run_result.stderr);

const is_ok = switch (zig_run_result.term) {
.Exited => |exit_code| exit_code == 0,
.exited => |exit_code| exit_code == 0,
else => false,
};

Expand Down Expand Up @@ -1495,7 +1496,8 @@ pub fn collectIncludeDirs(
.ofmt = comptime std.Target.ObjectFormat.default(builtin.os.tag, builtin.cpu.arch),
.dynamic_linker = std.Target.DynamicLinker.none,
};
const native_paths: std.zig.system.NativePaths = try .detect(arena_allocator.allocator(), store.io, &target_info);
const arena_allocator_allocator = arena_allocator.allocator();
const native_paths: std.zig.system.NativePaths = try .detect(arena_allocator_allocator, store.io, &target_info, @constCast(store.config.environ_map));

try include_dirs.ensureUnusedCapacity(allocator, native_paths.include_dirs.items.len);
for (native_paths.include_dirs.items) |native_include_dir| {
Expand Down
1 change: 1 addition & 0 deletions src/Server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,7 @@ pub fn resolveConfiguration(server: *Server) error{OutOfMemory}!void {

fn createDocumentStoreConfig(config_manager: *const configuration.Manager) DocumentStore.Config {
return .{
.environ_map = config_manager.environ_map,
.zig_exe_path = config_manager.config.zig_exe_path,
.zig_lib_dir = config_manager.zig_lib_dir,
.build_runner_path = config_manager.config.build_runner_path,
Expand Down
22 changes: 13 additions & 9 deletions src/build_runner/build_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub const std_options: std.Options = .{

///! This is a modified build runner to extract information out of build.zig
///! Modified version of lib/build_runner.zig
pub fn main() !void {
pub fn main(init: process.Init.Minimal) !void {
// Here we use an ArenaAllocator backed by a DirectAllocator because a build is a short-lived,
// one shot program. We don't need to waste time freeing memory and finding places to squish
// bytes into. So we free everything all at once at the very end.
Expand All @@ -48,9 +48,12 @@ pub fn main() !void {
};
const arena = thread_safe_arena.allocator();

const args = try process.argsAlloc(arena);
const args = try init.args.toSlice(arena);

var threaded: std.Io.Threaded = .init(arena, .{});
var threaded: std.Io.Threaded = .init(arena, .{
.environ = init.environ,
.argv0 = .init(init.args),
});
defer threaded.deinit();
const io = threaded.ioBasic();

Expand Down Expand Up @@ -92,9 +95,10 @@ pub fn main() !void {
.io = io,
.gpa = arena,
.manifest_dir = try local_cache_directory.handle.createDirPathOpen(io, "h", .{}),
.cwd = try process.getCwdAlloc(single_threaded_arena.allocator()),
},
.zig_exe = zig_exe,
.env_map = try process.getEnvMap(arena),
.environ_map = try init.environ.createMap(arena),
.global_cache_root = global_cache_directory,
.zig_lib_directory = zig_lib_directory,
.host = .{
Expand Down Expand Up @@ -388,7 +392,7 @@ pub fn main() !void {
return;
}

var w = try Watch.init(io);
var w = try Watch.init(io, graph.cache.cwd);

const message_thread = try std.Thread.spawn(.{}, struct {
fn do(ww: *Watch) void {
Expand Down Expand Up @@ -475,10 +479,10 @@ const Watch = struct {
manual_event: std.Io.Event,
steps: []const *Step,

fn init(io: std.Io) !Watch {
fn init(io: std.Io, cwd_path: []const u8) !Watch {
return .{
.io = io,
.fs_watch = if (@TypeOf(std.Build.Watch) != void) try std.Build.Watch.init() else {},
.fs_watch = if (@TypeOf(std.Build.Watch) != void) try std.Build.Watch.init(cwd_path) else {},
.supports_fs_watch = @TypeOf(std.Build.Watch) != void and shared.BuildOnSaveSupport.isSupportedRuntime(builtin.zig_version) == .supported,
.manual_event = .unset,
.steps = &.{},
Expand Down Expand Up @@ -1395,7 +1399,7 @@ const copied_from_zig = struct {
pkg_name,
"--cflags",
"--libs",
}, &code, .Ignore)) |stdout| stdout else |err| switch (err) {
}, &code, .ignore)) |stdout| stdout else |err| switch (err) {
error.ProcessTerminated => return error.PkgConfigCrashed,
error.ExecNotSupported => return error.PkgConfigFailed,
error.ExitCodeFailure => return error.PkgConfigFailed,
Expand Down Expand Up @@ -1437,7 +1441,7 @@ const copied_from_zig = struct {
}

fn execPkgConfigList(self: *std.Build, out_code: *u8) (std.Build.PkgConfigError || std.Build.RunError)![]const std.Build.PkgConfigPkg {
const stdout = try self.runAllowFail(&.{ "pkg-config", "--list-all" }, out_code, .Ignore);
const stdout = try self.runAllowFail(&.{ "pkg-config", "--list-all" }, out_code, .ignore);
var list = ArrayListManaged(std.Build.PkgConfigPkg).init(self.allocator);
errdefer list.deinit();
var line_it = mem.tokenizeAny(u8, stdout, "\r\n");
Expand Down
42 changes: 15 additions & 27 deletions src/configuration.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const log = std.log.scoped(.config);
pub const Manager = struct {
io: std.Io,
allocator: std.mem.Allocator,
environ_map: *const std.process.Environ.Map,
config: Config,
zig_exe: ?struct {
/// Same as `Manager.config.zig_exe_path.?`
Expand Down Expand Up @@ -41,10 +42,11 @@ pub const Manager = struct {
arena: std.heap.ArenaAllocator.State,
},

pub fn init(io: std.Io, allocator: std.mem.Allocator) error{OutOfMemory}!Manager {
pub fn init(io: std.Io, allocator: std.mem.Allocator, environ_map: *const std.process.Environ.Map) error{OutOfMemory}!Manager {
return .{
.io = io,
.allocator = allocator,
.environ_map = environ_map,
.zig_exe = null,
.zig_lib_dir = null,
.global_cache_dir = null,
Expand Down Expand Up @@ -169,7 +171,7 @@ pub const Manager = struct {

if (config.zig_exe_path == null) blk: {
if (!std.process.can_spawn) break :blk;
const zig_exe_path = try findZig(io, manager.allocator) orelse break :blk;
const zig_exe_path = try findZig(io, manager.allocator, manager.environ_map) orelse break :blk;
defer manager.allocator.free(zig_exe_path);
config.zig_exe_path = try arena.dupe(u8, zig_exe_path);
}
Expand Down Expand Up @@ -320,7 +322,7 @@ pub const Manager = struct {
"--show-builtin",
};

const run_result = std.process.Child.run(
const run_result = std.process.run(
manager.allocator,
io,
.{
Expand Down Expand Up @@ -565,7 +567,7 @@ pub fn getZigEnv(
result_arena: std.mem.Allocator,
zig_exe_path: []const u8,
) error{OutOfMemory}!?Env {
const zig_env_result = std.process.Child.run(
const zig_env_result = std.process.run(
allocator,
io,
.{ .argv = &.{ zig_exe_path, "env" } },
Expand All @@ -580,7 +582,7 @@ pub fn getZigEnv(
}

switch (zig_env_result.term) {
.Exited => |code| {
.exited => |code| {
if (code != 0) {
log.err("zig env command exited with error code {d}.", .{code});
if (zig_env_result.stderr.len != 0) {
Expand All @@ -589,7 +591,7 @@ pub fn getZigEnv(
return null;
}
},
.Signal, .Stopped, .Unknown => {
.signal, .stopped, .unknown => {
log.err("zig env command terminated unexpectedly.", .{});
if (zig_env_result.stderr.len != 0) {
log.err("stderr: {s}", .{zig_env_result.stderr});
Expand Down Expand Up @@ -685,29 +687,15 @@ pub const DidConfigChange = @Struct(
&@splat(.{ .default_value_ptr = &false }),
);

pub fn findZig(io: std.Io, allocator: std.mem.Allocator) error{OutOfMemory}!?[]const u8 {
pub fn findZig(
io: std.Io,
allocator: std.mem.Allocator,
environ_map: *const std.process.Environ.Map,
) error{OutOfMemory}!?[]const u8 {
const is_windows = builtin.target.os.tag == .windows;

const env_path = std.process.getEnvVarOwned(allocator, "PATH") catch |err| switch (err) {
error.EnvironmentVariableNotFound => return null,
error.OutOfMemory => |e| return e,
error.InvalidWtf8 => |e| {
log.err("failed to load 'PATH' environment variable: {}", .{e});
return null;
},
};
defer allocator.free(env_path);

const env_path_ext = if (is_windows)
std.process.getEnvVarOwned(allocator, "PATHEXT") catch |err| switch (err) {
error.EnvironmentVariableNotFound => return null,
error.OutOfMemory => |e| return e,
error.InvalidWtf8 => |e| {
log.err("failed to load 'PATH' environment variable: {}", .{e});
return null;
},
};
defer if (is_windows) allocator.free(env_path_ext);
const env_path = environ_map.get("PATH") orelse return null;
const env_path_ext = if (is_windows) environ_map.get("PATHEXT") orelse return null;

var filename_buffer: std.ArrayList(u8) = .empty;
defer filename_buffer.deinit(allocator);
Expand Down
Loading