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

"Environment variables loaded from .env" is misleading #10104

Open
bennycode opened this issue Nov 4, 2021 · 14 comments
Open

"Environment variables loaded from .env" is misleading #10104

bennycode opened this issue Nov 4, 2021 · 14 comments
Labels
kind/improvement An improvement to existing feature and code. team/client Issue for team Client. team/schema Issue for team Schema. topic: env topic: .env

Comments

@bennycode
Copy link

Bug description

I followed the "Running migrations on different environments" guide and executed this statement in my terminal:

dotenv -e .env.test -- npx prisma migrate dev

It was giving me the following output:

Environment variables loaded from .env
Prisma schema loaded from prisma\schema.prisma
Datasource "db": PostgreSQL database "tests", schema "public" at "localhost:5433"

I was very surprised because I do have a .env and .env.test file in my project and thought that Pisma is not pickung up my .env.test file because it's reporting that it loaded variables from ".env". In fact it used my .env.test file.

How to reproduce

  1. Create a .env file with DATABASE_URL="postgresql://postgres:postgres@localhost:5432/root_db?schema=public"
  2. Create a .env.test file with DATABASE_URL="postgresql://prisma:prisma@localhost:5433/test_db?schema=public"
  3. Run dotenv -e .env.test -- npx prisma migrate dev

Expected behavior

To avoid this confusion I suggest to change the phrasing of the console output. Preferably it should show the correct file name (e.g. loaded from .env.test) or not mention the file name at all (e.g. loaded environment variables).

Prisma information

schema.prisma

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model enum_role {
    type        String @id
    description String
}

Environment & setup

  • OS: Windows 10
  • Database: PostgreSQL
  • Node.js version: 16.10.0

Prisma Version

prisma                  : 3.4.0
@prisma/client          : 3.4.0
Current platform        : windows
Query Engine (Node-API) : libquery-engine 1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85 (at ..\..\node_modules\@prisma\engines\query_engine-windows.dll.node)
Migration Engine        : migration-engine-cli 1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85 (at ..\..\node_modules\@prisma\engines\migration-engine-windows.exe)
Introspection Engine    : introspection-core 1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85 (at ..\..\node_modules\@prisma\engines\introspection-engine-windows.exe)
Format Binary           : prisma-fmt 1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85 (at ..\..\node_modules\@prisma\engines\prisma-fmt-windows.exe)
Default Engines Hash    : 1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85
Studio                  : 0.438.0
@janpio
Copy link
Member

janpio commented Nov 4, 2021

I think what technically is happening there that Prisma does read from .env, but that dotenv -e .env.test already filled the env vars and so the Prisma part skips the already existing env vars instead of overwriting them.

To avoid this confusion I suggest to change the phrasing of the console output. Preferably it should show the correct file name (e.g. loaded from .env.test) or not mention the file name at all (e.g. loaded environment variables).

This is unfortunately not possible, as Prisma itself das not read .env.test but the dotenv CLI does that and populates the environment variables.

Does that make sense?

Still I totally see how this can be confusing.

@janpio
Copy link
Member

janpio commented Nov 19, 2021

ping @bennycode

@bennycode
Copy link
Author

Hi @janpio, your explanation makes sense to me. I think the best that can be done here is placing a note in the docs. And how about rephrasing "Environment variables loaded from .env" to just "Environment variables loaded"?

@janpio
Copy link
Member

janpio commented Nov 22, 2021

The correct wording would probably be Read .env file and put contained environment variables into environment - because at this step we really only do that, and then the reading of the actual environment variables per the env() in the schema file definition happens later. But that is super complicated to explain unfortunately. Or do you think maybe .env found, loaded file content into environment variables (if not present yet) would indeed be more correct and less misleading?

@NTag
Copy link

NTag commented Nov 10, 2022

It's super misleading, I lost two hours trying to figure out why my env variables from .env file were loaded during my tests whereas I followed the instructions to load a .env.test file.

Actually dotenv and prisma aren't smart at all: if you have the same variable in the two files and used dotenv -e .env.test ok when prisma is initiated it won't override the variable. HOWEVER, it will load variables from the .env file which aren't in the .env.test file. This is super wrong, this is not explained in the docs, and this is a very bad pattern.

.env file shouldn't be automatically magically loaded by prisma.

@janpio janpio added kind/improvement An improvement to existing feature and code. team/client Issue for team Client. team/schema Issue for team Schema. and removed team/support-engineering labels Dec 14, 2022
@JustinPraas
Copy link

Any news on this?

@thesmart
Copy link

The problem is the advice to use dotenv-cli while prisma insists on loading .env. This is very confusing and error prone. Why doesn't prisma use an environment variable to load the desired .env file?

For example: ENV_PATH="./.env.dev" prisma migrate dev.

@dodas
Copy link

dodas commented Oct 11, 2023

Just got bitten by this. I wouldn't expect prisma to expand .env onto process.env at all. That's what dotenv is for.

@dodas
Copy link

dodas commented Nov 16, 2023

@janpio is there any reason prisma is integrating dotenv functionality? A teammate lost 1-2 hours again because of this issue. Prisma is implicitly loading an .env file in our app, causing an unexpected behavior.

@kannthu
Copy link

kannthu commented Nov 28, 2023

Same for me, we got kinda fucked by this. Totally unexpected behavior.

@thesmart
Copy link

thesmart commented Nov 28, 2023 via email

@iamtonmoy0
Copy link

i am using render.com for postgresql but when i want to migrate to db i get this:

Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "test_database_lenk", schema "public" at "dpg-cm59nkq1hbls73ahtp70-a.oregon-postgres.render.com"

Error: P1001: Can't reach database server at dpg-cm59nkq1hbls73ahtp70-a.oregon-postgres.render.com:5432

Please make sure your database server is running at dpg-cm59nkq1hbls73ahtp70-a.oregon-postgres.render.com:5432.

can anyone tell me what is the problem?

@Susanta-0007

This comment was marked as off-topic.

@Susanta-0007

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/improvement An improvement to existing feature and code. team/client Issue for team Client. team/schema Issue for team Schema. topic: env topic: .env
Projects
None yet
Development

No branches or pull requests

9 participants