Skip to content
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

zig init template: add compiler version to build.zig.zon by default #22698

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

sweetbbak
Copy link
Contributor

referenced here on ziggit which describes the problem in depth, but the gist is that this solves the problem of having to manually try many different compiler versions to attempt to build a project that is unclear what version was used. If someone uses a tagged version, its not such a big deal (still annoying) but if they used a non-tagged version, it becomes nigh impossible to pin down what zig version was used to build a project. This currently just serves as an anchor point of reference so this is easier. It could also allow zig version managers to automatically use the correct version of the zig compiler.

Instead of parsing the zon file, I opted for using a wildcard like what is used for the '.name' field and just appending the version to the buffer that gets written.

It doesn't handle updating the version, I guess its better for the programmer to do so manually, and another simple expansion upon this could be emitting a warning when the current zig version is lower than or not equal to what is specified in the build.zig.zon file.

src/main.zig Outdated Show resolved Hide resolved
@sweetbbak
Copy link
Contributor Author

You can now define variables and replacements like so:

    // default replacements for templates
    const replacements = &[_]Replacement{
        .{ .variable = "$root", .replacement = cwd_basename },
        .{ .variable = "$version", .replacement = build_options.version },
    };

    for (template_paths) |template_path| {
        if (templates.write(arena, fs.cwd(), template_path, replacements)) |_| {
            std.log.info("created {s}", .{template_path});
            ok_count += 1;
        } else |err| switch (err) {
            error.PathAlreadyExists => std.log.info("preserving already existing file: {s}", .{
                template_path,
            }),
            else => std.log.err("unable to write {s}: {s}\n", .{ template_path, @errorName(err) }),
        }
    }

one edge case I haven't addressed would be if variables start with the same prefix. I guess a simple solution to that would be to ensure that all identifiers have unique prefixes. The variable $root might match against something like $root_other_var (but not the other way around) but I didnt want to over-engineer this.

@alexrp
Copy link
Member

alexrp commented Feb 6, 2025

Needs a zig build fmt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants