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

stdin is NotOpenForReading when you run zig build run #12258

Closed
doongjohn opened this issue Jul 27, 2022 · 3 comments
Closed

stdin is NotOpenForReading when you run zig build run #12258

doongjohn opened this issue Jul 27, 2022 · 3 comments
Labels
bug Observed behavior contradicts documented or intended behavior os-windows
Milestone

Comments

@doongjohn
Copy link

Zig Version

0.10.0-dev.3315+1a16b7214

Steps to Reproduce

// main.zig
const std = @import("std");
const stdout = std.io.getStdOut().writer();
const stdin = std.io.getStdIn().reader();

pub fn main() !void {
    var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){};
    const galloc = general_purpose_allocator.allocator();
    defer _ = general_purpose_allocator.deinit();

    const input = try stdin.readUntilDelimiterAlloc(galloc, '\n', 1000);
    defer galloc.free(input);
    _ = try stdout.print("input = {s}\n", .{input});
}
// build.zig
const std = @import("std");

pub fn build(b: *std.build.Builder) void {
    // Standard target options allows the person running `zig build` to choose
    // what target to build for. Here we do not override the defaults, which
    // means any target is allowed, and the default is native. Other options
    // for restricting supported target set are available.
    const target = b.standardTargetOptions(.{});

    // Standard release options allow the person running `zig build` to select
    // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
    const mode = b.standardReleaseOptions();

    const exe = b.addExecutable("app", "src/main.zig");
    exe.setTarget(target);
    exe.setBuildMode(mode);
    exe.install();

    const run_cmd = exe.run();
    run_cmd.step.dependOn(b.getInstallStep());
    if (b.args) |args| {
        run_cmd.addArgs(args);
    }

    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);

    const exe_tests = b.addTest("src/main.zig");
    exe_tests.setTarget(target);
    exe_tests.setBuildMode(mode);

    const test_step = b.step("test", "Run unit tests");
    test_step.dependOn(&exe_tests.step);
}
zig build run

Expected Behavior

should be able to read from stdin

Actual Behavior

~/projects/exp/zigzig build run
error: NotOpenForReading
/home/username/zig/0.10.0-dev.3315+1a16b7214/files/lib/std/os.zig:650:22: 0x228fd5 in std.os.read (app)
            .BADF => return error.NotOpenForReading, // Can be a race condition.
                     ^
/home/username/zig/0.10.0-dev.3315+1a16b7214/files/lib/std/fs/file.zig:975:13: 0x228e31 in std.fs.file.File.read (app)
            return os.read(self.handle, buffer);
            ^
/home/username/zig/0.10.0-dev.3315+1a16b7214/files/lib/std/io/reader.zig:26:13: 0x22ca1f in std.io.reader.Reader(std.fs.file.File,std.os.ReadError,std.fs.file.File.read)
.read (app)
            return readFn(self.context, buffer);
            ^
/home/username/zig/0.10.0-dev.3315+1a16b7214/files/lib/std/io/reader.zig:232:30: 0x22c930 in std.io.reader.Reader(std.fs.file.File,std.os.ReadError,std.fs.file.File.read
).readByte (app)
            const amt_read = try self.read(result[0..]);
                             ^
/home/username/zig/0.10.0-dev.3315+1a16b7214/files/lib/std/io/reader.zig:114:32: 0x22c811 in std.io.reader.Reader(std.fs.file.File,std.os.ReadError,std.fs.file.File.read
).readUntilDelimiterArrayList (app)
                var byte: u8 = try self.readByte();
                               ^
/home/username/zig/0.10.0-dev.3315+1a16b7214/files/lib/std/io/reader.zig:136:13: 0x22c562 in std.io.reader.Reader(std.fs.file.File,std.os.ReadError,std.fs.file.File.read
).readUntilDelimiterAlloc (app)
            try self.readUntilDelimiterArrayList(&array_list, delimiter, max_size);
            ^
/home/username/projects/exp/zig/src/main.zig:10:19: 0x22a1a3 in main (app)
    const input = try stdin.readUntilDelimiterAlloc(galloc, '\n', 1000);
                  ^
The following command exited with error code 1 (expected 0):
cd /home/username/projects/exp/zig && /home/username/projects/exp/zig/zig-out/bin/app
error: UnexpectedExitCode
/home/username/zig/0.10.0-dev.3315+1a16b7214/files/lib/std/os.zig:2749:19: 0x2a168b in std.os.mkdiratZ (build)
        .EXIST => return error.PathAlreadyExists,
                  ^
/home/username/zig/0.10.0-dev.3315+1a16b7214/files/lib/std/os.zig:2709:9: 0x2a1573 in std.os.mkdirat (build)
        return mkdiratZ(dir_fd, &sub_dir_path_c, mode);
        ^
/home/username/zig/0.10.0-dev.3315+1a16b7214/files/lib/std/fs.zig:1332:9: 0x2a1466 in std.fs.Dir.makeDir (build)
        try os.mkdirat(self.fd, sub_path, default_new_dir_mode);
        ^
/home/username/zig/0.10.0-dev.3315+1a16b7214/files/lib/std/os.zig:1680:23: 0x28afdd in std.os.openatZ (build)
            .NOENT => return error.FileNotFound,
                      ^
/home/username/zig/0.10.0-dev.3315+1a16b7214/files/lib/std/fs.zig:1081:13: 0x276e4c in std.fs.Dir.openFileZ (build)
            try os.openatZ(self.fd, sub_path, os_flags, 0);
            ^
/home/username/zig/0.10.0-dev.3315+1a16b7214/files/lib/std/fs.zig:1008:9: 0x274c61 in std.fs.Dir.openFile (build)
        return self.openFileZ(&path_c, flags);
        ^
/home/username/zig/0.10.0-dev.3315+1a16b7214/files/lib/std/os.zig:2754:19: 0x2a1702 in std.os.mkdiratZ (build)
        .NOENT => return error.FileNotFound,
                  ^
/home/username/zig/0.10.0-dev.3315+1a16b7214/files/lib/std/os.zig:2709:9: 0x2a1573 in std.os.mkdirat (build)
        return mkdiratZ(dir_fd, &sub_dir_path_c, mode);
        ^
/home/username/zig/0.10.0-dev.3315+1a16b7214/files/lib/std/fs.zig:1332:9: 0x2a1466 in std.fs.Dir.makeDir (build)
        try os.mkdirat(self.fd, sub_path, default_new_dir_mode);
        ^
/home/username/zig/0.10.0-dev.3315+1a16b7214/files/lib/std/build/RunStep.zig:277:17: 0x2dc687 in std.build.RunStep.runCommand (build)
                return error.UnexpectedExitCode;
                ^
/home/username/zig/0.10.0-dev.3315+1a16b7214/files/lib/std/build/RunStep.zig:183:5: 0x2ccad6 in std.build.RunStep.make (build)
    try runCommand(
    ^
/home/username/zig/0.10.0-dev.3315+1a16b7214/files/lib/std/build.zig:3648:9: 0x2a1b9e in std.build.Step.make (build)
        try self.makeFn(self);
        ^
/home/username/zig/0.10.0-dev.3315+1a16b7214/files/lib/std/build.zig:508:9: 0x2a0fec in std.build.Builder.makeOneStep (build)
        try s.make();
        ^
/home/username/zig/0.10.0-dev.3315+1a16b7214/files/lib/std/build.zig:502:17: 0x2a0f98 in std.build.Builder.makeOneStep (build)
                return err;
                ^
/home/username/zig/0.10.0-dev.3315+1a16b7214/files/lib/std/build.zig:463:13: 0x2974c1 in std.build.Builder.make (build)
            try self.makeOneStep(s);
            ^
/home/username/zig/0.10.0-dev.3315+1a16b7214/files/lib/build_runner.zig:213:21: 0x2911b5 in main (build)
            else => return err,
                    ^
error: the following build command failed with exit code 1:
/home/username/projects/exp/zig/zig-cache/o/f68e9a1804c76861e552af4fc326bcdf/build /home/username/zig/0.10.0-dev.3315+1a16b7214/files/zig /home/username/projects/exp/z
ig /home/username/projects/exp/zig/zig-cache /home/username/.cache/zig run

this error only happens when build.zig is being compiled when you execute zig build run
so if you run zig build run again this error does not occur

@doongjohn doongjohn added the bug Observed behavior contradicts documented or intended behavior label Jul 27, 2022
@Vexu
Copy link
Member

Vexu commented Jul 27, 2022

Can't reproduce, what OS are you on?

@doongjohn
Copy link
Author

I'm using WSL2 (Void linux)

@doongjohn
Copy link
Author

This issue no longer occurs in the latest version 0.11.0-dev.201+3c3def6ac

@Vexu Vexu closed this as completed Nov 18, 2022
@Vexu Vexu modified the milestones: 0.12.0, 0.10.1 Nov 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior os-windows
Projects
None yet
Development

No branches or pull requests

2 participants