Skip to content
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

Scripts: Use ZIG_EXE environment variable #1909

Merged
merged 3 commits into from
May 6, 2024
Merged

Conversation

sentientwaffle
Copy link
Member

I use (many) git worktrees, and rather than giving each their own zig binary, I install it once in my PATH, so ./zig/zig doesn't exist.
ZIG_EXE is compatible with both workflows.

(ZIG_EXE is set automatically for zig run ... – but not zig build ... with Run steps, so it needs to be explicitly set in build.zig.)

I use (many) git worktrees, and rather than giving each their own zig binary, I install it once in my PATH, so `./zig/zig` doesn't exist.
`ZIG_EXE` is compatible with both workflows.

(`ZIG_EXE` is set automatically for `zig run ...`  – but not `zig build ...` with Run steps, so it needs to be explicitly set in `build.zig`.)
@sentientwaffle sentientwaffle marked this pull request as ready for review May 3, 2024 20:40
Copy link
Member

@matklad matklad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

In general, I am a firm believer that it's better to say "this is the single configuration our project uses" rather than to try to support many different ways to do things. (and, for my worktrees, I actuall have a ./zig/zig symlink in each one of them).

However we don't actually make this more configurable really, we are just extracting a hard-coded constant into a variable, which is an improvement just along the readability axis!

LGTM with zig_exe_alloc replaced with a field --- I don't think we should have any "caller manages the memory" functions in shell.zig, it should figure out memory itself

src/shell.zig Outdated
@@ -596,12 +596,33 @@ pub fn spawn_options(
return child;
}

pub fn zig_exe_alloc(shell: Shell, allocator: std.mem.Allocator) ![]const u8 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For simplicity, let's rather do this during Shell.create and store the result as a field, like how we do for project_root.

I debated whether we should keep shell.zig as an independent re-usable utility, or whether to allow dependency on TigerBeetle repo specifically, and I figured that a bounded amount of dependency is probably better.k

As a bonus, this'll tell us eagarly if we miss the environmental variable in our build.zig!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that is much better! We even already have the EnvMap in Shell.create, which simplifies the error handling.

Though, we do miss the environment variable often -- e.g. build.zig itself uses Shell. It just doesn't end up being needed in those cases.

@@ -143,7 +144,7 @@ fn run_fuzzers(

const random = std.crypto.random;

try shell.exec("../zig/zig build -Drelease build_fuzz", .{});
try shell.zig("build -Drelease build_fuzz", .{});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I actually forgot that we have shell.zig!

@@ -186,8 +187,12 @@ fn run_fuzzers(
log.debug("will start '{s}'", .{command});
const child = try shell.spawn_options(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but, sadly, we can't use it here, because we need a child...

It's on my todo list to re-visint shell.zig and try to come up with somewhat more orthogonal set of APIs. I like using shell.zig, but the implementaiton is almost 1000 lines of mostly copy-paste :(

@@ -97,7 +97,7 @@ fn validate_release(shell: *Shell, gpa: std.mem.Allocator, language_requested: ?
.{},
);
const tag = stdx.cut(release_info, "\t").?.prefix;
log.info("validateing release {s}", .{tag});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

src/shell.zig Outdated
// ZIG_EXE is set in `build.zig`, so it is available when using the run step for `zig build
// scripts`.
// ZIG_EXE is already an absolute path, but zig/zig is not.
const zig_exe_default = comptime "zig/zig" ++ builtin.target.exeFileExt();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, let's assume that it's just always set!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a bonus to this -- b.zig_exe (in build.zig) is already an absolute path!

@matklad matklad self-assigned this May 6, 2024
Unfortunately `zig_exe` is optional – we can't assert that the `ZIG_EXE` environment variable is set.
In particular, `build.zig` itself uses `Shell`.
@sentientwaffle sentientwaffle added this pull request to the merge queue May 6, 2024
Merged via the queue into main with commit 2bdad97 May 6, 2024
25 checks passed
@sentientwaffle sentientwaffle deleted the dj-ci-zig-exe-env branch May 6, 2024 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants