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

Cross compile to Windows fails with relative global cache directory path #19284

Closed
aherrmann opened this issue Mar 13, 2024 · 4 comments · Fixed by #19319
Closed

Cross compile to Windows fails with relative global cache directory path #19284

aherrmann opened this issue Mar 13, 2024 · 4 comments · Fixed by #19319
Labels
bug Observed behavior contradicts documented or intended behavior regression It worked in a previous version of Zig, but stopped working.
Milestone

Comments

@aherrmann
Copy link
Contributor

Zig Version

0.12.0-dev.3282+da5b16f9e

Steps to Reproduce and Observed Behavior

Configuring a global cache directory on the command line using a relative path causes cross compilation to Windows to fail. Tested on Ubuntu 22.04 x86_64.

Steps to reproduce

Download and unpack the Zig SDK.

$ curl -L https://ziglang.org/builds/zig-linux-x86_64-0.12.0-dev.3282+da5b16f9e.tar.xz -o zig-linux-x86_64-0.12.0-dev.3282+da5b16f9e.tar.xz
$ tar xvf zig-linux-x86_64-0.12.0-dev.3282+da5b16f9e.tar.xz

Create a simple hello world program.

$ cat <<EOF >main.zig
const std = @import("std");

pub fn main() !void {
    try std.io.getStdOut().writer().print("Hello world!\n", .{});
}
EOF

Try to cross-compile to Windows while specifying a global cache directory on the command line using a relative path.

$ zig-linux-x86_64-0.12.0-dev.3282+da5b16f9e/zig build-exe --global-cache-dir custom-zig-global-cache -target x86_64-windows-gnu main.zig
error: unable to generate DLL import .lib file for kernel32: FileNotFound

Observe how the build fails due to a missing .lib file for the kernel32 library.

Run the same command, but use an absolute path for the global cache directory parameter.

$ zig-linux-x86_64-0.12.0-dev.3282+da5b16f9e/zig build-exe --global-cache-dir $PWD/custom-zig-global-cache -target x86_64-windows-gnu main.zig
$ wine64 ./main.exe
Hello world!

Observe how the build succeeds and the produced binary can run with Wine.

Context

This issue was initially observed in https://github.com/aherrmann/rules_zig when trying to update to Zig 0.12.0-dev. To ensure that builds are hermetic and reproducible, rules_zig controls the global and local cache directories. At the moment it declares a global and local cache output for each build action that is managed by Bazel. Bazel operates on relative paths, so these cache directory paths are always configured using relative paths.

(Side note: Better cache handling is planned as a future improvement, see aherrmann/rules_zig#87)

Expected Behavior

Zig builds should succeed irrespective of whether the global (or local) cache paths are specified as relative or absolute paths.

@aherrmann aherrmann added the bug Observed behavior contradicts documented or intended behavior label Mar 13, 2024
@squeek502
Copy link
Collaborator

squeek502 commented Mar 13, 2024

If this worked previously, it could possibly be a regression introduced in #19173 or maybe #17771

@aherrmann
Copy link
Contributor Author

If this worked previously

Yes, it did work previously with Zig 0.11.0.

@squeek502 squeek502 added the regression It worked in a previous version of Zig, but stopped working. label Mar 13, 2024
@unnask
Copy link

unnask commented Mar 14, 2024

Seems also related to the error I am getting from a basic raylib hello-world (0.12.0-dev.3284+153ba46a5)

error: unable to check cache: stat file '/Users/<user>/.cache/zig/p/1220218d3958e4b46293456abc189ec98e8a7e3add6a46550b852bc0952609805315/Users/<user>/.cache/zig/p/1220218d3958e4b46293456abc189ec98e8a7e3add6a46550b852bc0952609805315/src/rcore.c' failed: FileNotFound

Seems the path up to the downloaded cache directory is being inserted twice.

@aherrmann
Copy link
Contributor Author

git bisect identified 9f2cb92 from #17771 as the culprit.

aherrmann added a commit to aherrmann/zig that referenced this issue Mar 15, 2024
Closes ziglang#19284

As of 9f2cb92 configuring the global
cache directory to a relative path causes errors of the following form
when cross compiling to `windows-gnu`.
```
error: unable to generate DLL import .lib file for kernel32: FileNotFound
```

The issue occurred because the `def_final_path` included the global
cache path as a prefix and later on the `def_final_file` was created
using that path with the global cache directory handle.
@Vexu Vexu added this to the 0.12.0 milestone Mar 16, 2024
Vexu pushed a commit that referenced this issue Mar 16, 2024
Closes #19284

As of 9f2cb92 configuring the global
cache directory to a relative path causes errors of the following form
when cross compiling to `windows-gnu`.
```
error: unable to generate DLL import .lib file for kernel32: FileNotFound
```

The issue occurred because the `def_final_path` included the global
cache path as a prefix and later on the `def_final_file` was created
using that path with the global cache directory handle.
RossComputerGuy pushed a commit to ExpidusOS-archive/zig that referenced this issue Mar 20, 2024
Closes ziglang#19284

As of 9f2cb92 configuring the global
cache directory to a relative path causes errors of the following form
when cross compiling to `windows-gnu`.
```
error: unable to generate DLL import .lib file for kernel32: FileNotFound
```

The issue occurred because the `def_final_path` included the global
cache path as a prefix and later on the `def_final_file` was created
using that path with the global cache directory handle.
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 regression It worked in a previous version of Zig, but stopped working.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants