Zsort: Fast Import Organizer for Zig
New tool automatically organizes imports in Zig projects
1What is Zsort?
Zsort is an import organizer tool developed for the Zig programming language. Working similarly to isort in Python or goimports in Go, it automatically sorts, groups, and arranges @import declarations in Zig projects. The tool is compatible with Zig 0.15.2 and above, and can run on Linux, macOS, and WSL2. Zsort is specifically designed to ensure code consistency in large-scale Zig projects.
2Installation and Usage
Zsort can be installed via Homebrew using 'brew tap mstdokumaci/zsort' and 'brew install mstdokumaci/zsort/zsort' commands. Pre-built binaries are also available on GitHub Releases. To integrate into a project, specific steps need to be added to the build.zig file. Zsort checks import ordering in 'check' mode and rewrites files in place in 'fix' mode. For example, 'zsort check src/' command checks imports in a directory while 'zsort fix .' command organizes imports throughout the entire project.
3Features and Capabilities
Zsort groups imports into four distinct bands and sorts within each band. The tool preserves comments attached to imports and supports import blocks at file endings. It also offers features like banning specific import prefixes (--ban-prefix) and placing import blocks at file endings (--bottom). Zsort automatically skips .gitignore entries, .git, .zig-cache, zig-cache, and zig-out directories, and ignores files marked with // zsort: skip comments.
4Sorting Rules
Zsort groups imports into four bands: std library, external libraries, project modules, and member imports. Within each band, plain imports come before member imports, and both are sorted by path name. Between imports with the same path, sorting is based on the complete code line. Zsort produces deterministic output regardless of input order and leaves a blank line between import blocks and following code.
5Functionality of Fix Mode
Fix mode sorts and groups file header imports according to the above bands. It moves scattered imports and aliases found deeper in files to their appropriate bands. It preserves comments attached to imports and ensures a blank line between import blocks and subsequent code. //! doc comments and original line endings (LF/CRLF) are preserved. With --bottom option, the entire import block can be moved to file ending, producing the layout used by zig init templates.
6Integration and Automation
Zsort can be integrated with pre-commit hooks to automatically check import ordering before each commit. By adding to .pre-commit-hooks.yaml file, 'zsort' hook runs in check mode (fails on unsorted imports) or 'zsort-fix' hook runs in fix mode (rewrites files in place). Hooks require zsort on $PATH and only check .zig files. Extra flags can be passed via args, for example to ban specific import prefixes.
7Development and Contribution
Zsort is an open-source project released under MIT license. Development environment setup and lint checks are documented in CONTRIBUTING.md, while version history is in CHANGELOG.md. The project welcomes contributions from the Zig community. Zsort aims to improve code consistency and readability, particularly for Zig developers working in large teams.
8Why It Matters
Zsort standardizes import organization in Zig projects, making codebases more consistent and easier to maintain. Especially in open-source projects and when working with large teams, automatic import organization tools save time and minimize style debates. Zsort's functionality comparable to isort in Python and goimports in Go contributes to the maturation of Zig ecosystem and supports professional use of the language.
Original article: Hacker News: Show HN — Zig için Hızlı Import Düzenleyici: Zsort