Skip to content

Support --env-file-if-exists (no error thrown sans .env file) #23323

Open

Description

@danielbayley

Summary

In addition to the existing --env-file feature, Node also has --env-file-if-exists, which does not throw an error if the given file does not exist. This is particularly useful in dev/lifecycle scripts/CI etc.

Example

#! /usr/bin/env -S uv run --env-file-if-exists .env --script
import os
UV_ENV_FILE = os.getenv("UV_ENV_FILE", ".env")
exists = os.path.isfile(UV_ENV_FILE)
print(exists) # false

Activity

  1. added
    enhancement
    on Nov 20, 2026
  2. woodruffw commented on Nov 20, 2026

    @woodruffw
    Member

    Hi @danielbayley, thanks for opening an issue. Can you say a bit more about your use case, and how you end up hitting the failure mode here?

    I think my main concern with this is that "if" in CLI flags is usually a code smell: a flag enables or disables something conventionally, so an "if" means that the flag is actually a second order condition (it enables/disables something if something else is the case).

  3. added
    area:cli
    on Nov 20, 2026
  4. danielbayley commented on Nov 24, 2026

    @danielbayley
    Author

    Can you say a bit more about your use case, and how you end up hitting the failure mode here?

    Hi @woodruffw, sure, it’s useful e.g. to be able to run the same executable script, with the above bang line, both locally (reading environment variables from .env — which as you undoubtedly know, should never be commited…), and in CI/GitHub Action workflows (where there is no .env, and these should instead be read from repository secrets).

    I think my main concern with this is that "if" in CLI flags is usually a code smell: a flag enables or disables something conventionally, so an "if" means that the flag is actually a second order condition (it enables/disables something if something else is the case).

    You can call it something else, whatever you like, just allow the option to not throw an error when no .env file exists…

    Also, I mean, if it’s good enough for Node.js…

  5. BurntSushi commented on Nov 24, 2026

    @BurntSushi
    Member

    Can you work around this by touch .env in your CI job?

  6. danielbayley commented on Nov 24, 2026

    @danielbayley
    Author

    Can you work around this by touch .env in your CI job?

    I guess, that‘s a bit hacky though… Also, this is just one example. Another question is, should the existing --env-file even throw an error if a given file does not exist, as opposed to a warning?

  7. woodruffw commented on Nov 24, 2026

    @woodruffw
    Member

    I think it should, but even before we reach that: changing it to a warning would be a breaking change on uv's side.

    (FWIW, I think an empty .env file is less of a hack than a "maybe" CLI flag.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:clienhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions