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

Closed
wants to merge 4 commits into 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.

@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.

Copy link
Member

@andrewrk andrewrk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, this is adhoc templating language is too complicated for how featureless it is.

it either should be kept simple by using fixed width tokens (i.e. finding another symbol besides '$' to mean different things, or updating to use length-2 tokens such as "$a" and "$b"

or it should go full tokenization and be able to say "unrecognized substitution: $misspelled_thing"

what's in this PR is an awkward middle ground between both of these things.

maybe it can be recovered if you fail with an error message that prints the rest of the line in case no match is found.

please use mem.startsWith rather than crashing when $ is at the end of the file

@andrewrk
Copy link
Member

I have this solved as part of #22994

@andrewrk andrewrk closed this Feb 25, 2025
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.

3 participants