dotenv

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2026 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package dotenv wraps the joho/godotenv package to load environment files.

The env must be one of "development", "test", "production", or "agent". The "agent" environment is reserved for the coding agent's local work.

Load reads the following files depending on env. The first file has the highest precedence and .env has the lowest:

Priority  Filename            .gitignore?  Secrets?  Notes
Highest   .env.{{env}}.local  Yes          Yes       Environment-specific local overrides
2nd       .env.local          Yes          Yes       Local overrides
3rd       .env.{{env}}        No           Never     Shared environment-specific variables
Lowest    .env                No           Never     Shared for all environments

A file that does not exist is skipped, and so is a name that is not a regular file, such as a directory.

Errors

Load returns ErrMissingEnvironment or ErrUnknownEnvironment for a bad env, before it looks at any file.

Load returns ErrClosedDirectory when the directory itself may not be searched, as when root runs a command as another user from a directory that user is closed out of, such as /root. Nothing has been loaded then. A caller that should take its settings from the process environment and its flags in that case can ignore it:

if err := dotenv.Load(env); err != nil && !errors.Is(err, dotenv.ErrClosedDirectory) {
	return err
}

Any other failure to stat, read, or parse a file that is there is returned wrapped with the file's name, and errors.Is matches its cause, such as fs.ErrPermission for a file that may not be read. Loading stops at that file: the files of higher precedence have already been applied, and those of lower precedence have not. ErrClosedDirectory also matches fs.ErrPermission, so test for it first.

Load never overwrites a variable that is already set, even if it is empty. The real process environment always wins over every file, and the first file in the table above that defines a key wins over the later ones.

Variable references such as ${NAME} are expanded by godotenv, which sees only the keys defined earlier in the same file. A reference to a key from another file, or from the process environment, expands to the empty string.

WARNING: we are incompatible with bkeepers/dotenv since we load `.env.local` in test. Read https://github.com/bkeepers/dotenv/issues/418 for the history of this decision.

Index

Constants

View Source
const (
	// ErrMissingEnvironment is returned when env is the empty string.
	ErrMissingEnvironment = Error("missing environment")

	// ErrUnknownEnvironment is returned when env is not one of
	// "development", "test", "production", or "agent". The match is
	// case-sensitive.
	ErrUnknownEnvironment = Error("unknown environment")

	// ErrClosedDirectory is returned when the directory that holds the
	// environment files may not be searched, as when root runs a command
	// as another user from /root. No file has been loaded when it is
	// returned, so the process environment is unchanged.
	//
	// The error also wraps the underlying *fs.PathError, so errors.Is
	// reports it as fs.ErrPermission too. A caller that wants to carry on
	// without the files must test for ErrClosedDirectory, not
	// fs.ErrPermission, which also matches a single unreadable file.
	ErrClosedDirectory = Error("closed directory")
)

Variables

This section is empty.

Functions

func Load

func Load(env string) error

Load loads environment files from the working directory. See the package documentation for supported environments and precedence.

Types

type Error

type Error string

Error is a string that implements the error interface, allowing sentinel errors to be declared as untyped constants.

func (Error) Error

func (e Error) Error() string

Error implements the error interface.

type Loader

type Loader struct {
	// contains filtered or unexported fields
}

Loader loads environment files from a filesystem into the process environment.

func New

func New(fsys fs.FS) *Loader

New creates a loader that reads environment files from the root of fsys. The filesystem must not be nil.

func (*Loader) Load

func (l *Loader) Load(env string) error

Load uses the joho/godotenv parser to load environment files from the root of the loader's filesystem. See the package documentation for supported environments and precedence.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL