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

fix for reading custom env files (for e.g. ormconfig.env) #3361

Closed
wants to merge 1 commit into from

Conversation

pdeszynski
Copy link
Contributor

@pdeszynski pdeszynski commented Jan 4, 2019

Added missing extension when reading <custom_name>.env file

Tested on TypeORM v0.2.11

Addes missing extension when reading .env file
@pleerock
Copy link
Member

pleerock commented Jan 4, 2019

Can you please tell what issue you have? Because we have changed this area recently. Do you have something broken or what? adding @steevsachs

@pdeszynski
Copy link
Contributor Author

@pleerock for me it stopped to read ‘ormconfig.env’ file as it was trying to load file without any extension.

@vlapo
Copy link
Contributor

vlapo commented Jan 4, 2019

I think we should finally fix and unskip tests for this area. I know there is problem with config files not accesible during tests. Maybe using ts-node in tests will fix this after #3332 merged?

@pleerock
Copy link
Member

pleerock commented Jan 7, 2019

@vlapo yes, it will.

I'm waiting for @steevsachs review of this PR.

@steevsachs
Copy link
Contributor

No problem, I will review this morning.

@steevsachs
Copy link
Contributor

I can't reproduce the problem described here, can anyone provide more details in case I'm overlooking something?

this.baseFilePath in a ConnectionOptionsReader is the full path of the config file, so appending .env to that file name will always result in a different file path from the config file path.

This will also break functionality for a plain .env file because foundFileFormat will equal env but the ConnectionOptionsReader would try to load .env.env.

If you manually move the config file mocks into the appropriate build dir and unskip the last test in test/functional/connection-options-reader/connection-options-reader.ts, the tests will fail.

@pdeszynski
Copy link
Contributor Author

pdeszynski commented Jan 7, 2019

@steevsachs when I was testing this on 0.2.11 and having ormconfig.env file it was trying to load in my case ormconfig file instead (without any extension). After this change I was also testing it with .env file and it was still correctly loaded.

But maybe it's already fixed in master. I will try to prepare reproducible case in a separate repository tonight.

@pdeszynski
Copy link
Contributor Author

@steevsachs Minimal reproducible code https://github.com/pdeszynski/typeorm-config-failing

yarn install
yarn start

It should end up with

(node:36943) UnhandledPromiseRejectionWarning: Error: No connection options were found in any of configurations file.
    at ConnectionOptionsReader.<anonymous> (/Users/piteer/workspace/typeorm-config/src/connection/ConnectionOptionsReader.ts:41:19)
    at step (/Users/piteer/workspace/typeorm-config/node_modules/typeorm/connection/ConnectionOptionsReader.js:32:23)
    at Object.next (/Users/piteer/workspace/typeorm-config/node_modules/typeorm/connection/ConnectionOptionsReader.js:13:53)
    at fulfilled (/Users/piteer/workspace/typeorm-config/node_modules/typeorm/connection/ConnectionOptionsReader.js:4:58)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:744:11)
    at Object.<anonymous> (/Users/piteer/workspace/typeorm-config/node_modules/ts-node/src/bin.ts:157:12)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)

Renaming ormconfig.env to .env will solve this problem.

The same problem occurs when running this code against typeorm@0.2.12-rc.1

@steevsachs
Copy link
Contributor

Thank you for that, @pdeszynski. So the issue here is specifically support for ormconfig.env, which occurs because we default the base config name to ormconfig and correctly detect a .env config type, but then don't by default load from ormconfig.env.

We can't just append .env because it would break support for any environment type config that has specified .env in its filename.

@@ -94,7 +94,7 @@ export class ConnectionOptionsReader {
// if .env file found then load all its variables into process.env using dotenv package
if (foundFileFormat === "env") {
const dotenv = PlatformTools.load("dotenv");
dotenv.config({ path: this.baseFilePath });
dotenv.config({ path: `${this.baseFilePath}.env` });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The extension should only be appended if possibleExtension is not equal to .env.

Copy link
Contributor Author

@pdeszynski pdeszynski Jan 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@steevsachs you're right, but this was not falling for this foundFileFormat === "env" condition, if i had .env file instead of ormconfig.env What about the else case at https://github.com/typeorm/typeorm/pull/3361/files/d0e8fa3af49052cd13703f27f7c31e2d6e5d9d5e#diff-fd8d31a6463f188113f6a9f43f77612fR98 ? Isn't it solving .env file case? When testing after applying this patch I had both cases working correctly (but I didn't run functional tests).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The else condition only fires if the config file format is not .env, ie ormconfig.json. This ensure environment is still loaded in those cases.

I believe the reason .env is working is that

  • ConnectionOptionsReader uses ormconfig as its base file name...
  • foundFileFormat will equal undefined at line 95 because there is no ormconfig.* at the base file path
  • Thus the else condition fires and loads the .env file.

@nimish
Copy link

nimish commented Jul 18, 2019

Hi all, this is still broken. Will someone merge this to fix this defect in the docs and the config loader? The only useful workaround is to switch to .env.

@Ivanca
Copy link

Ivanca commented Jul 31, 2019

Can't believe this fix hasn't been accepted, right now using ormconfig.env or foo.env or bar.env doesnt work at all, the only workaround is to name the file as ".env" cc @steevsachs

@karfau
Copy link

karfau commented Dec 8, 2019

Here is my workaround to deal with the dropped support for loading ormconfig.env by default that works for typeorm CLI and without changing any existing code:
https://gist.github.com/karfau/b6d0927628f6662ca6d892153562522f

This allowed me to upgrade from v0.2.7 to v0.2.21 without any (other) issues.

Copy link
Contributor

@imnotjames imnotjames left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a test to confirm that it actually does what it's intended to. Can you add at least one that confirms the behavior?

@imnotjames
Copy link
Contributor

Also there's a conflict.

@steevsachs
Copy link
Contributor

This PR seems way out of sync, but I'm pretty sure a viable fix for master is

if (PlatformTools.fileExist(this.baseFilePath)) {
   PlatformTools.dotenv(this.baseFilePath);
} else if (PlatformTools.fileExist(`${this.baseFilePath}.env`)) {
   PlatformTools.dotenv(`${this.baseFilePath}.env`);
}

on line 98 of ConnectionOptionsReader.

There's definitely other ways to handle this, but I think this one will restore compat without breaking anything.

I don't know that I'll have time to refamiliarize myself with this repo enough to get testing working in the short term, so anyone else please feel free to run with this! If no one does, I'll try to make some time in the next couple weeks.

@imnotjames imnotjames self-assigned this Oct 9, 2020
@imnotjames
Copy link
Contributor

imnotjames commented Oct 16, 2020

Actually - I don't think this is broken? I've created some tests and cannot replicate the issue. Attempting via the repo given.

@pdeszynski
Copy link
Contributor Author

Hello all, I've completely forgot about this PR - if you still need it I can take care of it, maybe during this weekend

@imnotjames
Copy link
Contributor

imnotjames commented Oct 16, 2020

Ok - in the repo I can replicate it but not in our tests - weird! In both cases I am using ts-node.

EDIT: No wonder it was working, I was running the wrong test!!

@imnotjames
Copy link
Contributor

Hello all, I've completely forgot about this PR - if you still need it I can take care of it, maybe during this weekend

If you'd like! I'll let you know if I end up getting anywhere today on it. :)

@imnotjames
Copy link
Contributor

@pdeszynski I think I've got it fixed in #6922

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

Successfully merging this pull request may close these issues.

None yet

8 participants