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

Extension doesn't use MANAGE_PY_PATH #24214

Open
oaahmad opened this issue Sep 30, 2024 · 11 comments
Open

Extension doesn't use MANAGE_PY_PATH #24214

oaahmad opened this issue Sep 30, 2024 · 11 comments
Assignees
Labels
info-needed Issue requires more information from poster triage-needed Needs assignment to the proper sub-team

Comments

@oaahmad
Copy link

oaahmad commented Sep 30, 2024

Type: Bug

Behaviour

Overview

Unittest discovery fails for Django tests when following these instructions. At least, they fail for me in projects that use a Python virtualenv inside a dev container where manage.py is not at the root of the project. I'm not sure which of those factors (if any) is relevant. I was able to reproduce the issue by creating this minimal example project: https://github.com/oaahmad/sample-django-project .

Steps to reproduce:

  1. Clone this repo: https://github.com/oaahmad/sample-django-project
  2. Build the dev container
  3. Open the "Testing" view from the activity bar

Expected Behaviour

  • The test_sample test should be visible in the Testing view
  • There should be no errors in the Testing view

Actual Behaviour

  • There are no tests listed in the Testing view
  • The Testing view shows the error "Error discovering unittest tests"

Screenshot:
discovery-error

Additional Information

The tests run without issue from the integrated terminal with ./manage.py test --noinput (from the /home/ubuntu/sample-django-project/src directory). Here is the output:

(sample-virtual-env) ~/sample-django-project/src$ ./manage.py test --noinput
Found 1 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
I ran
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK
Destroying test database for alias 'default'...

This is part of the output in Output -> Python (I will paste the full output under "Diagnostic Data"):

django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

This makes it seem like the Python extension is trying to run the tests as regular unit tests instead of using manage.py. However, I specify MANAGE_PY_PATH in a .env file, and I set "python.envFile" to that file's path (see the example project).

I tried rebuilding the dev container "without cache" multiple times. I tried different Python versions, and different Ubuntu versions for the container. I tried with both a custom user and with the ubuntu user that comes with Ubuntu 24. I tried with both Python's builtin venv package and the virtualenv package. I got the same issue every time.

Diagnostic data

Output from Python in the Output panel

2024-09-30 22:40:26.920 [info] Discover tests for workspace name: sample-django-project - uri: /home/ubuntu/sample-django-project
2024-09-30 22:40:26.922 [warning] could not find a pixi interpreter for the interpreter at /home/ubuntu/sample-virtual-env/bin/python3
2024-09-30 22:40:26.953 [info] > . ~/sample-virtual-env/bin/activate && echo 'e8b39361-0157-4923-80e1-22d70d46dee6' && python ~/.vscode-server/extensions/ms-python.python-2024.14.1-linux-arm64/python_files/printEnvVariables.py
2024-09-30 22:40:26.953 [info] shell: bash
2024-09-30 22:40:26.975 [warning] could not find a pixi interpreter for the interpreter at /home/ubuntu/sample-virtual-env/bin/python3
2024-09-30 22:40:26.979 [info] > ~/sample-virtual-env/bin/python3 ~/.vscode-server/extensions/ms-python.python-2024.14.1-linux-arm64/python_files/testing_tools/unittest_discovery.py . test*.py
2024-09-30 22:40:26.979 [info] cwd: ./src
2024-09-30 22:40:27.151 [error] Error discovering unittest tests:
 Failed to import test module: sampleapp.tests_sample
Traceback (most recent call last):
  File "/usr/lib/python3.12/unittest/loader.py", line 394, in _find_test_path
    module = self._get_module_from_name(name)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/unittest/loader.py", line 337, in _get_module_from_name
    __import__(name)
  File "/home/ubuntu/sample-django-project/src/sampleapp/tests_sample.py", line 2, in <module>
    from django.contrib.auth.models import User
  File "/home/ubuntu/sample-virtual-env/lib/python3.12/site-packages/django/contrib/auth/models.py", line 3, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/home/ubuntu/sample-virtual-env/lib/python3.12/site-packages/django/contrib/auth/base_user.py", line 57, in <module>
    class AbstractBaseUser(models.Model):
  File "/home/ubuntu/sample-virtual-env/lib/python3.12/site-packages/django/db/models/base.py", line 129, in __new__
    app_config = apps.get_containing_app_config(module)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/sample-virtual-env/lib/python3.12/site-packages/django/apps/registry.py", line 260, in get_containing_app_config
    self.check_apps_ready()
  File "/home/ubuntu/sample-virtual-env/lib/python3.12/site-packages/django/apps/registry.py", line 138, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

Extension version: 2024.14.1
VS Code version: Code 1.93.1 (Universal) (38c31bc77e0dd6ae88a4e9cc93428cc27a56ba40, 2024-09-11T17:20:05.685Z)
OS version: Darwin arm64 24.0.0
Modes:
Remote OS version: Linux arm64 6.10.4-linuxkit

  • Python version (& distribution if applicable, e.g. Anaconda): 3.12.3
  • Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): Venv
  • Value of the python.languageServer setting: Default
User Settings


envFile: "<placeholder>"

languageServer: "Pylance"

testing
• cwd: "<placeholder>"
• unittestArgs: "<placeholder>"
• unittestEnabled: true

Installed Extensions
Extension Name Extension Id Version
Code Spell Checker streetsidesoftware.code-spell-checker 3.0.1
GitLens — Git supercharged eamodio.gitlens 15.5.1
JavaScript Debugger ms-vscode.js-debug 1.93.0
Pylance ms-python.vscode-pylance 2024.9.2
Python ms-python.python 2024.14.1
Python Debugger ms-python.debugpy 2024.10.0
REST Client humao.rest-client 0.25.1
Table Visualizer for JavaScript Profiles ms-vscode.vscode-js-profile-table 1.0.9
System Info
Item Value
CPUs Apple M1 Pro (8 x 2400)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: enabled_on
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
video_decode: enabled
video_encode: enabled
webgl: enabled
webgl2: enabled
webgpu: enabled
webnn: disabled_off
Load (avg) 2, 3, 3
Memory (System) 16.00GB (0.07GB free)
Process Argv
Screen Reader no
VM 0%
Item Value
Remote Dev Container: Sample Dev Container @ desktop-linux
OS Linux arm64 6.10.4-linuxkit
CPUs unknown (8 x 0)
Memory (System) 7.66GB (4.16GB free)
VM 0%
@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Sep 30, 2024
@karthiknadig
Copy link
Member

karthiknadig commented Oct 1, 2024

@oaahmad The .env file needs to be in the root of the project. It is in src/sampleapp/.env, it should be sibling of src.

sample-django-project
|- .devcontainer
|- .vscode
|- src
|- .env

@oaahmad
Copy link
Author

oaahmad commented Oct 1, 2024

@karthiknadig Thank you, but that wasn't the issue. I tried moving the .env file to the root of the project (so it is a sibling of src) like you suggested. I still get the error, even after rebuilding the dev container.

Here is my directory structure now:
directory-structure

And here is the same error still happening, even with that directory structure:
error

Besides, the .env file does not have to be at the root of the project according to the documentation I linked to: https://code.visualstudio.com/docs/python/testing#_django-unit-tests

Note: By default, the Python extension looks for and loads .env files at the project root. If your .env file is not at the project root or you are using VS Code variable substitution, add "python.envFile": "${workspaceFolder}/<path-to-.env>" to your settings.json file. This enables the Python extension to load the environment variables from this file when running and discovering tests. Get more info about Python environment variables.

@karthiknadig
Copy link
Member

I just loaded your project it seems to be working:
image

image

Not really sure why this is not working for you. The above screen shot is from loading your project from main branch, all i did was click Open in dev container.

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Oct 1, 2024
@oaahmad
Copy link
Author

oaahmad commented Oct 1, 2024

I'm not sure either because that's not what happens for me 😕. I get the error in other projects too. Maybe the Python extension is caching something in a weird way? I will try on my personal Macbook and let you know how that goes (so far I have been trying on my work Macbook).

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Oct 1, 2024
@oaahmad
Copy link
Author

oaahmad commented Oct 1, 2024

@karthiknadig I get the error on my personal laptop as well. So it happens on both. They are both Macbooks with Apple Silicon (M-series chips). So they are both on ARM if that matters. They are both on the latest version of macOS. My Python extension is installed inside my dev container if that makes a difference. I only activated my Python extension for the workspace (not globally), but I don't think that matters.

I don't know what else I can look for, but it's just not working for me no matter what I try. Let me know if you think of something I can check. Thanks.

@karthiknadig
Copy link
Member

Since you are in a dev container could you just remove the .env and add that line to your DockerFile?

ENV MANAGE_PY_PATH=/home/ubuntu/sample-django-project/src/manage.py

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Oct 1, 2024
@oaahmad
Copy link
Author

oaahmad commented Oct 1, 2024

@karthiknadig I figured something out actually. I commented out my user-level settings for VS Code and it works now. I will try to figure out which setting the issue is and let you know so we can figure out if it's a bug or a user error on my part

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Oct 1, 2024
@oaahmad
Copy link
Author

oaahmad commented Oct 1, 2024

@karthiknadig I figured out which setting causes the issue. It's a weird one. When I comment out this setting:

"telemetry.telemetryLevel": "off",

It works fine. When I enable the setting, it breaks. I tried multiple times in both states. So that seems like a bug with the extension or with VS Code. But at least I have a short term solution.

Thanks for your help. Let me know if I should create a separate issue for that or if we should keep this one open.

@oaahmad
Copy link
Author

oaahmad commented Oct 1, 2024

Tagging @eleanorjboyd because I think she did most of the work enabling test discovery for Django. Maybe she'll have some ideas about what's going on.

@eleanorjboyd
Copy link
Member

thank you for the investigation! Yes, that is a weird setting to cause an issue since the two should have no impact on eachother. I will give this a try and get back to you, thanks for your help!

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Oct 1, 2024
@Dufran
Copy link

Dufran commented Oct 2, 2024

Was having the same problem with test discovery.
Thanks to @oaahmad for the info.
For me fix was commenting uncommenting user settings.json and restarting the VScode( without any changes)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
info-needed Issue requires more information from poster triage-needed Needs assignment to the proper sub-team
Projects
None yet
Development

No branches or pull requests

4 participants