zig icon indicating copy to clipboard operation
zig copied to clipboard

std.zig.system.NativeTargetInfo: look for a shebang line in /usr/bin/env, if any

Open BratishkaErik opened this issue 3 years ago • 4 comments
trafficstars

UPD: See https://github.com/ziglang/zig/pull/12151#pullrequestreview-1044192217

BratishkaErik avatar Jul 17 '22 15:07 BratishkaErik

On which system do you observe /usr/bin/env not being an ELF file? And what is it instead?

andrewrk avatar Jul 19 '22 18:07 andrewrk

On which system do you observe /usr/bin/env not being an ELF file? And what is it instead?

https://github.com/ziglang/zig/pull/12151#discussion_r923210625

It can be a script instead:

#!/usr/bin/coreutils --coreutils-prog-shebang=env

or similar.

BratishkaErik avatar Jul 19 '22 21:07 BratishkaErik

I think you're hitting a stage1 compiler bug. This diff seems to get things working again:

index c5b9454c9..d9467fe2a 100644
--- a/lib/std/zig/system/NativeTargetInfo.zig
+++ b/lib/std/zig/system/NativeTargetInfo.zig
@@ -390,19 +390,16 @@ fn detectAbiAndDynamicLinker(

                 else => |e| return e,
             };
-            var should_close = true;
-            defer if (should_close) file.close();

             const line = file.reader().readUntilDelimiter(&buffer, '\n') catch {
-                should_close = false;
                 break :blk file;
             };
             if (mem.startsWith(u8, line, "#!")) {
                 var it = std.mem.tokenize(u8, line[2..], " ");
                 file_name = it.next() orelse return defaultAbiAndDynamicLinker(cpu, os, cross_target);
+                file.close();
                 continue;
             } else {
-                should_close = false;
                 break :blk file;
             }
         }

topolarity avatar Jul 27 '22 20:07 topolarity

I think you're hitting a stage1 compiler bug. This diff seems to get things working again:

index c5b9454c9..d9467fe2a 100644
--- a/lib/std/zig/system/NativeTargetInfo.zig
+++ b/lib/std/zig/system/NativeTargetInfo.zig
@@ -390,19 +390,16 @@ fn detectAbiAndDynamicLinker(

                 else => |e| return e,
             };
-            var should_close = true;
-            defer if (should_close) file.close();

             const line = file.reader().readUntilDelimiter(&buffer, '\n') catch {
-                should_close = false;
                 break :blk file;
             };
             if (mem.startsWith(u8, line, "#!")) {
                 var it = std.mem.tokenize(u8, line[2..], " ");
                 file_name = it.next() orelse return defaultAbiAndDynamicLinker(cpu, os, cross_target);
+                file.close();
                 continue;
             } else {
-                should_close = false;
                 break :blk file;
             }
         }

Thank you!

BratishkaErik avatar Jul 27 '22 20:07 BratishkaErik

Thank you for your helpful reviews! :)

BratishkaErik avatar Aug 18 '22 17:08 BratishkaErik