charmlint: a linter for charms

TL;DR: uvx charmlint /path/to/charm checks a charm against our best practices. Please try it, and tell us how it should get better.

There’s a lot of charm best practice written down, in a lot of places, but it frequently evolves and it’s hard to keep track of everything. charmlint is an tool to validate against these, on your machine or in CI, whether a human or agent is working in the charm.

We’ve talked about doing this for a long time, but this cycle spun it out of our work on AI-assisted charming. We found that an agent writing a charm got more out of something to check its work against than out of more instructions. People do too, so charmlint isn’t tied to any agent or model: it works the same whether you run it or Claude Code, OpenCode, Pi, or something else does.

There are currently 50+ rules, covering actions, charmcraft.yaml, config, metadata, Pebble, security, status, testing, and more (the full list). Every rule is measured before it lands: we run it over the few hundred public charms we know of, and count how many findings are real problems and how many are false positives. A linter that cries wolf gets turned off, so we’d rather ship fewer rules that you can trust.

There are plenty more rules in the queue. We’re also planning a preview mode, following ruff’s model (charmlint is modelled on ruff in a lot of places), so new rules can be tried before they’re on by default. Until then, a minor release can add rules, so pin the version if you need CI to stay stable (details).

The current plan is that the core will stay deterministic: same charm, same findings, no model needed. However, some problems are out of reach of a static rule, such as an event.defer() that can loop forever, or a config-changed handler that restarts everything when one unrelated option changes. #36 is about an optional extra that uses a model for those. That might be a general model reviewing the code, but I’m also interested in the newer decision models, such as TypeSafe’s Jev (and OpenAI’s Decision API and the many others). For example, asking a question like ‘does this status message tell the operator what to do?’ and getting back an answer with a probability. They’re cheap and fast, and the probability means a finding can be held back unless the model is confident, which suits a tool that would rather miss something than cry wolf. They can also be measured the same way the rules are. None of this is decided, so if you have opinions, #36 is the place.

charmlint isn’t trying to do everything. @michaeldmitry’s flaplint finds charms that write unordered data to databags, files, Pebble layers or secrets, so Juju sees a change on every hook (their post explains it well). We currently feel that the right shape is several specialist tools in the loop, alongside ruff and pyright (speaking of which, we don’t want any duplication of ruff or type checker functionality in charmlint), rather than one tool that tries to cover everything. If you’re building one, we’d love to hear about it.

Please run it over your charms. If it flags something that’s fine, that’s potentially a bug and we want to know about it, so open an issue. If there’s a best practice you think should be a rule, issues and PRs are both welcome (CONTRIBUTING.md explains how we measure a new rule). Discussion is welcome here too.

If you’re at Canonical and coming to the upcoming sprints, there are a couple of sessions planned on measuring and exposing charming quality and compliance, and charmlint falls into that too. Come along, particularly if you review charms.

8 Likes

@sbouffard I see a lot of overlap between charmlint and our tool pqf. Do you think it would be interesting to use charmlint internally or maybe combine the two in some way?

@sgeorget I was also thinking about combining this along with charmkeeper. If we can orchestrate the two together - charmlint / pqf does the evaluation to find the delta in a charm in terms of what’s missing, and charmkeeper to go reduce the delta.

Definitely something I will look at!

@tony-meyer Thank you very much for this tool, it definitely looks promising!

I opened an issue with a suggestion for a new rule: Rule: warn if we are logging inside a loop. · Issue #442 · canonical/charmlint · GitHub

Thanks!

Jose

@swetha1654 @sgeorget Definitely see charmlint as a useful tool we could enable across our charms, maybe starting with our gold charms? We could add a metric within the PQF V1 that tracks those results and something we commit towards aligning on across all gold and silver products.