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

Avoid depending on child process execution when not supported by host OS #10782

Merged
merged 3 commits into from
Feb 7, 2022

Conversation

topolarity
Copy link
Contributor

In accordance with the requesting issue (#10750):

  • zig test skips any tests that it cannot spawn, returning success
  • zig run and zig build exit with failure, reporting the command the cannot be run
  • zig clang, zig ar, etc. already punt directly to the appropriate clang/lld main(), even before this change
  • Native libc Detection is not supported

Additionally, exec() and related Builder functions error at run-time, reporting the command that cannot be run

Copy link
Contributor

@matu3ba matu3ba left a comment

Choose a reason for hiding this comment

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

just a nit on can_spawn else looking good.

Did you check what is still missing?
Any of translate-c, ar, dlltool, lib, ranlib ?

@@ -936,6 +936,12 @@ pub const can_execv = switch (builtin.os.tag) {
else => true,
};

/// Tells whether spawning child processes is supported (e.g. via ChildProcess)
pub const can_spawn = switch (builtin.os.tag) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This will likely need to be extended to encompass other stuff, so add a TODO for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean that can_spawn will still need to be updated to be correct for other targets? If so, more than happy to add the TODO

Copy link
Member

Choose a reason for hiding this comment

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

Prefer to make the TODO in a github issue rather than a comment, please :-)

Copy link
Member

@andrewrk andrewrk left a comment

Choose a reason for hiding this comment

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

Thanks! Left you some review comments.

return self.spawnPosix();
} else {
unreachable;
Copy link
Member

Choose a reason for hiding this comment

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

I think this spawn function should be untouched, or if you want to make the compile error clearer, put at the top of it:

if (!std.process.can_spawn) {
    @compileError("the target operating system cannot spawn processes");
}

@@ -936,6 +936,12 @@ pub const can_execv = switch (builtin.os.tag) {
else => true,
};

/// Tells whether spawning child processes is supported (e.g. via ChildProcess)
pub const can_spawn = switch (builtin.os.tag) {
Copy link
Member

Choose a reason for hiding this comment

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

Prefer to make the TODO in a github issue rather than a comment, please :-)

build.zig Outdated
@@ -229,6 +229,11 @@ pub fn build(b: *Builder) !void {
const version = if (opt_version_string) |version| version else v: {
const version_string = b.fmt("{d}.{d}.{d}", .{ zig_version.major, zig_version.minor, zig_version.patch });

// If we can't spawn a process for git, just trust the version string
Copy link
Member

Choose a reason for hiding this comment

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

if we can't spawn a process for git, emit a fatal error here saying that it must be provided with -Dversion-string.

src/Compilation.zig Outdated Show resolved Hide resolved
src/main.zig Outdated Show resolved Hide resolved
src/main.zig Outdated
@@ -4081,7 +4093,7 @@ extern "c" fn ZigClang_main(argc: c_int, argv: [*:null]?[*:0]u8) c_int;
extern "c" fn ZigLlvmAr_main(argc: c_int, argv: [*:null]?[*:0]u8) c_int;

/// TODO https://github.com/ziglang/zig/issues/3257
fn punt_to_clang(arena: Allocator, args: []const []const u8) error{OutOfMemory} {
pub fn punt_to_clang(arena: Allocator, args: []const []const u8) error{OutOfMemory} {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think these should be made public or used outside this file

@topolarity
Copy link
Contributor Author

Thanks for the review, folks!

Did you check what is still missing? Any of translate-c, ar, dlltool, lib, ranlib ?

These are already covered without this change 👍 Zig calls directly into ZigLlvmAr_main

As for what's left: I thought I had covered all of the usage of ChildProcess with my most recent commit, but @andrewrk's request to make referencing spawn() a compile-time error (probably a good idea, I agree) is still triggering on an unknown reference I haven't been able to suss out yet.

I'll dive in deeper tomorrow and see if I can eliminate the lingering references to ChildProcess.spawn, and address the rest of your comments.

@andrewrk
Copy link
Member

andrewrk commented Feb 4, 2022

I'll dive in deeper tomorrow and see if I can eliminate the lingering references to ChildProcess.spawn, and address the rest of your comments.

This is a deficiency of zig compile errors; would be nice if it could explain how this dependency is being dragged in. Someday, we'll have it. For now, a workaround is to temporarily delete the spawn function from the std lib and then it tends to generate more helpful errors.

@topolarity topolarity force-pushed the gate-child-processes branch from 665a9c5 to b4056b7 Compare February 5, 2022 02:22
@topolarity topolarity requested a review from andrewrk February 5, 2022 02:23
@topolarity topolarity force-pushed the gate-child-processes branch 2 times, most recently from 3909724 to 3773f5e Compare February 5, 2022 02:30
@topolarity
Copy link
Contributor Author

This is a deficiency of zig compile errors; would be nice if it could explain how this dependency is being dragged in. Someday, we'll have it. For now, a workaround is to temporarily delete the spawn function from the std lib and then it tends to generate more helpful errors.

This tip was a huge help; ended up being short work to track things down.

I believe I've addressed all the feedback in the latest changes now. Let me know if anything still needs work 👍

@zigazeljko
Copy link
Contributor

Why is argvCmd needed? Doesn't std.mem.join do exactly the same?

@topolarity
Copy link
Contributor Author

Why is argvCmd needed? Doesn't std.mem.join do exactly the same?

Nice catch. I updated the PR to use std.mem.join

topolarity and others added 3 commits February 6, 2022 22:21
In accordance with the requesting issue (ziglang#10750):
- `zig test` skips any tests that it cannot spawn, returning success
- `zig run` and `zig build` exit with failure, reporting the command the cannot be run
- `zig clang`, `zig ar`, etc. already punt directly to the appropriate clang/lld main(), even before this change
- Native `libc` Detection is not supported

Additionally, `exec()` and related Builder functions error at run-time, reporting the command that cannot be run
and adjust the warning message for invoking LLD twice in the same
process.
@andrewrk andrewrk force-pushed the gate-child-processes branch from d174dee to 33fa296 Compare February 7, 2022 05:31
@andrewrk andrewrk merged commit 2113538 into ziglang:master Feb 7, 2022
@andrewrk
Copy link
Member

andrewrk commented Feb 7, 2022

Excellent work. Congrats on landing your first Zig PR 🎉

@topolarity topolarity deleted the gate-child-processes branch February 7, 2022 21:49
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.

4 participants