Skip to content

avoid being tricked that Build.zig is build.zig #23984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
34 changes: 22 additions & 12 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7255,10 +7255,10 @@ fn findBuildRoot(arena: Allocator, options: FindBuildRootOptions) !BuildRoot {
while (true) {
const joined_path = try fs.path.join(arena, &[_][]const u8{ dirname, build_zig_basename });
if (fs.cwd().access(joined_path, .{})) |_| {
const dir = fs.cwd().openDir(dirname, .{}) catch |err| {
const dir = fs.cwd().openDir(dirname, .{ .iterate = true }) catch |err| {
fatal("unable to open directory while searching for build.zig file, '{s}': {s}", .{ dirname, @errorName(err) });
};
return .{
if (try caseMatches(dir, build_zig_basename)) return .{
.build_zig_basename = build_zig_basename,
.directory = .{
.path = dirname,
Expand All @@ -7267,19 +7267,29 @@ fn findBuildRoot(arena: Allocator, options: FindBuildRootOptions) !BuildRoot {
.cleanup_build_dir = dir,
};
} else |err| switch (err) {
error.FileNotFound => {
dirname = fs.path.dirname(dirname) orelse {
std.log.info("initialize {s} template file with 'zig init'", .{
Package.build_zig_basename,
});
std.log.info("see 'zig --help' for more options", .{});
fatal("no build.zig file found, in the current directory or any parent directories", .{});
};
continue;
},
error.FileNotFound => {},
else => |e| return e,
}
dirname = fs.path.dirname(dirname) orelse {
std.log.info("initialize {s} template file with 'zig init'", .{
Package.build_zig_basename,
});
std.log.info("see 'zig --help' for more options", .{});
fatal("no build.zig file found, in the current directory or any parent directories", .{});
};
}
}

fn caseMatches(iterable_dir: std.fs.Dir, name: []const u8) !bool {
// TODO: maybe there is more efficient platform-specific mechanisms to implement this?
var iterator = iterable_dir.iterate();
var found_case_insensitive_match = false;
while (try iterator.next()) |entry| {
if (std.mem.eql(u8, entry.name, name)) return true;
found_case_insensitive_match = found_case_insensitive_match or std.ascii.eqlIgnoreCase(entry.name, name);
}
if (!found_case_insensitive_match) return error.FileNotFound;
return false;
}

const LoadManifestOptions = struct {
Expand Down
3 changes: 3 additions & 0 deletions test/standalone/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@
.config_header = .{
.path = "config_header",
},
.case_insensitive = .{
.path = "case_insensitive",
},
},
.paths = .{
"build.zig",
Expand Down
27 changes: 27 additions & 0 deletions test/standalone/case_insensitive/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
pub fn build(b: *std.Build) !void {
const run_zig_build = b.addSystemCommand(&[_][]const u8{
b.graph.zig_exe,
"build",
"printfoo",
});
run_zig_build.setCwd(b.path("capital"));
run_zig_build.expectStdOutEqual("Foo\n");
b.default_step.dependOn(&run_zig_build.step);

const printfoo = try b.allocator.create(std.Build.Step);
printfoo.* = std.Build.Step.init(.{
.id = .custom,
.name = "printfoo",
.owner = b,
.makeFn = printFoo,
});
b.step("printfoo", "").dependOn(printfoo);
}

fn printFoo(step: *std.Build.Step, options: std.Build.Step.MakeOptions) anyerror!void {
_ = step;
_ = options;
try std.io.getStdOut().writer().writeAll("Foo\n");
}

const std = @import("std");
3 changes: 3 additions & 0 deletions test/standalone/case_insensitive/capital/Build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
comptime {
@compileError("Don't be tricked into thinking this is build.zig, it's Build.zig!");
}
Loading