Skip to content
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
12 changes: 10 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,13 @@ jobs:
if: matrix.os == 'ubuntu-22.04'
run: zig fmt --check .

- name: Run Tests
run: zig build test --summary all
- name: Run zig build check test
run: zig build check test --summary all

- name: Setup wasmtime
if: matrix.os == 'ubuntu-22.04'
uses: bytecodealliance/actions/wasmtime/setup@v1

- name: Run zig build check test -Dtarget=wasm32-
if: matrix.os == 'ubuntu-22.04'
run: zig build check test -Dtarget=wasm32-wasi -fwasmtime --summary all
21 changes: 19 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,20 @@ pub fn build(b: *Build) !void {
.use_lld = use_llvm,
});

if (target.result.cpu.arch.isWasm() and b.enable_wasmtime) {
// Zig's build system integration with wasmtime does not support adding custom preopen directories so it is done manually.
const args: []const ?[]const u8 = &.{
"wasmtime",
"--dir=.",
b.fmt("--dir={}::/lib", .{b.graph.zig_lib_directory}),
b.fmt("--dir={s}::/cache", .{b.cache_root.join(b.allocator, &.{"zls"}) catch @panic("OOM")}),
"--",
null,
};
tests.setExecCmd(args);
src_tests.setExecCmd(args);
}

blk: { // zig build test, zig build test-build-runner, zig build test-analysis
const test_step = b.step("test", "Run all the tests");
const test_build_runner_step = b.step("test-build-runner", "Run all the build runner tests");
Expand All @@ -268,16 +282,19 @@ pub fn build(b: *Build) !void {

// Create run steps
@import("tests/add_build_runner_cases.zig").addCases(b, test_build_runner_step, test_filters, build_runner);
@import("tests/add_analysis_cases.zig").addCases(b, test_analysis_step, test_filters);
@import("tests/add_analysis_cases.zig").addCases(b, target, optimize, test_analysis_step, test_filters);

const run_tests = b.addRunArtifact(tests);
const run_src_tests = b.addRunArtifact(src_tests);

run_tests.skip_foreign_checks = target.result.cpu.arch.isWasm() and b.enable_wasmtime;
run_src_tests.skip_foreign_checks = target.result.cpu.arch.isWasm() and b.enable_wasmtime;

// Setup dependencies of `zig build test`
test_step.dependOn(&run_tests.step);
test_step.dependOn(&run_src_tests.step);
test_step.dependOn(test_build_runner_step);
test_step.dependOn(test_analysis_step);
if (target.query.eql(b.graph.host.query)) test_step.dependOn(test_build_runner_step);

if (!coverage) break :blk;

Expand Down
70 changes: 47 additions & 23 deletions src/DocumentStore.zig
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,18 @@ pub fn computeHash(bytes: []const u8) Hash {

pub const Config = struct {
zig_exe_path: ?[]const u8,
zig_lib_path: ?[]const u8,
zig_lib_dir: ?std.Build.Cache.Directory,
build_runner_path: ?[]const u8,
builtin_path: ?[]const u8,
global_cache_path: ?[]const u8,

pub fn fromMainConfig(config: @import("Config.zig")) Config {
return .{
.zig_exe_path = config.zig_exe_path,
.zig_lib_path = config.zig_lib_path,
.build_runner_path = config.build_runner_path,
.builtin_path = config.builtin_path,
.global_cache_path = config.global_cache_path,
};
}
global_cache_dir: ?std.Build.Cache.Directory,

pub const init: Config = .{
.zig_exe_path = null,
.zig_lib_dir = null,
.build_runner_path = null,
.builtin_path = null,
.global_cache_dir = null,
};
};

/// Represents a `build.zig`
Expand Down Expand Up @@ -672,9 +670,30 @@ pub fn getOrLoadHandle(self: *DocumentStore, uri: Uri) ?*Handle {
log.err("file path is not absolute '{s}'", .{file_path});
return null;
}
const file_contents = std.fs.cwd().readFileAllocOptions(

const dir, const sub_path = blk: {
if (builtin.target.cpu.arch.isWasm() and !builtin.link_libc) {
// look up whether the file path refers to a preopen directory.
for ([_]?std.Build.Cache.Directory{
self.config.zig_lib_dir,
self.config.global_cache_dir,
}) |opt_preopen_dir| {
const preopen_dir = opt_preopen_dir orelse continue;
const preopen_path = preopen_dir.path.?;
std.debug.assert(std.mem.eql(u8, preopen_path, "/lib") or std.mem.eql(u8, preopen_path, "/cache"));

if (!std.mem.startsWith(u8, file_path, preopen_path)) continue;
if (!std.mem.startsWith(u8, file_path[preopen_path.len..], "/")) continue;

break :blk .{ preopen_dir.handle, file_path[preopen_path.len + 1 ..] };
}
}
break :blk .{ std.fs.cwd(), file_path };
};

const file_contents = dir.readFileAllocOptions(
self.allocator,
file_path,
sub_path,
max_document_size,
null,
.of(u8),
Expand Down Expand Up @@ -843,8 +862,8 @@ pub fn invalidateBuildFile(self: *DocumentStore, build_file_uri: Uri) void {

if (self.config.zig_exe_path == null) return;
if (self.config.build_runner_path == null) return;
if (self.config.global_cache_path == null) return;
if (self.config.zig_lib_path == null) return;
if (self.config.global_cache_dir == null) return;
if (self.config.zig_lib_dir == null) return;

if (builtin.single_threaded) {
self.invalidateBuildFileWorker(build_file_uri, false);
Expand Down Expand Up @@ -1156,7 +1175,12 @@ fn prepareBuildRunnerArgs(self: *DocumentStore, build_file_uri: []const u8) ![][
defer tracy_zone.end();

const base_args = &[_][]const u8{
self.config.zig_exe_path.?, "build", "--build-runner", self.config.build_runner_path.?, "--zig-lib-dir", self.config.zig_lib_path.?,
self.config.zig_exe_path.?,
"build",
"--build-runner",
self.config.build_runner_path.?,
"--zig-lib-dir",
self.config.zig_lib_dir.?.path orelse ".",
};

var args: std.ArrayListUnmanaged([]const u8) = try .initCapacity(self.allocator, base_args.len);
Expand Down Expand Up @@ -1189,8 +1213,8 @@ fn loadBuildConfiguration(self: *DocumentStore, build_file_uri: Uri) !std.json.P

std.debug.assert(self.config.zig_exe_path != null);
std.debug.assert(self.config.build_runner_path != null);
std.debug.assert(self.config.global_cache_path != null);
std.debug.assert(self.config.zig_lib_path != null);
std.debug.assert(self.config.global_cache_dir != null);
std.debug.assert(self.config.zig_lib_dir != null);

const build_file_path = try URI.parse(self.allocator, build_file_uri);
defer self.allocator.free(build_file_path);
Expand Down Expand Up @@ -1678,8 +1702,8 @@ pub fn resolveCImport(self: *DocumentStore, handle: *Handle, node: Ast.Node.Inde

if (!std.process.can_spawn) return null;
if (self.config.zig_exe_path == null) return null;
if (self.config.zig_lib_path == null) return null;
if (self.config.global_cache_path == null) return null;
if (self.config.zig_lib_dir == null) return null;
if (self.config.global_cache_dir == null) return null;

// TODO regenerate cimports if the header files gets modified

Expand Down Expand Up @@ -1838,9 +1862,9 @@ pub fn uriFromImportStr(self: *DocumentStore, allocator: std.mem.Allocator, hand
defer tracy_zone.end();

if (std.mem.eql(u8, import_str, "std")) {
const zig_lib_path = self.config.zig_lib_path orelse return null;
const zig_lib_dir = self.config.zig_lib_dir orelse return null;

const std_path = try std.fs.path.join(allocator, &.{ zig_lib_path, "std", "std.zig" });
const std_path = try zig_lib_dir.join(allocator, &.{ "std", "std.zig" });
defer allocator.free(std_path);

return try URI.fromPath(allocator, std_path);
Expand Down
Loading
Loading