Skip to content

Unable to find module json #53

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

Closed
Lukkyz opened this issue Dec 4, 2023 · 15 comments
Closed

Unable to find module json #53

Lukkyz opened this issue Dec 4, 2023 · 15 comments
Labels

Comments

@Lukkyz
Copy link

Lukkyz commented Dec 4, 2023

Description

I'm encountering this error when installing getty json with zon

thread 11249 panic: unable to find module 'json'
/usr/lib/zig/std/debug.zig:374:22: 0x36c1c9 in panicExtra__anon_46291 (build)
    std.builtin.panic(msg, trace, ret_addr);
                     ^
/usr/lib/zig/std/debug.zig:349:15: 0x340d89 in panic__anon_13633 (build)
    panicExtra(null, null, format, args);
              ^
/usr/lib/zig/std/Build.zig:1556:18: 0x2f7ae7 in module (build)
            panic("unable to find module '{s}'", .{name});
                 ^
/home/lucas/Documents/ninja/build.zig:8:55: 0x2f60ca in build (build)
    const json_mod = b.dependency("json", opts).module("json");
                                                      ^
/usr/lib/zig/std/Build.zig:1639:43: 0x2e5180 in runBuild__anon_7138 (build)
        .ErrorUnion => try build_zig.build(b),
                                          ^
/usr/lib/zig/build_runner.zig:297:29: 0x2e0f52 in main (build)
        try builder.runBuild(root);
                            ^
/usr/lib/zig/std/start.zig:574:37: 0x2cc85e in posixCallMainAndExit (build)
            const result = root.main() catch |err| {
                                    ^
/usr/lib/zig/std/start.zig:243:5: 0x2cc341 in _start (build)
    asm volatile (switch (native_arch) {

How to Reproduce the Bug

Add json to zon :

{
  ...
  .dependencies = .{
    .json = .{
      .url = "https://github.com/getty-zig/json/archive/d9819a96d5769dfa497ba27832ecd6fd86645ea9.tar.gz",
      .hash = "1220e73e4fd05a6e5813c61055a6e935d35774b0b9a79f1547012f89546739c49bc8"
    }
  }
}

Add module in build.zig :

const json_mod = b.dependency("json", opts).module("json");
exe.addModule("json", json_mod);

Additional Context

No response

@Lukkyz Lukkyz added the bug label Dec 4, 2023
@ibokuri
Copy link
Contributor

ibokuri commented Dec 4, 2023

What version of Zig are you using?

I just tried using the same version of getty-json that you showed and it worked for me, though I had to use a different package hash value.

@Lukkyz
Copy link
Author

Lukkyz commented Dec 4, 2023

I use zig 0.11.0
Can you provide me the hash you used ?

@ibokuri
Copy link
Contributor

ibokuri commented Dec 4, 2023

Hash: 1220e37ec05868531042349ba841ddace6517adb8f2414ea629c3c6e426c0f395b8f

I'm on master, btw. I wonder if something changed in regards to the package manager between 0.11.0 and master? 🤔

@Lukkyz
Copy link
Author

Lukkyz commented Dec 4, 2023

I tried with your hash and master and it don't work.
Yes maybe, Zon has some weird behavior sometimes.
I can try to install getty without zig but don't know how to modify my build.zig to make it work.

@ibokuri
Copy link
Contributor

ibokuri commented Dec 4, 2023

Can you delete the .hash = "..." line from your build.zig.zon and then run the following in your project directory using Zig master?

$ rm -rf zig-cache
$ zig build --fetch

The 2nd command should give you a hash value that you can update build.zig.zon with. See if the hash value is the same as your original one or mine.

Also, if you could show your build.zig (maybe on https://zigbin.io/), that'd help as well.

@Lukkyz
Copy link
Author

Lukkyz commented Dec 4, 2023

I tried, when the compiler give me a hash and use it I have this error :

hash mismatch: expected: 1220c28503c1a092c028aa45e09e488458f419122934d6c3b9a48b63fe0e131fe1a1, found: 1220e73e4fd05a6e5813c61055a6e935d35774b0b9a79f1547012f89546739c49bc8
            .hash = "1220c28503c1a092c028aa45e09e488458f419122934d6c3b9a48b63fe0e131fe1a1",

(This is where it's weird, because the expected hash is one that I provide and found hash is another hash, seems like a bug, the word found and expected are inverted)
So I tried with the "found" hash and after that I got the same initial error "unable to find module json"
Here is my build.zig : https://zigbin.io/b214c0

@ibokuri
Copy link
Contributor

ibokuri commented Dec 4, 2023

I see the json module being added to mapExe in your build.zig but I don't see any addModule calls for exe.

@Lukkyz
Copy link
Author

Lukkyz commented Dec 5, 2023

Yes because for the moment I only need this module on mapExe.

@ibokuri
Copy link
Contributor

ibokuri commented Dec 6, 2023

Could you try adding it to exe anyways? Maybe there's some package manager weirdness where it needs to be applied to both despite only being required by mapExe.

edit: I just noticed that you're using multiple build.zig files, and it looks like nested ones as well? If I had to guess, it's probably something to do with the project structure that's causing the package manager to freak out. In which case, asking around in the Zig discord might be a better option here.

@Lukkyz
Copy link
Author

Lukkyz commented Dec 6, 2023

I've tried all 😥
Tried to add it on exe and got the same problem.
Tried on an empty project with this build : https://zigbin.io/f619a8 and same

@ibokuri
Copy link
Contributor

ibokuri commented Dec 7, 2023

Does that build.zig even work? Why is the path main.zig and not src/main.zig? I just did a zig init (using Zig master) on a new, empty directory to test this out and everything works fine for me.

build.zig

const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});
    const opts = .{ .target = target, .optimize = optimize };

    const json_mod = b.dependency("json", opts).module("json");

    const exe = b.addExecutable(.{
        .name = "test",
        .root_source_file = .{ .path = "src/main.zig" },
        .target = target,
        .optimize = optimize,
    });
    exe.addModule("json", json_mod);

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

build.zig.zon

.{
    .name = "test",
    .version = "0.0.0",
    .paths = .{""},
    .dependencies = .{
        .json = .{
            .url = "https://github.com/getty-zig/json/archive/d9819a96d5769dfa497ba27832ecd6fd86645ea9.tar.gz",
            .hash = "1220e37ec05868531042349ba841ddace6517adb8f2414ea629c3c6e426c0f395b8f",
        },
    },
}

src/main.zig

const std = @import("std");
const json = @import("json");

pub fn main() !void {
    const s = try json.toSlice(std.heap.c_allocator, true);

    std.debug.print("{s}\n", .{s});
}

Shell

$ zig build run
true

@Lukkyz
Copy link
Author

Lukkyz commented Dec 8, 2023

I didn't know zig init, but tried with and I got the same problem, I created a post on the zig discord and we will see.

@Cloudef
Copy link

Cloudef commented Dec 16, 2023

Are you using BTRFS? Maybe you are hitting this issue ziglang/zig#17282

@AdjectiveAllison
Copy link

I'm having a similar issue, I found the zig discord thread and commented several times there but I'll summarize what I've tried here.

I also was using 0.11.0 originally. I used commit dda4272d6e138a5fc5838490186481320a2a2bfe throughout the following steps.

Here is zig build with no hash on 0.11.0. Throughout the course of my troubleshooting I don't think --fetch is needed and that it will fetch automatically if no hash is found(identical results).

        .json = .{ .url = "https://github.com/getty-zig/json/archive/dda4272d6e138a5fc5838490186481320a2a2bfe.tar.gz" },
                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: expected .hash = "12200df556b3aa78c574f8ce916e7d3085e0c14981103e0b4fc40ccbca58c1a4ffbe",

I run zig build again with the reported hash added on 0.11.0 and get the following hash mismatch error:

/home/allison/.cache/zig/p/12200df556b3aa78c574f8ce916e7d3085e0c14981103e0b4fc40ccbca58c1a4ffbe/build.zig.zon:14:21: error: hash mismatch: expected: 1220300f52d7592914660ee3c8e461dc7c10bd45708eb32deb0e341b47c7921536dc, found: 12201b27a3fa3bc208186ec46b54d57ec119809fd5a0476ddda55d369ab70a2b3fce
            .hash = "1220300f52d7592914660ee3c8e461dc7c10bd45708eb32deb0e341b47c7921536dc",

I deleted local zig-cache, switched to master branch of zig, and restarted the process.

As expected, with a zig build on master we get a hash request:

Fetch Packages [3/2] /blah/build.zig.zon:14:27: error: dependency is missing hash field
        .json = .{ .url = "https://github.com/getty-zig/json/archive/dda4272d6e138a5fc5838490186481320a2a2bfe.tar.gz" },
                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: expected .hash = "12206efb55d5d5eb7698ac1984cdb1aebdfa7bc38771bfad98d4e121fc9c2465cd94",

Added that hash in and reran zig build on master and it works! I now have errors with other packages and master branch though. So I'd prefer to be able to get 0.11.0 working!

While typing this up I got a response on discord that seems relevant/useful:

The issue here is that 0.11.0 and master calculate the hash differently for packages which use the paths field in build.zig.zon to include only certain files in the final package: https://github.com/ziglang/zig/blob/master/doc/build.zig.zon.md#paths
0.11.0 doesn't know about paths, so the hash consists of all files in the fetched package, but master will consider only paths in paths in the hash. Unfortunately, getty-zig/json has transitive dependencies in its own build.zig.zon which use the hash calculated by master, making it incompatible with 0.11.0:

.hash = "1220300f52d7592914660ee3c8e461dc7c10bd45708eb32deb0e341b47c7921536dc",

If your other packages are incompatible with master, that raises an unfortunate dilemma (getty-zig/json only works with master, other dependencies only work with 0.11.0)

@ibokuri
Copy link
Contributor

ibokuri commented Dec 19, 2023

Thanks for looking into it @AdjectiveAllison and posting your findings here so that others can reference it!

The short of it is, projects that want to use the latest version of Getty or Getty JSON will have to upgrade from 0.11.0 to a more recent version of Zig. Both Getty and Getty JSON are developed using Zig master (see the badges on either project's README) and therefore use features and have fixes for changes that are not available or present in 0.11.0.

Until Zig reaches 1.0, both projects will continue using Zig master for development. I might consider keeping a separate branch though for whenever Zig 0.12.0 comes out that'll remain compatible with that version of Zig.

[...] I don't think --fetch is needed and that it will fetch automatically if no hash is found(identical results).

Correct, --fetch is not needed. However, I do like including it just so that the build process stops immediately after fetching packages. That way, no unnecessary build stuff will run when we're just looking for the hashes.

@ibokuri ibokuri closed this as completed Feb 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants