adotenv-lint
Optional lint rules, schema checks and verified fixes for
adotenv documents.
The linter is a companion crate: lint and schema policies do not belong in the
parser core. Findings are structured and never contain values. A finding may
carry a fix that is applied through adotenv's verified edit or format APIs.
use ;
let doc = parse_strict?;
let findings = lint;
assert_eq!;
let fixed = apply_fix?;
assert_eq!;
let example = parse_strict?;
let schema = from_example
.with_allow_unknown
.with_key;
let app = parse_strict?;
let schema_findings = lint_with_schema;
assert_eq!;
# Ok::
CLI
adotenv-lint [options] <file-or-directory>...
Examples:
adotenv-lint --key-order --key-case upper .env
adotenv-lint --example .env.example --no-unknown-keys --no-empty-values .env
adotenv-lint --format=json --recursive .
adotenv-lint --format=github --key-order .
adotenv-lint --recursive --exclude='target/**' .
adotenv-lint --max-input-bytes 1048576 --max-lines 10000 --max-items 50000 .env
adotenv-lint --fix --quote-style=unquoted --equals-spacing=none --max-output-bytes 1048576 .env
Without --fix the CLI is report-only. With --fix it applies compatible
verified fixes in batches (including two-pass QuoteStyle::Unquoted when
combined with trivia formatting) and rewrites changed files through a temporary
file in the same directory. With no path it searches the current directory
recursively, even when the configuration sets recursive = false. It
discovers .env and .env.* files with --recursive, skips .git, target,
node_modules, and the configured --example / [schema] example file during
directory discovery (explicit positional file arguments are still linted), and
exits with 0 for no findings (or all findings fixed), 1 for remaining
findings and 2 for usage, read, parse, limit or write errors. Human output
reports the configured severity as severity[rule]; JSON includes severity,
and GitHub annotations use the matching error, warning or notice level.
The Windows replace fallback is best-effort and not crash-atomic, matching the
CLI write contract.
Configuration
By default the command discovers .adotenv-lint.toml from the current
directory upward. Use --config <path> to select another file. Command-line
options override configuration values.
= "portable"
= "json"
= true
= true
= "upper"
= "double"
= "none"
= true
= ["target/**", "vendor/**"]
[]
= "error"
= "warning"
= "off"
= "error"
= "error"
= "warning"
= "error"
[]
= ".env.example"
= false
= false
[]
= "port"
[]
= ["development", "staging", "production"]
[]
= false
= true
= "bool"
All fields are optional. Unknown fields, unknown rules and unknown value shapes
are rejected so typos are visible. rules values are off, warning or
error; off disables the rule. key-order is enabled by its rule severity.
warning and error for key-case, quote-style, whitespace,
missing-key, unknown-key, empty-value and invalid-value set the
reported severity (and schema rules still require --example,
--no-empty-values, or [schema]). Relative schema.example paths resolve
against the directory containing .adotenv-lint.toml. Supported shape values
are any, bool, integer, unsigned-integer, port, float, url and
one-of (with one_of = [...], which also implies one-of). CLI options
override the corresponding configuration values, and --exclude replaces
configured ignore patterns. Ignore globs match a full path, a basename, a
path relative to the discovery root, or a path suffix for LSP document URIs,
so patterns such as target/** also apply to absolute editor URIs.
Library policy API
The same configuration model is available without the CLI:
use ;
let start = current_dir?;
let config = load_effective_config?;
let mut policy = default;
if let Some = config
assert_eq!;
# Ok::
load_effective_config handles explicit paths and ancestor discovery.
LintConfig::from_toml is available for callers that already have the source.
LintPolicy carries the resolved dialect, format, recursive mode, lint rules,
ignored glob patterns and optional [schema] configuration, so the language
server can share the CLI policy without a second parser or discovery
implementation.
Rules
The default policy reports duplicate keys only when no Schema is supplied.
Enable the other rules or supply a Schema explicitly:
| Rule | Default | Rationale |
|---|---|---|
duplicate-key |
on | Repeated declarations are usually accidental and make lookup depend on dialect policy. |
key-order |
off | Sorting is a style choice; some files group related keys deliberately. |
key-case |
off | Upper- and lowercase keys are both common in the wild. |
quote-style |
preserve | Quote policy depends on the project and on values that contain references. |
whitespace |
preserve | Whitespace policy is project-specific; formatting is opt-in. |
missing-key |
schema opt-in | Reports required keys declared in a Schema or .env.example that are missing. |
unknown-key |
schema opt-in | Reports undeclared keys when allow_unknown = false (--no-unknown-keys). |
empty-value |
schema opt-in | Reports empty (KEY=) or disallowed bare (KEY) values under a Schema or --no-empty-values. |
invalid-value |
schema opt-in | Reports literal values that do not match their configured ValueShape. |
Findings include the rule, configured severity, one-based line, source span,
key and an optional verified fix. Schema checks operate on decoded literal
values (Entry::value), never expand references or inspect the process
environment, and never include values in findings or attach automatic fixes.
Key-order findings cover a whole group and have no key; groups that
Document::sort_with_options refuses to reorder (for example due to a cycle or
exceeded EditOptions::max_references) are still reported without a fix
because no verified sort edit exists. apply_fix / apply_fix_with_options apply one fix and apply_fixes /
apply_fixes_with_options apply one compatible single-pass batch;
apply_batch / apply_batch_with_options apply independent renames together
followed by sorting and formatting (splitting QuoteStyle::Unquoted into its
own verified stage when combined with trivia rewrites, which single-pass
Document::format_with_options intentionally leaves unquoted-neutral) while
honoring EditOptions output, reference and parse bounds. Parse diagnostics
remain available from adotenv::parse.
This crate requires Rust 1.81 and does not change the core crate's no_std or
dependency-free guarantees.
Version 0.1.0 was the first published release of the companion crate. Version
0.2.0 added the shared configuration loader and ignored-path API used by the
CLI and language server. Version 0.2.1 fixes rooted ignore matching, severity
output and explicit whitespace-option precedence, and reports unsortable
key-order groups without a fix. Version 0.2.2 adopts adotenv 1.6.0 and adds
--max-input-bytes and --max-lines parse-limit flags across lint and --fix
runs. Version 0.2.3 adopts adotenv 1.7.0 and hardens multi-file error
reporting, bounded file reads, stdout error handling, explicit symlinked root
rejection and canonical path deduplication. Version 0.3.0 adopts adotenv
1.8.0, adds opt-in schema and .env.example presence-and-shape rules
(Schema, KeySchema, ValueShape, [schema], --example,
--no-unknown-keys, --no-empty-values), adds bounded lint_with_schema,
lint_with_options, apply_fix_with_options, apply_fixes_with_options and
apply_batch_with_options (with two-pass QuoteStyle::Unquoted convergence),
and adds --max-items, --max-diagnostics, --max-output-bytes,
--max-output-lines and --max-references CLI flags. Version 0.3.1 adopts
adotenv 1.8.1, hardens LintConfig::load with a regular-file check and 4 MiB
read cap (enforcing --max-input-bytes on CLI config reads), merges per-key
[schema.keys.*] overrides in LintConfig::apply, rejects empty closed
[schema] allow_unknown = false configs consistently with --no-unknown-keys,
and escapes control, line-separator and Bidi characters in human-output keys.
Version 0.3.2 extends key escaping to --format=github annotations and keeps
CLI .adotenv-lint.toml config reads capped at min(--max-input-bytes, 4 MiB).