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

.env loading silently fails under common scenarios #307

Closed
xlevus opened this issue Oct 19, 2023 · 4 comments
Closed

.env loading silently fails under common scenarios #307

xlevus opened this issue Oct 19, 2023 · 4 comments

Comments

@xlevus
Copy link

xlevus commented Oct 19, 2023

With the following test:

## example_test.py

import os

def test_potato():
  assert os.getenv("POTATO") == "potato"

def test_cabbage():
  assert os.getenv("CABBAGE") == "cabbage"
## BUILD
python_tests(extra_env_vars=["POTATO"])

This .env file works when running pants test ::

POTATO=potato
CABBAGE=cabbage

So does:

POTATO=potato
CABBAGE=cabbage
PYTHONPATH="/foo/bar:$PYTHONPATH"

But the following does not, and does not warn you there was a problem loading the env file.

CABBAGE=cabbage
PYTHONPATH="/foo/bar:$PYTHONPATH"
POTATO=potato

This is particularly troublesome, as adding PYTHONPATH="...:$PYTHONPATH" to your .env file is recommended to get vscode integration working in the documentation: https://www.pantsbuild.org/docs/setting-up-an-ide#first-party-sources

This seems to be the case for both Pants 2.16 and 2.17 .

@huonw
Copy link
Contributor

huonw commented Nov 1, 2023

Thanks for filing an issue. This is subtle and annoying. To solve the immediate problem, it looks like a working syntax is without the quotes:

CABBAGE=cabbage
PYTHONPATH=/foo/bar:$PYTHONPATH
POTATO=potato

The Pants docs should be fixed to not generate the " while it doesn't work.

The root cause is "scie jump"'s use of dotenvy in a way that silently eats errors (a-scie/jump#163). Despite it being an upstream dep causing the problem, let's leave this open because we'll still need to change scie-pants even once upstream is fixed: to pull in the new version.


Investigation:

  1. The loading of .env is controlled "Scie jump": scie-pants opts into it:
    load_dotenv = true
  2. "scie jump" calls out to dotenvy: https://github.com/a-scie/jump/blob/f44ca18f9c6e8a02f8fab84f6e22c8887b5a3267/jump/src/lib.rs#L181-L186 (PROBLEM 1: scie-jump ignores all errors)
  3. why does the ordering matter? dotenvy aborts on first error https://github.com/allan2/dotenvy/blob/08e35eea693ca12c4038a226bae2e3144445ba69/dotenv/src/iter.rs#L33, so the "working" version sets both POTATO and CABBAGE before bailing out due to the error, while the "broken" version sets CABBAGE and then bails out, never setting POTATO. (PROBLEM 2: env vars are partially set)

Test program:

[package]
name = "scie-pants-307"
version = "0.1.0"
edition = "2021"

[dependencies]
dotenvy = "0.15.7"
fn main() {
    let file: &[u8] = b"\
CABBAGE=cabbage
PYTHONPATH=\"/foo/bar:$PYTHONPATH\"
POTATO=potato";

    dbg!(
        dotenvy::from_read_iter(file).collect::<Vec<_>>()
    );

    // now actually apply those to this process
    let _ = dbg!(dotenvy::from_read(file));

    let _ = dbg!(std::env::var("CABBAGE"));
    let _ = dbg!(std::env::var("PYTHONPATH"));
    let _ = dbg!(std::env::var("POTATO"));
}

Output of cargo run:

[src/main.rs:7] dotenvy::from_read_iter(file).collect::<Vec<_>>() = [
    Ok(
        (
            "CABBAGE",
            "cabbage",
        ),
    ),
    Err(
        LineParse(
            "\"/foo/bar:$PYTHONPATH\"",
            21,
        ),
    ),
    Ok(
        (
            "POTATO",
            "potato",
        ),
    ),
]
[src/main.rs:12] dotenvy::from_read(file) = Err(
    LineParse(
        "\"/foo/bar:$PYTHONPATH\"",
        21,
    ),
)
[src/main.rs:14] std::env::var("CABBAGE") = Ok(
    "cabbage",
)
[src/main.rs:15] std::env::var("PYTHONPATH") = Err(
    NotPresent,
)
[src/main.rs:16] std::env::var("POTATO") = Err(
    NotPresent,
)

@huonw
Copy link
Contributor

huonw commented Nov 1, 2023

(Also filed pantsbuild/pants#20127 about the doc issue, thank you.)

huonw added a commit to pantsbuild/pants that referenced this issue Nov 3, 2023
This fixes #20127 by avoiding double quotes in the `.env` file generated
by the code snippet in the IDE set-up docs.

Double quotes aren't supported by the `scie-pants` `.env` loader, and a
line containing them was previously silently ignored (plus all lines
after), but with scie-pants 0.11.0, will be an error. See
pantsbuild/scie-pants#307 and
pantsbuild/scie-pants#319.

The new suggestion has _no_ quotes at all, because there's no other way
to have a line that includes a substitution. To handle source roots with
spaces in their paths, this has to also manually escape them. Supporting
double quotes is effectively a new feature of `scie-pants` launcher (via
scie jump a-scie/jump#166 via `dotenvy`
allan2/dotenvy#11), but for now we can at
least stop suggesting incorrect things.

For instance, with source roots `a b/c` and `def`, this generates:

```
PYTHONPATH=./a\ b/c:./def:$PYTHONPATH
```
WorkerPants pushed a commit to pantsbuild/pants that referenced this issue Nov 3, 2023
This fixes #20127 by avoiding double quotes in the `.env` file generated
by the code snippet in the IDE set-up docs.

Double quotes aren't supported by the `scie-pants` `.env` loader, and a
line containing them was previously silently ignored (plus all lines
after), but with scie-pants 0.11.0, will be an error. See
pantsbuild/scie-pants#307 and
pantsbuild/scie-pants#319.

The new suggestion has _no_ quotes at all, because there's no other way
to have a line that includes a substitution. To handle source roots with
spaces in their paths, this has to also manually escape them. Supporting
double quotes is effectively a new feature of `scie-pants` launcher (via
scie jump a-scie/jump#166 via `dotenvy`
allan2/dotenvy#11), but for now we can at
least stop suggesting incorrect things.

For instance, with source roots `a b/c` and `def`, this generates:

```
PYTHONPATH=./a\ b/c:./def:$PYTHONPATH
```
WorkerPants pushed a commit to pantsbuild/pants that referenced this issue Nov 3, 2023
This fixes #20127 by avoiding double quotes in the `.env` file generated
by the code snippet in the IDE set-up docs.

Double quotes aren't supported by the `scie-pants` `.env` loader, and a
line containing them was previously silently ignored (plus all lines
after), but with scie-pants 0.11.0, will be an error. See
pantsbuild/scie-pants#307 and
pantsbuild/scie-pants#319.

The new suggestion has _no_ quotes at all, because there's no other way
to have a line that includes a substitution. To handle source roots with
spaces in their paths, this has to also manually escape them. Supporting
double quotes is effectively a new feature of `scie-pants` launcher (via
scie jump a-scie/jump#166 via `dotenvy`
allan2/dotenvy#11), but for now we can at
least stop suggesting incorrect things.

For instance, with source roots `a b/c` and `def`, this generates:

```
PYTHONPATH=./a\ b/c:./def:$PYTHONPATH
```
huonw added a commit to pantsbuild/pants that referenced this issue Nov 3, 2023
…20146)

This fixes #20127 by avoiding double quotes in the `.env` file generated
by the code snippet in the IDE set-up docs.

Double quotes aren't supported by the `scie-pants` `.env` loader, and a
line containing them was previously silently ignored (plus all lines
after), but with scie-pants 0.11.0, will be an error. See
pantsbuild/scie-pants#307 and
pantsbuild/scie-pants#319.

The new suggestion has _no_ quotes at all, because there's no other way
to have a line that includes a substitution. To handle source roots with
spaces in their paths, this has to also manually escape them. Supporting
double quotes is effectively a new feature of `scie-pants` launcher (via
scie jump a-scie/jump#166 via `dotenvy`
allan2/dotenvy#11), but for now we can at
least stop suggesting incorrect things.

For instance, with source roots `a b/c` and `def`, this generates:

```
PYTHONPATH=./a\ b/c:./def:$PYTHONPATH
```

Co-authored-by: Huon Wilson <huon@exoflare.io>
huonw added a commit to pantsbuild/pants that referenced this issue Nov 3, 2023
…20145)

This fixes #20127 by avoiding double quotes in the `.env` file generated
by the code snippet in the IDE set-up docs.

Double quotes aren't supported by the `scie-pants` `.env` loader, and a
line containing them was previously silently ignored (plus all lines
after), but with scie-pants 0.11.0, will be an error. See
pantsbuild/scie-pants#307 and
pantsbuild/scie-pants#319.

The new suggestion has _no_ quotes at all, because there's no other way
to have a line that includes a substitution. To handle source roots with
spaces in their paths, this has to also manually escape them. Supporting
double quotes is effectively a new feature of `scie-pants` launcher (via
scie jump a-scie/jump#166 via `dotenvy`
allan2/dotenvy#11), but for now we can at
least stop suggesting incorrect things.

For instance, with source roots `a b/c` and `def`, this generates:

```
PYTHONPATH=./a\ b/c:./def:$PYTHONPATH
```

Co-authored-by: Huon Wilson <huon@exoflare.io>
@huonw
Copy link
Contributor

huonw commented Nov 14, 2023

Update: #319 theoretically gives noisier behaviour with a hard error on problems, but I'm concerned about it being a breaking change.

For instance, someone may have a repo that has an .env that isn't understood by pants and publishing that will cause their builds to break (especially if they have CI that installs the latest scie-pants, unpinned). This could happen "legitimately", such as if the .env file is meant for being read by another tool, that does understand X="Y", and the env vars within it don't affect pants' behaviour.

Thus, I'm thinking it'd be better to wait for at least either:

@jsirois
Copy link
Contributor

jsirois commented Feb 2, 2024

Closed by #339

@jsirois jsirois closed this as completed Feb 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants