adotenv 1.7.3

A tooling-grade .env parser: literal values (no variable expansion), exact byte spans, and edits that leave the rest of the file untouched.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# Command-line tool

`adotenv-cli` checks, formats, compares and loads `.env` files from a shell or
a CI job. It uses the library APIs, so it reads files exactly as the library
does.

## Install

```console
cargo install adotenv-cli --locked
```

This installs one binary, `adotenv-cli`. It needs Rust 1.81 or later to build.
`adotenv-cli --help` prints the usage and `adotenv-cli --version` prints the
version.

## Commands

```text
adotenv-cli [--dialect <portable|node|python|compose|docker>] [--dialect=<value>]
            [--max-input-bytes <n>] [--max-lines <n>] <command> [options]

get <file> <key>
check [--format human|json|github] [--json] [--source] [-r|--recursive] [--exclude <path>] <path>...
fmt [--write|--check] [--sort] [-r|--recursive] [--exclude <path>] <path>...
diff [--format human|matrix|json] [--json] [--mask] <baseline> <file>...
example <file>
load <directory> [mode]
set [--max-output-bytes <n>] [--max-output-lines <n>] <file> <key> <value>
run <directory> [mode] -- <program> [args...]
```

| Command | Does |
|---|---|
| `get` | Prints the effective value of a key and a line break, without quoting. |
| `check` | Reports diagnostics for each file. |
| `fmt` | Prints formatted text, checks formatting with `--check` or rewrites files with `--write`. `--sort` also sorts each group. |
| `diff` | Compares each file with the baseline, the first path. |
| `example` | Prints the document with assigned values removed. |
| `load` | Prints the merged cascade of a directory as Portable lines. |
| `set` | Changes the value of a key in place. |
| `run` | Loads a cascade and runs a program with the loaded values. |

`set` changes an existing declaration. The key must be declared exactly once;
a missing or duplicated key is an error, and `set` never adds a key. When the
value is already current, `set` leaves the file untouched.

`--json` is short for `--format json`, `--format=<value>` is accepted, and `-r`
is short for `--recursive`. Two different formats in one command are a usage
error, as is `--source` with JSON or GitHub output, and `--write` with
`--check`. Arguments must be valid UTF-8.

### Dialects

`--dialect` selects the reading rules for every file in the command. It can
appear before the command, or anywhere after the command name before `--`. For
`set`, a `--dialect` token after the file argument is a key or value rather
than a global option; put the global option earlier or use `--`. The
`--dialect=<value>` form is equivalent and may be specified only once. Unknown
options are usage errors rather than paths, unless they follow `--`.

| Value | Library preset |
|---|---|
| `portable` (default) | `Dialect::PORTABLE` |
| `node` | `Dialect::NODE_DOTENV` |
| `python` | `Dialect::PYTHON_DOTENV` |
| `compose` | `Dialect::COMPOSE` |
| `docker` or `docker-raw` | `Dialect::DOCKER_RAW` |

See [Dialects](dialects.md) for what each preset accepts.

### Limits

`--max-input-bytes <n>` and `--max-lines <n>` (or their `=` forms) are global
options accepted in the same positions as `--dialect` and specified at most
once. They forward `ParseOptions::with_max_input_bytes` and
`ParseOptions::with_max_lines` to every file parse and cascade load. `check`
reports limit violations as `dotenv.limit-exceeded` diagnostics (exit code 1);
strict commands (`get`, `fmt`, `diff`, `example`, `load`, `set`, `run`) exit with code 2.

`set` also accepts `--max-output-bytes <n>` and `--max-output-lines <n>` before
`<file>` to bound the edited file through `EditOptions` before writing.

## Values in output

Values may be secrets. `check` prints source lines only with `--source`. These
commands print values or text that can contain them:

- `get` prints the effective value.
- `load` prints every loaded value.
- `diff` prints values in every format: human, matrix and JSON. `diff --mask`
  replaces compared values with `***`.
- `fmt` prints or writes the whole document.
- `example` removes values but keeps comments verbatim, including anything
  secret written in a comment.

## Diagnostics

`check` prints one line per diagnostic: path, one-based line and column,
severity, code and message. A stripped leading byte-order mark (or BOM run under
`BomPolicy::StripAll`) is hidden on line 1, so it does not count in the human or
GitHub column; `BomPolicy::Keep` and `BomPolicy::Error` keep it in column 1.
JSON spans still count its bytes. A summary line follows when any file has a diagnostic or could not be
read; a read failure counts as one error and one affected file. A clean run
prints nothing.

```text
$ adotenv-cli check bad.env
bad.env:1:1: error[dotenv.invalid-key]: key does not match the selected dialect
bad.env:3:1: warning[dotenv.duplicate-key]: duplicate key; lookup follows the selected dialect policy
1 error, 1 warning, 0 info in 1 file
```

`--source` adds the source line and a caret under the span. A leading
byte-order mark is omitted from the source line. Human diagnostics go to
standard output and read errors to standard error. JSON reports a read error as
a `files` entry on standard output, and GitHub output emits an annotation
there. Human output is not a machine contract; use JSON or GitHub output in
scripts. The codes are listed in
[Diagnostic codes](../reference/diagnostics.md).

### `check --format json`

The report is JSON. The CLI emits one line; this example is formatted for
readability:

```json
{
  "version": 1,
  "files": [
    {
      "path": "app.env",
      "ok": false,
      "diagnostics": [
        {
          "line": 1,
          "severity": "error",
          "code": "dotenv.invalid-key",
          "message": "key does not match the selected dialect",
          "span": {
            "start": 0,
            "end": 7
          },
          "secondary_span": null
        }
      ]
    }
  ]
}
```

Every field is listed below, in output order. The fields, codes, severities and
spans are stable.

| Object | Field | Type | Meaning |
|---|---|---|---|
| check report | `version` | number | Schema version, `1`. |
| check report | `files` | array of check file | One entry per found file, in argument order. With `--recursive`, found files are sorted and duplicates resolved to the same path are removed. A path that could not be searched comes first, with `ok:false` and an `error`. |
| check file | `path` | string | The path as given or found, the path that could not be searched, or `-` or `--stdin` for standard input. Bytes that are not UTF-8 become U+FFFD. |
| check file | `ok` | boolean | `false` when the file has an error diagnostic, could not be read, or its search path failed. |
| check file | `error` | string | Present only when the file could not be read, is not UTF-8, or its search path failed. The message includes the path. |
| check file | `diagnostics` | array of diagnostic | The file's diagnostics. Empty when `error` is present. |
| diagnostic | `line` | number | One-based line of the primary span. |
| diagnostic | `severity` | string | `error`, `warning` or `info`. |
| diagnostic | `code` | string | Stable diagnostic code, such as `dotenv.invalid-key`. |
| diagnostic | `message` | string | Human-readable text. Match on `code` instead. |
| diagnostic | `span` | span | Primary source range. |
| diagnostic | `secondary_span` | span or null | A related range, such as the first declaration of a duplicate key, or `null`. |
| span | `start` | number | Byte offset into the file. A leading byte-order mark is counted. |
| span | `end` | number | Byte offset just past the range. |

A file with no diagnostics produces an empty `diagnostics` array. The shared
JSON Schema is `tests/vectors/schema/check-report.v1.json` and is published at
<https://asamarts.github.io/adotenv/schema/check-report.v1.json>.

### `check --format github`

One GitHub Actions workflow command per line:

```text
::<level> file=<path>,line=<line>,col=<column>,title=<code>::<message>
::error file=<path>,title=adotenv::<error>
```

The first form reports a diagnostic. `<level>` is `error`, `warning` or `notice`
for an `info` diagnostic. `<column>` is one-based and counts Unicode scalar
values, not a leading byte-order mark. The second form reports a file that
could not be read or a path that could not be searched. In `<path>` and
`<code>`, `%`, CR, LF, `:` and `,` are percent-encoded. In `<message>` and
`<error>`, `%`, CR and LF are percent-encoded. This format is stable.

```yaml
- run: cargo install adotenv-cli --locked
- run: adotenv-cli check --format github --recursive
```

## Comparison

`diff` compares effective declarations. In human output, each candidate starts
with a `baseline -> file` header, followed by one line per difference: `-` for
a key that takes part in lookup only in the baseline, `+` for one that takes
part only in the candidate and `~` for a changed value, state or export prefix.
A value that contains a line break or lone CR can make that difference span
more than one physical output line. A candidate with no differences prints
`(no differences)`. This applies when there is exactly one candidate as well.

```text
$ adotenv-cli diff base.env app.env
base.env -> app.env
~ B: two -> changed
~ C: 3 (export) -> 3
- BARE=<bare>
+ D=""
```

`--mask` replaces every non-empty compared value with `***` in human, JSON and
matrix output. Keys, states, export prefixes and missing/unset markers remain
visible. It affects output only; comparison still uses the real values.

`--format matrix` prints one table with a row per key and a column per file.
A cell shows the value, `""` for an empty value, `<bare>` for a declaration
without `=`, `<unset>` for a missing key and an `export ` prefix for exported
declarations. Neither human output nor the matrix is a machine contract.

### `diff --format json`

The report is JSON. The CLI emits one line; this example is formatted for
readability:

```json
{
  "version": 1,
  "baseline": "base.env",
  "files": [
    {
      "path": "app.env",
      "ok": true,
      "differences": [
        {
          "key": "A",
          "kind": "value",
          "left": {
            "state": "value",
            "value": "1",
            "exported": false
          },
          "right": {
            "state": "value",
            "value": "2",
            "exported": false
          }
        }
      ]
    }
  ]
}
```

Every field is listed below, in output order. The schema is stable.

| Object | Field | Type | Meaning |
|---|---|---|---|
| diff report | `version` | number | Schema version, `1`. |
| diff report | `baseline` | string | The baseline path as given. |
| diff report | `files` | array of diff file | One entry per candidate, in argument order. |
| diff file | `path` | string | The candidate path as given. |
| diff file | `ok` | boolean | Always `true`. A file that cannot be read or parsed stops `diff` with exit `2` and no JSON. |
| diff file | `differences` | array of difference | Baseline keys in the order of their first declaration that takes part in lookup, then the keys that take part in lookup only in the candidate, in the same order. Keys whose declarations agree are left out. |
| difference | `key` | string | The key. |
| difference | `kind` | string | The first difference found, checked in this order: `only-left` (takes part in lookup only in the baseline), `only-right` (only in the candidate), `state`, `value`, `export`. |
| difference | `left` | value or null | The baseline declaration, or `null` when the key is absent. |
| difference | `right` | value or null | The candidate declaration, or `null` when the key is absent. |
| value | `state` | string | `bare` for a key without `=`, `empty` for an empty value, otherwise `value`. |
| value | `value` | string or null | The decoded literal value: `""` for `empty`, `null` for `bare`, or `"***"` for a non-empty value under `--mask`. |
| value | `exported` | boolean | Whether the declaration has an `export` prefix. |

The shared JSON Schema is `tests/vectors/schema/diff-report.v1.json` and is
published at <https://asamarts.github.io/adotenv/schema/diff-report.v1.json>.

## Formatting

`fmt` applies `FormatOptions::normalize_groups()`:

- one space on each side of `=`, so `A=1` becomes `A = 1`, in dialects where
  that keeps the value;
- trailing spaces and tabs removed;
- each run of blank lines collapsed to one;
- quote styles kept;
- a final line break ensured.

`--sort` also sorts declarations within each blank-line group; see
[Formatting and sorting](formatting.md). Formatting never changes a value. A
change that would alter meaning is refused and exits `2`.

With several files, printed output starts each file with a `==> path <==`
header. `--check` prints `path: would reformat` for each file that would
change and exits `1` if any would. `--write` rewrites changed files in place
and leaves an unchanged file untouched. Diagnostics are reported on standard
error, and a file with error diagnostics is still formatted.

## `load` output

`load` prints the merged cascade as a Portable document: one `KEY=value` line
per key, sorted by key, with LF line endings. Values are quoted and escaped by
the core Portable encoder, so the output parses back to the same keys and values
with the Portable dialect, whichever dialect read the files. No value spans
several lines.

When a key is not a Portable key, or a value contains a control character other
than tab, CR or LF, `load` prints nothing and exits `2`. The error names the key
but not the value.

A cascade file that does not exist is skipped, so a directory without any
cascade files, or one that does not exist, loads nothing: `load` prints nothing
and exits `0`.

## `run` environment

`run` starts the program with the parent environment plus the loaded values. A
loaded value replaces a parent variable of the same name. Other parent variables
are passed through unchanged. The parent environment is not changed. When no
cascade file exists, the program runs with the parent environment alone.

On Windows the child environment is case-insensitive: keys that differ only by
case and values longer than 32,767 UTF-16 code units are rejected before the
child starts. Loaded files are trusted like code. A loaded `PATH` changes which
program runs, and variables such as `LD_PRELOAD` change how it runs.

Values stay literal: `run` does not expand references or run command
substitutions. The loader omits empty keys; keys that remain and contain `=`
or NUL, and values that contain NUL, are rejected before the program starts.

## Discovery

With `--recursive`, `check` and `fmt` search each directory argument for
`.env` and `.env.*` files. With no path they search `.`. A file argument is
included whatever its name, and `-` reads standard input.

Without `--recursive`, every path argument must be a file. A directory is a
read failure, and `check` or `fmt` with no path is a usage error.

The search skips `.git`, `target`, `node_modules` and symbolic links. A
directory argument that is a symbolic link is refused; a symbolic link that
names a file is an explicit file argument and is followed once.

A path that cannot be searched, such as a missing root, a refused symbolic
link or an unreadable directory, is reported. Human output writes it to
standard error, JSON adds a `files` entry with `ok:false` and `error`, and
GitHub output writes an `::error` annotation. The remaining paths are still
searched and processed, and the command exits `2`.

When the search finds no files and no path failed, `check` prints an empty
report (`{"version":1,"files":[]}` for JSON), `no .env files found` goes to
standard error, and the exit code is `0`. `fmt` behaves the same way.

`--exclude <path>` needs `--recursive` and can be repeated. Each exclude is
relative to each directory argument, or to the parent directory of a file
argument. It removes the path and everything below it, comparing whole path
components. A leading `./` is ignored and an empty exclude has no effect. An
absolute exclude is compared with paths as they are spelled, so it only matches
below an absolute directory argument.

## Input and output

File-reading commands accept `-` or `--stdin` for standard input, at most once
per command. `set` and `fmt --write` need a file and refuse standard input.
`load` and `run` also refuse standard input because they take a directory.
`--` ends option parsing for `check`, `fmt`, `diff`, `get`, `example`, `load`
and `set`, so a leading-dash path can be named explicitly; use it before the
first path argument. For `run`, `--` separates the directory and optional mode
from the program and its arguments.

When the result is unchanged, `fmt --write` and `set` do not replace the
file. Its inode, hard links, modification time, permissions and extended
attributes stay as they are.

`fmt --write` and `set` resolve the write target once before reading and do
not resolve it again while replacing it. On Unix they check its device, inode,
size and modification time before the temporary file and immediately before
the replace, and fail if it changed after the read. They replace a changed file through a temporary
file in the same directory:

- When the path is a symbolic link, the file it finally points to is replaced in
  that file's directory, and the link is kept.
- A path that is a directory or another non-regular file is refused.
- Hard links are not kept. The path gets a new file, and other links to the old
  file keep the old contents.
- Extended attributes and ACLs are not copied.

On Unix the temporary file is created with mode `0600` before anything is
written. It then receives the original owner, group and mode, is synced and is
renamed over the original. The directory is synced after the rename. The
original owner is kept when the running user may set it, for example as root;
otherwise the new file belongs to the running user. The group is kept when the
running user may set it. If it cannot be kept and the mode grants the group any
access, the write fails and the file is unchanged.

On Windows the command writes a temporary file, applies the original
permissions and attempts a direct replace. If that fails, it moves the old file
to a backup, installs the new file and restores the backup on error. The Windows
fallback is not crash-atomic.

## Exit codes

Every command except `run` exits with one of these codes:

- `0`: success.
- `1`: findings. `check` or `fmt` found an error diagnostic, `fmt --check` found
  a file that would change, `diff` found a difference, or `get` did not find the
  key.
- `2`: the command could not complete. This covers usage errors, files that
  cannot be read or are not UTF-8, failed writes, and input the command cannot
  process: an invalid document for `get`, `diff`, `set`, `load` or `run`, an
  edit that `set` cannot apply, `load` output that has no Portable spelling, or
  a `fmt` change that would alter meaning. Failed writes include writes to
  standard output and standard error. A closed output pipe also exits `2`,
  without a message.

`check` and `fmt` process every file and exit with the highest code, so a file
that fails takes precedence over findings in other files.

Warning and info diagnostics do not change the exit code. `example` reports
non-limit diagnostics on standard error and exits `0`, or exits `2` without
output when a parse limit is exceeded.

`run` exits `2` for a usage error, when it cannot load the cascade, when a
loaded key or value cannot be passed to a child, or when the program cannot be
started. Otherwise it exits with the child's exit code. On Unix, a child ended
by signal `N` exits `128 + N`, as in POSIX shells. A child exit code outside
`0` to `255`, such as a Windows `NTSTATUS` crash code, exits `255`.

## Scope boundaries

The CLI never runs command substitutions from loaded files. To run them, use
the library: `Document::resolve_with_commands` with a callback, or
`exec::Shell` from the `exec` feature. To change the environment of the current
process, use [`adotenv-process`](../guide/process.md).