r/Zig • • 10h ago

pureinit: a tool for cleaning `zig init` templates

0 Upvotes

Hi everyone, I really like Zig language. A tool was recently developed.

  • Repo link: https://github.com/Hentioe/pureinit
  • Readme:

pureinit

A tool for cleaning redundant code from templates, currently used to clean Zig project templates generated by zig init.

Introduction

This tool extracts cleanup information from patches generated by git-diff. It does not scan for code targets or comments. Therefore, it is effectively generic and can perform cleanup from external patches. The built-in patches are actually located in the src/diff_patchs source directory.

Usage

For the zig init template in Zig 0.17.1, it has:

-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Zig                              4             29            115            101
-------------------------------------------------------------------------------
SUM:                             4             29            115            101
-------------------------------------------------------------------------------

It is filled with a large number of comments (115 lines) and example code (more than half). This may help beginners, but it is meaningless for experienced users. In my view, this is all "noise."

Use pureinit to clean it (it detects the version in build.zig.zon and uses the corresponding patch):

$ pureinit
Patch info:
+-----------------------+----------+-----+--------+
| name                  | from     | env | ver    |
+=======================+==========+=====+========+
| zig-init.0.17.1.patch | builting | zig | 0.17.1 |
+-----------------------+----------+-----+--------+
Apply patch to: '.'? [y/N]: y
INFO Applying build.zig
INFO Applying build.zig.zon
INFO Applying src/main.zig
INFO Applying src/root.zig

Afterward:

-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Zig                              4              9              2             43
-------------------------------------------------------------------------------
SUM:                             4              9              2             43
-------------------------------------------------------------------------------

The project template is now very clean! You can view these cleaned templates in the templates directory.

Building

This is a pure Zig project, so:

zig build

Install it to the system (the user bin directory):

zig build --release=safe install --prefix ~/.local

For Other Projects

Because it is generic by design, you can create your own patches and use pureinit to clean them. First (assuming your framework is called foo):

foo init
  • Commit the original template code to Git, then perform the manual cleanup.
  • When finished, add the modifications to the staging area.

Now you can generate a patch:

git diff --cached HEAD | save foo-init.0.0.1.patch

Use pureinit to apply your custom patch:

pureinit -p foo-init.0.0.1.patch

Note: pureinit does not delete files, because cleaning zig init does not currently require this.

I know zig init has --minimal parameter, but this tool still makes sense I think.

  • First of all it is universal. This phenomenon also exists for other tools/frameworks. The templates they generate are too "bloated".
  • Actually I don't like the zig init --minimal template either. It's too "concise" for either a program or a library.

r/Zig • • 16h ago

Painful error messages

29 Upvotes

Andrew dare bash on C++ yet their own error messages are so helpful and informative

error: cannot format slice without a specifier (i.e. {s}, {x}, {b64}, or {any})

compileError("cannot format slice without a specifier (i.e. {s}, {x}, {b64}, or {any})");

And what does the reference trace shows? the userland where failure happened right? Nope, a bunch of trace inside the std library. So we just need to hunt down all the places we print?

Is this the suppposed compiler behaviour, something I am missing here guys? Cause this definitely isn't the first kind of errors that point to the std library instead of userland, other common culprit being allocators.


r/Zig • • 12h ago

Migrating my codebase from Zig 0.16.0 to 0.17.1, any advice?

16 Upvotes

I’m about to start the process of upgrading my kernel written in Zig—from 0.16.0 to the 0.17.1, and I'd love some advice from others who have gone through a similar process, especially on bare-metal or systems migrations.

What was your strategy for tackling the task? What sort of problems did you encounter and how did you overcome them?

Appreciate any insights or approaches you've found effective!