A cmake-format, cmakelang, and gersemi alternative in Rust
Formatting CMake files usually means a Python package and a second tool for linting. Wefti does both in the program that also configures your project.
Run wefti fmt to format CMakeLists.txt and *.cmake files, and wefti lint --fix to apply fixes for 12 lint rules. Both are part of one Rust program with no Python, and both read settings from .wefti.toml. The same engine formats in VS Code, Cursor, Zed, Neovim, and Helix, and .pre-commit-hooks.yaml has hooks for pre-commit.
What wefti fmt does
wefti fmt changes only whitespace and, if you ask, the letter case of command names. Before it writes a file, a second check confirms that the syntax tree, the protected text, and the comment positions did not change. It refuses to overwrite a symbolic link that you name directly.
wefti fmt # format the project in place
wefti fmt --check # exit 1 if any file needs formatting
wefti fmt --diff # print unified diffs without writing
wefti fmt --stdin --stdin-filepath cmake/deps.cmake < cmake/deps.cmake
Settings live in .wefti.toml. An unknown key is a configuration error, so a typo does not pass silently.
exclude = ["third_party/**", "generated/*.cmake"]
[fmt]
indent_width = 2 # 1 to 16, default 2
line_width = 100 # 20 to 1000, default 100
command_case = "lower" # "preserve" (default), "lower", or "upper"
end_of_line = "lf" # "preserve" (default), "lf", or "crlf"
[lint]
disable = ["directory-options"]
The file search skips .git, CMakeFiles, nested build trees, symbolic links, and the exclude patterns. cmake-format and gersemi use their own configuration files, so write the settings again in .wefti.toml when you move.
What wefti lint --fix checks
wefti lint checks CMakeLists.txt and *.cmake files against 12 static rules. A safe fix keeps the configure behavior. An unsafe fix can change it, so Wefti applies it only with --unsafe-fixes. The fix changes only the bytes of the finding, so your comments and layout stay.
| Rule | What it finds | Automatic fix |
|---|---|---|
| undefined-variable | A variable reference is unset or likely misspelled | unsafe |
| glob-without-configure-depends | file(GLOB) feeds target sources without CONFIGURE_DEPENDS | safe |
| single-config-condition | A condition reads CMAKE_BUILD_TYPE without a single-config guard | none |
| unguarded-build-type | An assignment or FORCE overwrite masks CMAKE_BUILD_TYPE | unsafe |
| directory-options | Directory-wide commands such as add_definitions affect all targets | none |
| obsolete-command | Obsolete commands and unsupported cmake_minimum_required versions | unsafe |
| old-policy | A policy set to OLD | unsafe |
| fragile-if-expansion | An unquoted variable in if() breaks on empty or list values | safe |
| legacy-block-end-args | Arguments repeated in endif() and similar commands | safe |
| deprecated-link-scope | LINK_PUBLIC or LINK_PRIVATE in target_link_libraries() | safe or unsafe |
| redundant-fatal-error | FATAL_ERROR in cmake_minimum_required() | safe |
| redundant-policy-pin | A policy pinned to NEW that the policy version already sets | safe |
wefti lint --fix # apply safe fixes, then report what remains
wefti lint --fix --unsafe-fixes # also apply unsafe fixes
wefti lint --diff --unsafe-fixes # print every fix as a unified diff
wefti lint --deny-warnings --sarif > wefti-lint.sarif
Suppress one rule on one command with # wefti-lint: disable=<rule> on the line before it. The --sarif option writes a SARIF 2.1.0 log for CI code scanning.
Set up formatting, linting, and pre-commit
Follow these steps to replace a Python formatter in a project.
- Install Wefti. Install a release. The formatter, the linter, and the language server all come in the same install.
- Add
.wefti.toml. Setindent_width,line_width, andcommand_casein the[fmt]table. Wefti reads the nearest.wefti.tomlinside the project root. - Check the format. Run
wefti fmt --check. It exits with code 1 when a file needs formatting. Runwefti fmt --diffto see the changes first. - Fix lint findings. Run
wefti lint --fixfor safe fixes. Add--unsafe-fixeswhen you accept changes that can alter configure behavior. - Add the pre-commit hooks. Add
wefti-fmtandwefti-lintto.pre-commit-config.yaml, as shown below.
repos:
- repo: https://github.com/wefti/wefti
rev: v0.3.1
hooks:
- id: wefti-fmt
- id: wefti-lint
The hooks run the wefti that is on your PATH. wefti-fmt fails when a file needs formatting and changes nothing. Use wefti-fmt-fix instead when you want the hook to format the files. wefti-lint passes on warnings unless you add args: [--deny-warnings].
Format in your editor
wefti-lsp formats with the same engine as wefti fmt, so the editor and the command line agree.
| Editor | Setup |
|---|---|
| VS Code, Cursor, Windsurf | The Wefti extension registers the default CMake formatter. |
| Zed | The Wefti extension starts wefti-lsp for CMake files. |
| Neovim 0.11 and later | Copy editors/neovim/wefti.lua. It sets up wefti-lsp and formats with wefti fmt through conform.nvim. |
| Helix | Add the tables from editors/helix/languages.toml. Helix then formats on save. |
Frequently asked questions
What is an alternative to cmake-format and cmakelang?
wefti fmt formats CMake files and wefti lint --fix fixes lint findings. Both are in one Rust program with no Python. They read .wefti.toml instead of a cmake-format configuration file.
What is an alternative to gersemi?
wefti fmt is a lossless formatter with --check and --diff. It confirms that the syntax tree and comments are unchanged before it writes a file.
Is there a CMake linter written in Rust?
Yes. wefti lint has 12 rules, automatic fixes, --deny-warnings, and SARIF 2.1.0 output. wefti unused also finds unused packages, duplicate links, and unused variables.
How do I add a CMake pre-commit hook?
Add the wefti-fmt and wefti-lint hooks from https://github.com/wefti/wefti to .pre-commit-config.yaml. The hooks arrive with version 0.3.1, so the v0.3.1 tag works after that release. Install Wefti first, because the hooks run the wefti on your PATH.
How do I format CMakeLists.txt in VS Code, Neovim, Zed, or Helix?
Install the Wefti extension for VS Code, Cursor, Windsurf, or Zed. For Neovim copy editors/neovim/wefti.lua, and for Helix add editors/helix/languages.toml. Each setup formats with wefti-lsp or wefti fmt.
Will wefti fmt change my comments or the meaning of my files?
It changes whitespace and, if you set command_case, the case of command names. A second check confirms that the syntax tree, protected text, and comment positions are the same before it writes.