Skip to content

Getting started

adotenv reads .env files as source code: it keeps every byte, reports where each token is and changes text only through verified edits. This page installs the crates and walks through the main tasks.

[dependencies]
adotenv = "1"

The library needs Rust 1.81 or later and has no required dependencies. Optional features add the parts that touch the outside world:

Feature Adds Needs
std (default) Standard library support. Error types implement std::error::Error with or without it
load Reading .env cascades and file lists from disk std
serde Deserializing documents, and with load loaded environments, into typed structs
exec exec::Shell, a command runner for command substitution std
adotenv = { version = "1", features = ["load", "serde"] }

Disable default features for a no_std build that uses alloc. Parsing, lookup, edits, formatting, sorting, comparison and resolution stay available.

adotenv = { version = "1", default-features = false }
Terminal window
cargo install adotenv-cli --locked

This installs the adotenv-cli binary:

Terminal window
adotenv-cli check .env
adotenv-cli fmt --check .env
adotenv-cli diff .env.example .env

See Command-line tool for every command, the JSON output and the exit codes.

Crate Version Purpose
adotenv-process 1 Apply a loaded environment to the current process. See Process environment.
adotenv-config 0.12 A config 0.15 source. Needs Rust 1.88. See config and Figment.
adotenv-figment 0.12 A Figment 0.10 provider. See config and Figment.
adotenv-lint 0.3 Opt-in lint rules, schema / .env.example checks and verified fixes. See adotenv-lint on docs.rs.
adotenv-lsp 0.7 Language server and transport-free LSP library with schema diagnostics. See Language server.

The adapters keep their own 0.x versions, so a new major version of config or Figment is a minor release of the adapter.

parse_strict returns a Document or the first error. Values are literal: nothing is expanded.

let source = "# app\nexport PORT=8080\nURL=\"${HOST}/api\" # base\n";
let doc = adotenv::parse_strict(source).unwrap();
assert_eq!(doc.get("PORT"), Some("8080"));
assert_eq!(doc.get("URL"), Some("${HOST}/api"));
let url = doc.get_entry("URL").unwrap();
assert_eq!(url.line(), 3);
assert_eq!(url.raw_value(), Some("\"${HOST}/api\""));
assert_eq!(url.inline_comment(), Some("# base"));

parse never fails. It returns the document and at most 10,000 ordinary diagnostics, plus the first error after that budget, so a tool can report problems without losing the rest of the file. If more problems exist, the last diagnostic is dotenv.too-many-diagnostics:

let (doc, diagnostics) = adotenv::parse("BAD KEY=value\nGOOD=kept\n");
assert_eq!(doc.get("GOOD"), Some("kept"));
assert_eq!(diagnostics[0].code().as_str(), "dotenv.invalid-key");
assert_eq!(diagnostics[0].line(), 1);

Parsing and lookup covers duplicates, raw tokens, spans and positions. Dialects covers files written for Node, Python, Docker Compose or Docker.

Edits never change the document. They return VerifiedEdits: the new text plus the text edits that produce it. The output is parsed again and checked before it is returned.

let doc = adotenv::parse_strict("A=1\nB=2\n").unwrap();
let edited = doc.set("B", "two words").unwrap();
assert_eq!(edited.as_str(), "A=1\nB=two words\n");
// The value is quoted only when it needs to be.
let quoted = doc.set("B", "a # b").unwrap();
assert_eq!(quoted.as_str(), "A=1\nB=\"a # b\"\n");
let renamed = doc.rename("A", "ALPHA").unwrap();
assert_eq!(renamed.as_str(), "ALPHA=1\nB=2\n");

Editing covers batches, insert positions and how to apply the text edits yourself. Formatting and sorting covers whole-file changes.

With the load feature, load_cascade reads .env, .env.local, .env.{mode} and .env.{mode}.local from one directory. Later files win.

use adotenv::load::load_cascade;
let loaded = load_cascade("/srv/my-app", "production")?;
if let Some(port) = loaded.get("PORT") {
let origin = loaded.origin("PORT").unwrap();
println!("PORT={port} from {}:{}", origin.path().display(), origin.line());
}

Loading never changes the process environment. Pass values to a child process yourself, use adotenv-cli run, or use adotenv-process. Loading files covers origins, reports and values-only loading.