Skip to content

Debug configuration env variables should overwrite env variables defined in .env file #16984

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

Closed
ccrvlh opened this issue Jul 24, 2021 · 7 comments · Fixed by #18095
Closed

Debug configuration env variables should overwrite env variables defined in .env file #16984

ccrvlh opened this issue Jul 24, 2021 · 7 comments · Fixed by #18095
Assignees
Labels
area-debugging bug Issue identified by VS Code Team member as probable bug regression Bug didn't exist in a previous release verified Verification succeeded
Milestone

Comments

@ccrvlh
Copy link

ccrvlh commented Jul 24, 2021

Environment data

  • VS Code version: 1.58.2
  • Extension version (available under the Extensions sidebar):
  • OS and version: MacOS 11.4
  • Python version: 3.8.10
  • Type of virtual environment used: venv
  • Relevant/affected Python packages and their versions: Flask
  • Value of the python.languageServer setting: "Default"

Expected behaviour

"env" portion of launch.json doesn't seem to work.
When trying to launch a Flask application I have the following tree structure:

/.venv
/.vscode
/app
-- uwsgi.py ---> Flask Application

My launch.json file is:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Flask",
      "type": "python",
      "request": "launch",
      "module": "flask",
      "pythonPath": "${workspaceRoot}/.venv/bin/python",
      "env": {
        "FLASK_APP": "app/uwsgi.py"
      },
      "args": ["run"],
      "jinja": true
    }
  ]
}

When running the debugger Flask says it can't find application and the first line shows:

* Serving Flask app "application.py"

So from what I can understand it is as if the env var "FLASK_APP=app/uwsgi.py" is not being read, so it loads the default "application", and can't find the app.

Tried without the debugger and works fine (FLASK_APP=app/uwsgi.py; flask run).

Another weird thing is that when I set

      "env": {
        "FLASK_APP": "app/uwsgi.py",
        "FLASK_ENV": "development"
      },

it goes as for as running the server, but when trying to reach any endpoint it says flask.cli.NoAppException: Could not import "application"..
If I remove the FLASK_ENV line it doesn't even run the server and gives me an error: Error: Could not import "application".

@karthiknadig karthiknadig transferred this issue from microsoft/vscode-python Jul 25, 2021
@fabioz
Copy link

fabioz commented Jul 29, 2021

@lowercase00

The way you're defining things, I think your "cwd" must be at a pretty specific place for app/uwsgi.py to be picked up, yet, you're not defining it... can you try to specify the "cwd" in the launch config to be the folder which contains app/uwsgi.py to see if it works?

Note: it did work for me when the cwd was properly specified...

@ccrvlh
Copy link
Author

ccrvlh commented Aug 2, 2021

@fabioz thanks, I did try with ${workspaceFolder} before the app/uwsgi.py also didn't work, I'll try setting cmd to set how it goes

@ccrvlh
Copy link
Author

ccrvlh commented Aug 3, 2021

Just tried with cmd and also didn't work.
What I find weird is the application. Is not that is not finding the Flask App, it's not even looking for app/uwsgi.py is only looking for application.py as if the env var is not being read.

@ccrvlh
Copy link
Author

ccrvlh commented Aug 16, 2021

Weird enough, I can't replicate the issue on a newly create Flask project with exactly the same folder structure. I've already created and recreated the venv and everything else that I could (mypy, pyenv, vscode folder etc). Any tips ou troubleshooting this? I've got no messages at all at the developers console, so really no idea what could be the issue.

@int19h
Copy link

int19h commented Aug 16, 2021

Are there any version differences in the packages between the two environments, if you look inside .venv/site-packages?

@ccrvlh
Copy link
Author

ccrvlh commented Aug 16, 2021

So finally found out the issue. I had an old FLASK_APP=application.py on my .env file, and it was overwriting the env vars I was passing in the launch.json. I'm not sure if it's possible for the extension to control the order in which env vars are loaded, so not sure if anything different could have been done. If it is, I would suggest that the env vars in the launch.json file to be top priority and overwrite other env vars, instead of the other way around.

@int19h
Copy link

int19h commented Aug 16, 2021

From debugpy perspective, it doesn't actually know anything about .env - that's something the extension handles, basically, by reading the file and then injecting those variables into your debug config. So this would need to be handled in vscode-python.

But I agree that launch.json should take priority - .env is supposed to define the default values for the entire folder/workspace, but individual debug configurations should be able to override that as needed.

@int19h int19h transferred this issue from microsoft/debugpy Aug 16, 2021
@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Aug 16, 2021
@karthiknadig karthiknadig self-assigned this Aug 17, 2021
@karthiknadig karthiknadig added area-debugging bug Issue identified by VS Code Team member as probable bug regression Bug didn't exist in a previous release triage and removed triage-needed Needs assignment to the proper sub-team labels Aug 17, 2021
@luabud luabud added important Issue identified as high-priority and removed important Issue identified as high-priority labels Sep 29, 2021
@karthiknadig karthiknadig changed the title Flask debugger not reading the correct env var Debug configuration env variables should overwrite env variables defined in .env file Nov 29, 2021
@karthiknadig karthiknadig added this to the November 2021 milestone Nov 29, 2021
@karthiknadig karthiknadig added the verified Verification succeeded label Nov 29, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-debugging bug Issue identified by VS Code Team member as probable bug regression Bug didn't exist in a previous release verified Verification succeeded
Projects
None yet
5 participants