zig icon indicating copy to clipboard operation
zig copied to clipboard

zig build: FileNotFound when lib.ofmt = .c

Open hryx opened this issue 1 year ago • 4 comments

Zig Version

0.11.0-dev.12+ebf9ffd34

Steps to Reproduce and Observed Behavior

In build.zig:

    // ...
    const lib = b.addStaticLibrary("scanner", "scanner.zig");
    lib.ofmt = .c;
    // ...

Then run zig build.

Output

error: FileNotFound
/home/hryx/bin/zig-git/lib/zig/std/os.zig:1741:23: 0x2cb3c8 in openatZ (build)
            .NOENT => return error.FileNotFound,
                      ^
/home/hryx/bin/zig-git/lib/zig/std/fs.zig:1175:13: 0x2b7b26 in openFileZ (build)
            try os.openatZ(self.fd, sub_path, os_flags, 0);
            ^
/home/hryx/bin/zig-git/lib/zig/std/fs.zig:1102:9: 0x29be28 in openFile (build)
        return self.openFileZ(&path_c, flags);
        ^
/home/hryx/bin/zig-git/lib/zig/std/fs.zig:2532:24: 0x30171a in updateFile (build)
        var src_file = try source_dir.openFile(source_path, .{});
                       ^
/home/hryx/bin/zig-git/lib/zig/std/build.zig:1089:29: 0x30b517 in updateFile (build)
        const prev_status = try fs.Dir.updateFile(cwd, source_path, cwd, dest_path, .{});
                            ^
/home/hryx/bin/zig-git/lib/zig/std/build.zig:3416:9: 0x2f5e74 in make (build)
        try builder.updateFile(self.artifact.getOutputSource().getPath(builder), full_dest_path);
        ^
/home/hryx/bin/zig-git/lib/zig/std/build.zig:3648:9: 0x28b377 in make (build)
        try self.makeFn(self);
        ^
/home/hryx/bin/zig-git/lib/zig/std/build.zig:509:9: 0x279114 in makeOneStep (build)
        try s.make();
        ^
/home/hryx/bin/zig-git/lib/zig/std/build.zig:503:17: 0x279082 in makeOneStep (build)
                return err;
                ^
/home/hryx/bin/zig-git/lib/zig/std/build.zig:464:13: 0x278de2 in make (build)
            try self.makeOneStep(s);
            ^
/home/hryx/bin/zig-git/lib/zig/build_runner.zig:223:21: 0x27bd4a in main (build)
            else => return err,
                    ^

The generated C file is correctly placed, but zig.h is not.

Expected Behavior

No error trace, and zig.h is next to the generated C file

If I understand, it's trying to copy $PREFIX/lib/zig/include/zig.h to the output directory from the wrong location. Manually copying the file is a workaround.

Related: https://github.com/ziglang/zig/issues/12965 (duplicate?), https://github.com/ziglang/zig/pull/12968 (does not fix), https://github.com/ziglang/zig/issues/11643

hryx avatar Nov 02 '22 02:11 hryx

Workaround: Edit your target, setting target.ofmt = .c; before calling setTarget. (Thanks to @jacobly0 for finding this workaround in #13389)

andrewrk avatar Nov 02 '22 02:11 andrewrk

Can this be closed now that #13389 is merged, and lib.ofmt no longer exists?

jacobly0 avatar Nov 05 '22 03:11 jacobly0

After changing lib.ofmt = .c to lib.target.ofmt = .c and running zig build with the latest nightly, I don't get the error anymore. But zig.h is still not getting copied to the output dir. I could make a separate issue if you like, and if so, this can be closed.

hryx avatar Nov 05 '22 07:11 hryx

It also should not set the executable bit on the .c file

andrewrk avatar Nov 05 '22 08:11 andrewrk