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

No compile errors with -fno-emit-bin if c files in the build #22682

Closed
leecannon opened this issue Jan 30, 2025 · 1 comment · Fixed by #23255
Closed

No compile errors with -fno-emit-bin if c files in the build #22682

leecannon opened this issue Jan 30, 2025 · 1 comment · Fixed by #23255
Labels
bug Observed behavior contradicts documented or intended behavior
Milestone

Comments

@leecannon
Copy link
Contributor

leecannon commented Jan 30, 2025

Zig Version

0.14.0-dev.2989+bf6ee7cb3

Steps to Reproduce and Observed Behavior

With an empty c file called empty.c and main.zig containing:

pub fn main() void {
    const a: bool = 2;
    _ = a;
}
$ zig build-exe -fno-emit-bin empty.c main.zig
$ echo $?                                                           
0

The correct behavior occurs without the c file:

$ zig build-exe -fno-emit-bin main.zig  
main.zig:2:21: error: expected type 'bool', found 'comptime_int'
    const a: bool = 2;
                    ^
referenced by:
    posixCallMainAndExit: /home/lee/zig/0.14.0-dev.2989+bf6ee7cb3/files/lib/std/start.zig:656:37
    _start: /home/lee/zig/0.14.0-dev.2989+bf6ee7cb3/files/lib/std/start.zig:464:40
    3 reference(s) hidden; use '-freference-trace=5' to see all references
$ echo $?                                             
1

Expected Behavior

The same error as without -fno-emit-bin:

$ zig build-exe empty.c main.zig              
main.zig:2:21: error: expected type 'bool', found 'comptime_int'
    const a: bool = 2;
                    ^
referenced by:
    posixCallMainAndExit: /home/lee/zig/0.14.0-dev.2989+bf6ee7cb3/files/lib/std/start.zig:656:37
    _start: /home/lee/zig/0.14.0-dev.2989+bf6ee7cb3/files/lib/std/start.zig:464:40
    3 reference(s) hidden; use '-freference-trace=5' to see all references
$ echo $?                                                           
1
@leecannon leecannon added the bug Observed behavior contradicts documented or intended behavior label Jan 30, 2025
@leecannon leecannon changed the title -fno-emit-bin does not report compile errors when c files included in build No compile errors with -fno-emit-bin if c files in the build Feb 3, 2025
@leecannon
Copy link
Contributor Author

This issue is caused by comp.remaining_prelink_tasks being incremented here

comp.remaining_prelink_tasks += @intCast(comp.c_object_table.count());
but it is never decremented so performAllTheWorkInner returns early

zig/src/Compilation.zig

Lines 3852 to 3855 in 08d661f

if (comp.remaining_prelink_tasks > 0) {
// Indicates an error occurred preventing prelink phase from completing.
return;
}
but no error has been emitted so compilation exits without reporting failure.

But I'm struggling to follow through the logic to actually get to the bottom of this.

leecannon added a commit to CascadeOS/CascadeOS that referenced this issue Feb 6, 2025

Verified

This commit was signed with the committer’s verified signature.
snyk-bot Snyk bot
Workaround for ziglang/zig#22682
sphaerophoria added a commit to sphaerophoria/sphimp that referenced this issue Mar 15, 2025
See comment. The overly hacky part can be removed if ziglang/zig#22682
is resolved
@mlugg mlugg added this to the 0.14.1 milestone Mar 15, 2025
mlugg added a commit to mlugg/zig that referenced this issue Mar 15, 2025
The old logic only decremented `remaining_prelink_tasks` if `bin_file`
was not `null`. This meant that on `-fno-emit-bin` builds with
registered prelink tasks (e.g. C source files), we exited from
`Compilation.performAllTheWorkInner` early, assuming a prelink error.

Instead, when `bin_file` is `null`, we still decrement
`remaining_prelink_tasks`; we just don't do any actual work.

Resolves: ziglang#22682
lishaduck added a commit to LoganjdM/cu-fied that referenced this issue Mar 16, 2025
andrewrk pushed a commit that referenced this issue Mar 23, 2025
The old logic only decremented `remaining_prelink_tasks` if `bin_file`
was not `null`. This meant that on `-fno-emit-bin` builds with
registered prelink tasks (e.g. C source files), we exited from
`Compilation.performAllTheWorkInner` early, assuming a prelink error.

Instead, when `bin_file` is `null`, we still decrement
`remaining_prelink_tasks`; we just don't do any actual work.

Resolves: #22682
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants