Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dotenv: different behaviour between load.ts and mod.ts when used together with run --watch= #2490

Open
czottmann opened this issue Aug 2, 2022 · 1 comment
Labels
bug Something isn't working needs triage

Comments

@czottmann
Copy link

czottmann commented Aug 2, 2022

Describe the bug

When using Deno's watcher (deno run --watch=file,folder/ …) in conjunction with dotenv, I get different behaviour with auto loading compared to using config(). I'm not sure whether the fault lies with dotenv or Deno.env's handling of changes, though.

On a watcher-triggered reload, when using auto-loading, eventual changes in .env will not be available via Deno.env. When using config(), they are available.

Background: I was implementing an .env-based Deno Fresh server configuration (setting the HTTP port etc.) when I noticed that the file changes weren't picked up.

Steps to Reproduce

  1. Create initial .env file:
MY_ENV_VAR=foo
  1. Create app-config.ts:
import { config } from "https://deno.land/std@0.150.0/dotenv/mod.ts";
console.log((await config())["MY_ENV_VAR"]);
  1. Run app-config.ts and note the current output, "foo":
deno run -A --watch=.env app-config.ts
  1. Create app-load.ts:
import "https://deno.land/std@0.150.0/dotenv/load.ts";
console.log(Deno.env.get("MY_ENV_VAR"));
  1. Run app-load.ts and note the current output, "foo":
deno run -A --watch=.env app-load.ts
  1. Change contents of .env file:
MY_ENV_VAR=bar
  1. The watcher will reload app-config.ts, the output will be "bar".
  2. The watcher will reload app-load.ts, the output will still be "foo".

Expected behavior

I expect both scripts to behave the same, they should both output "foo".

Environment

  • OS: macOS 12.5 (21G72)
  • deno version: 1.24.0
  • std version: 0.150.0
@denizdogan
Copy link
Contributor

Importing/executing dotenv/load.ts will export the parsed values to the environment of the current process, i.e. it will do Deno.env.set for each variable, unless the variable is already set in the environment.

dotenv.config() will not export anything to the environment, but if you do dotenv.config({ export: true }), it will behave identically.

On a side-note, --watch will not restart the entire process, just the main worker, so any changes that you make to the process environment will persist in your subsequent reloads. Maybe someone should clarify that in the CLI help text for --watch...

Anyway, I think this can be closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants