Skip to content

build: module imports do not respect target OS #20492

@cbilz

Description

@cbilz

Zig Version

0.14.0-dev.144+a31fe8aa3

Steps to Reproduce and Observed Behavior

In the following example, a Linux executable is allowed to import a module targeting WASI.

// build.zig
const std = @import("std");

pub fn build(b: *std.Build) void {
    const foreign = b.addModule("foreign", .{
        .root_source_file = b.path("src/foreign.zig"),
        .target = b.resolveTargetQuery(.{ .os_tag = .wasi }),
    });
    const exe = b.addExecutable(.{
        .name = "main",
        .root_source_file = b.path("src/main.zig"),
        .target = b.standardTargetOptions(.{}),
    });
    exe.root_module.addImport("foreign", foreign);

    const run_step = b.step("run", "Run the executable");
    run_step.dependOn(&b.addRunArtifact(exe).step);
}
// src/foreign.zig
pub const os = @import("builtin").os;
// src/main.zig
const std = @import("std");

pub fn main() void {
    std.debug.print("builtin os: {}\n", .{ @import("builtin").os.tag });
    std.debug.print("foreign os: {}\n", .{ @import("foreign").os.tag });
}
$ zig-linux-x86_64-0.14.0-dev.144+a31fe8aa3/zig build run
builtin os: Target.Os.Tag.linux
foreign os: Target.Os.Tag.wasi

Related Ziggit topic

Expected Behavior

I see two problems with this behavior:

  • The imported module cannot observe the actual compilation options because builtin does not behave as one might expect.
  • The intended compilation options of the imported module are ignored. So the Module.CreateOptions argument of addModule does not do what one might expect.

I think the build system should do one of the following:

  • ensure that any overrides of compilation options are reflected in builtin, or
  • refuse to import modules with different compilation options. For instance, refuse to import if anything in builtin will differ between the importing and the imported modules.

I don't know which is better, partly because I don't understand why Module was designed to carry compilation options in the first place.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions