addPackage error in build script

Hi all -
I’m getting an error when running build - here is the error -

andy@obsidian:~/Nanoscript$ zig build
/home/andy/Nanoscript/build.zig:78:8: error: no field or member function named 'addPackage' in 'Build.Step.Compile'
    exe.addPackage(pkgs.lexer);
    ~~~^~~~~~~~~~~
/home/andy/zig/lib/std/Build/Step/Compile.zig:1:1: note: struct declared here
const builtin = @import("builtin");
^~~~~
referenced by:
    runBuild__anon_8818: /home/andy/zig/lib/std/Build.zig:2116:27
    main: /home/andy/zig/lib/compiler/build_runner.zig:301:29
    remaining reference traces hidden; use '-freference-trace' to see all reference traces
andy@obsidian:~/Nanoscript$ 

The relevant part of my build script is here -

// The following package build syntax is from here -  
// https://github.com/zig-community/Zig-Showdown/blob/main/build.zig#L6-L62 
    
    const pkgs = struct {

    const lexer = std.build.Pkg{
        .name = "lexer",
        .path = "src/lexer.zig",
    };

    const parser = std.build.Pkg{
        .name = "parser",
        .path = "src/parser.zig",
    };

    const semantic_analyzer = std.build.Pkg{
        .name = "semantic_analyzer",
        .path = "src/semantic_analyzer.zig",
    };
};  

  // Add packages.  
    exe.addPackage(pkgs.lexer);
    exe.addPackage(pkgs.parser);
    exe.addPackage(pkgs.semantic_analyzer);

I should mention that this syntax is being used because I had an almost-identical error with the previous version of my build script which used this syntax -

exe.addPackagePath("lexer", "src/lexer.zig");
exe.addPackagePath("parser", "src/parser.zig");
exe.addPackagePath("semantic_analyzer", "src/semantic_analyzer.zig");

That syntax gave me “no field or member function named addPackagePath”.
I looked around and found the addPackage syntax shown above but that doesn’t work either.
I’m running on Linux Mint.
Zig version - 0.14.0-dev.14+ec337051a

Hoping someone can help! Many thanks in advance -

  • mooseman

It looks like code for old zig version (between 0.10.0 and 0.11.0).

addPackage is today addImport and
std.build.Pkg is today addModule or createModule.

const lexer = b.addModule("lexer", .{
    .root_source_file = b.path("src/lexer.zig"),
});
exe.root_module.addImport("lexer", lexer);

const parser = b.addModule("parser", .{
    .root_source_file = b.path("src/parser.zig"),
});
exe.root_module.addImport("parser", parser);

Hi dimdin!

Thanks very much for that, that’s awesome! You’re a legend! :slightly_smiling_face:

I was thinking (dangerous, I know, but anyway… :wink: )
An idea here for Andrew Kelley to consider (if he ever comes here… ).

Python has the dir() function which gives attributes of a class or module.
Maybe Zig could have something similar - something like -

   addModule(?)  

That would give the standard syntax of addModule (or anything else that you used (?) with. All of the parameters and everything.
I imagine it wouldn’t be hugely difficult to add to Zig - just a matter of self-introspection of the code.
This would only be added to the Build module (at least for now) as that’s the most central and critical one IMO.

I realise that Andrew Kelley probably never comes here (and would be very unlikely to take up a newbie’s suggestion anyway) but I thought I’d just “put that idea out there”.
It would be very handy to have more "DeprecatedWarning"s too. I think I’ve seen one or two but more would be very useful - especially in the Build module.

Many thanks again! Bye for now -

  • mooseman
  • Andrew has weighed in here. From what I’ve observed he generally judges ideas on their merits, not on the “newness” or other attributes of the person proposing them (except when the person has otherwise already proven to be unreliable).
  • Zig (Andrew) has taken a firm stance against warnings. Deprecations are another matter, but they’re expensive to manage and create friction against moving quickly. Zig is < 1.0 and it moves quickly and breaks things.