stray-env-var

Environment variables are used outside an env file.

Group
Code
Runs
On by default
Level
warning
Reads
Every file
Fix
None: which values the env file exports, and how the code receives them, is a design choice

Why

Read and write environment variables only in an env file, such as env.rs, env.py, env.ts, or env.rb, and pass the values on from there, so one file lists every variable the code uses, its default, and how it is parsed. A Cargo build script may use the environment Cargo gives it. In .ur/ur.toml, files under [lint.stray-env-var] names other env files, and enabled = false there turns the rule off.

The fix, as Ur suggests it: use the environment only in an env file, and pass the values on from there.

For agents

Read and write environment variables only in the project's env file, such as src/env.rs or env.py, and pass the values on from there.

Example

app/billing.py
import os


def tax_rate():
    return float(os.environ.get("TAX_RATE", "0.2"))


def invoice_total(subtotal):
    return subtotal * (1 + tax_rate())
What Ur prints
$ ur lint --rule stray-env-var
warning[stray-env-var]: environment variable `TAX_RATE` is used outside an env file; no env file above it exists for python, so create one, such as ./app/env.py
  --> ./app/billing.py:5:18 `os.environ.get("TAX_RATE", "0.2")`
  = hint: use the environment only in an env file, and pass the values on from there
0 clone classes and 1 use of the environment outside an env file in 1 file

Try it in the playground →