Cannot compile when the sources are in the same directory as build.zig

Hi guys,
I have 2 zig directory test1 and test2, test1 is used as a module by test2. when build test2, got following error

install
└─ install test2
   └─ compile exe test2 Debug native 1 errors
error: failed to check cache: 'lib.zig' file_hash FileNotFound
error: 1 compilation errors
failed command: "D:\\zig\\0.16.0\\zig.exe" build-exe --dep test1 "-Mroot=D:\\notes\\zig\\packages\\test2\\main.zig" -Mtest1=lib.zig --cache-dir .zig-cache --global-cache-dir "C:\\Users\\yusha\\AppData\\Local\\zig" --name test2 --zig-lib-dir "D:\\zig\\0.16.0\\lib\\" --listen=-

Build Summary: 0/3 steps succeeded (1 failed)
install transitive failure
└─ install test2 transitive failure
   └─ compile exe test2 Debug native 1 errors

test1\build.zig

const std = @import("std");
pub fn build(b: *std.Build) void {
    _ = b.addModule("test1", .{ //
        .root_source_file = .{ .cwd_relative = "lib.zig" },
        .target = b.standardTargetOptions(.{}),
    });
}

test1\build.zig.zon

.{
    .name = .test1,
    .version = "0.0.1",
    .minimum_zig_version = "0.16.0",
    .paths = .{ "build.zig", "build.zig.zon", "lib.zig" },
    .fingerprint = 0x8ab2dce208e49f5d,
    .dependencies = .{},
}

test1\lib.zig

pub fn hello() void {}

test2\build.zig

const std = @import("std");
pub fn build(b: *std.Build) void {
    const test1 = b.dependency("test1", .{});
    const test2 = b.addExecutable(.{ //
        .name = "test2",
        .root_module = b.createModule(.{ //
            .root_source_file = b.path("main.zig"),
            .target = b.standardTargetOptions(.{}),
            .imports = &.{.{ //
                .name = "test1",
                .module = test1.module("test1"),
            }},
        }),
    });
    b.installArtifact(test2);
}

test2\build.zig.zon

.{
    .name = .test2,
    .version = "0.0.1",
    .minimum_zig_version = "0.16.0",
    .paths = .{ "build.zig", "build.zig.zon", "main.zig" },
    .fingerprint = 0x13bb8d58e231c39c,
    .dependencies = .{
        .test1 = .{ .path = "../test1" },
    },
}

test2\main.zig

const test1 = @import("test1");
pub fn main() void {
    test1.hello();
}

you should use b.path() in almost every case rather than .cwd_relative.

1 Like

you are right. it works!