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

infer_schema! fail to find .env in local project if it is inside a workspace #1696

Closed
limira opened this Issue May 16, 2018 · 4 comments

Comments

Projects
None yet
4 participants
@limira

limira commented May 16, 2018

Setup

Versions

  • Rust: 1.26
  • Diesel: 1.2.2, diesel_infer_schema: 1.2.0
  • Database: Sqlite
  • Operating System Fedora 27

Feature Flags: ["sqlite"]

Problem Description

What are you trying to accomplish?

I have a workspace like this:

workspace\
    |---- Cargo.toml
    |---- .env (.env at workspace root)
    |---- projects\
            |---- project1\
                    |---- Cargo.toml
                    |---- .env (.env at project root)
                    |---- src\...

If I put DATABASE_URL in .env at project root

  • ::std::env::var("DATABASE_URL") found it 👍 , but
  • infer_schema("dotenv:DATABASE_URL") give error 👎 :
1 | infer_schema!("dotenv:DATABASE_URL");
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: message: called `Result::unwrap()` on an `Err` value:
    "Failed to load environment variable DATABASE_URL:
    environment variable not found"

If DATABASE_URL is in .env at workspace root: infer_schema works

What is the expected output?

infer_schema should works with .env inside project folder

Checklist

  • I have already looked over the issue tracker for similar issues.
@weiznich

This comment has been minimized.

Contributor

weiznich commented May 16, 2018

We are relaying on the dotenv crate for handling .env files. So this should probably be reported there.

@sgrif sgrif closed this May 16, 2018

@limira

This comment has been minimized.

limira commented May 17, 2018

I reported this to dotenv. It is not an issue in diesel itself, but I think it is useful to leave this open, because infer_schema does not actually work as expected.
We will update the version of dotenv in [dependencies] and close this when the issue in dotenv get fixed.

@daboross

This comment has been minimized.

daboross commented May 18, 2018

I believe this is an issue in diesel - it uses dotenv, but the error is in how diesel is using dotenv.

The method it's using, dotenv(), uses the current working directory to find .env files. However, it's being used here inside a procedural macro (in infer_schema_macros), and procedural macros are always run in the workspace root, rather than the crate root.

If this is deemed a real issue, the only way to fix this will be to have diesel change the working directory or use a different function from dotenv to load the file.

@limira

This comment has been minimized.

limira commented May 19, 2018

@weiznich, @sgrif, what do you think?
I actually can put the DATABASE_URL into .env at the workspace root. But it is very nice if we can put it into .env at the crate/project root, because it is only use for that specific crate.

Edit:
I noticed that you are deprecated infer_schema, so, just ignore this.

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