Skip to content

Ability to determine the file a module is in without complete knowledge of the compilations module dependency graph #20999

Open
@leecannon

Description

@leecannon

Zig Version

0.14.0-dev.985+cf87a1a7c

Steps to Reproduce and Observed Behavior

Within a single compilation multiple modules can have the same name, the build system de-duplicates these by appending a number to the name.

But this means you cannot determine which module a file is actually in, as the @src().module string depends on both if that module has a name collision with another and also on the order the imports were added, which with the package manager and transitive dependencies is a challenge :).

// build.zig
pub fn build(b: *std.Build) void {
    const exe = b.addExecutable(.{
        .name = "fff",
        .root_source_file = b.path("src/main.zig"),
        .target = b.standardTargetOptions(.{}),
        .optimize = b.standardOptimizeOption(.{}),
    });

    const dep1 = b.createModule(.{
        .root_source_file = b.path("dep1/main.zig"),
    });
    exe.root_module.addImport("dep", dep1);

    const dep2 = b.createModule(.{
        .root_source_file = b.path("dep2/main.zig"),
    });
    dep1.addImport("dep", dep2);

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

// src/main.zig
pub fn main() void {
    std.debug.print("{s} - {s}\n", .{ @src().module, @src().file });
    @import("dep").printSrcFile(); // this "dep" is dep1/main.zig
}

// dep1/main.zig
pub fn printSrcFile() void {
    std.debug.print("{s} - {s}\n", .{ @src().module, @src().file });
    @import("dep").printSrcFile(); // this "dep" is dep2/main.zig
}

// dep2/main.zig
pub fn printSrcFile() void {
    std.debug.print("{s} - {s}\n", .{ @src().module, @src().file });
}
$ zig build run
root - main.zig
dep - main.zig
dep0 - main.zig

Expected Behavior

Ability to determine the module a file is in without complete knowledge of the compilations module dependency graph.

Metadata

Metadata

Assignees

No one assigned

    Labels

    use caseDescribes a real use case that is difficult or impossible, but does not propose a solution.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions