Wefti

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.

The 12 wefti lint rules
RuleWhat it findsAutomatic fix
undefined-variableA variable reference is unset or likely misspelledunsafe
glob-without-configure-dependsfile(GLOB) feeds target sources without CONFIGURE_DEPENDSsafe
single-config-conditionA condition reads CMAKE_BUILD_TYPE without a single-config guardnone
unguarded-build-typeAn assignment or FORCE overwrite masks CMAKE_BUILD_TYPEunsafe
directory-optionsDirectory-wide commands such as add_definitions affect all targetsnone
obsolete-commandObsolete commands and unsupported cmake_minimum_required versionsunsafe
old-policyA policy set to OLDunsafe
fragile-if-expansionAn unquoted variable in if() breaks on empty or list valuessafe
legacy-block-end-argsArguments repeated in endif() and similar commandssafe
deprecated-link-scopeLINK_PUBLIC or LINK_PRIVATE in target_link_libraries()safe or unsafe
redundant-fatal-errorFATAL_ERROR in cmake_minimum_required()safe
redundant-policy-pinA policy pinned to NEW that the policy version already setssafe
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.

  1. Install Wefti. Install a release. The formatter, the linter, and the language server all come in the same install.
  2. Add .wefti.toml. Set indent_width, line_width, and command_case in the [fmt] table. Wefti reads the nearest .wefti.toml inside the project root.
  3. Check the format. Run wefti fmt --check. It exits with code 1 when a file needs formatting. Run wefti fmt --diff to see the changes first.
  4. Fix lint findings. Run wefti lint --fix for safe fixes. Add --unsafe-fixes when you accept changes that can alter configure behavior.
  5. Add the pre-commit hooks. Add wefti-fmt and wefti-lint to .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 formatting with Wefti
EditorSetup
VS Code, Cursor, WindsurfThe Wefti extension registers the default CMake formatter.
ZedThe Wefti extension starts wefti-lsp for CMake files.
Neovim 0.11 and laterCopy editors/neovim/wefti.lua. It sets up wefti-lsp and formats with wefti fmt through conform.nvim.
HelixAdd 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.