Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Debugger crashes when using Django's Development Server with a Proper Launch Profile #1276

Closed
DonJayamanne opened this issue Mar 28, 2019 · 1 comment
Assignees

Comments

@DonJayamanne
Copy link
Contributor

@pymike00 commented on Thu Mar 28 2019

Environment data

  • VS Code version: 1.32.2-1
  • Extension version: 2019.3.6139
  • OS and version: Linux Manjaro XFCE x86_64 18.0.4
  • Python version: 3.7.2
  • Type of virtual environment used: venv
  • Relevant/affected Python packages and their versions: Django 2.1.7

Expected behaviour

Using the Python Extension on Visual Studio Code to run Django's development server as explained in the docs, https://code.visualstudio.com/docs/python/tutorial-django#_create-a-debugger-launch-profile, without any problems.

Actual behaviour

Can't use the debugger to launch Django's development server; As soon as you try to use the site, the debugger pauses on the exception VariableDoesNotExist, even on a fresh Django Project.

The problem does not manifest itself when using a previous version of the Python Extension (such as 2019.2.5558), nor it manifests itself when running Django's development server from a shell, or even from VS Code's Terminal.

Steps to reproduce:

  1. Create a Virtual Environment with VENV and install Django.
  2. Create New Project: django-admin startproject NewProject
  3. Enter Folder: cd NewProject
  4. Open The Editor: code .
  5. Select Python Interpreter to be the one of the previously created VENV
  6. Create debugger Launch profile as explained in the docs. Comment out "no threading"
  7. Launch dev Server with the newly created Launch profile (terminal will show no problems)
  8. Open Chrome, go to localhost:8000. Hello World Page will load, but VS Code will show "Could not load source '': Source unavailable.". Debugger paused on exception
  9. Go to localhost:8000/admin/. Debugger will pause on exception VariableDoesNotExist as shown below.

Logs

Impossible to get the output as explained from the Output panel in Python: it is empty and the development server doesn't show any problem. Everything works fine if running Django's development server from a shell or from the terminal. The log below is the message shown in red (as shown in the screenshot) with the VariableDoesNotExist exception.

Exception has occurred: VariableDoesNotExist
Failed lookup for key [is_popup] in [{'True': True, 'False': False, 'None': None}, {'csrf_token': <SimpleLazyObject: <function csrf.<locals>._get_val at 0x7fe8884261e0>>, 'request': <WSGIRequest: GET '/admin/login/?next=/admin/'>, 'user': <SimpleLazyObject: <django.contrib.auth.models.AnonymousUser object at 0x7fe8884740b8>>, 'perms': <django.contrib.auth.context_processors.PermWrapper object at 0x7fe88839c7b8>, 'messages': <django.contrib.messages.storage.fallback.FallbackStorage object at 0x7fe8884747f0>, 'DEFAULT_MESSAGE_LEVELS': {'DEBUG': 10, 'INFO': 20, 'SUCCESS': 25, 'WARNING': 30, 'ERROR': 40}}, {}, {'form': <AdminAuthenticationForm bound=False, valid=Unknown, fields=(username;password)>, 'view': <django.contrib.auth.views.LoginView object at 0x7fe8883da9b0>, 'site_title': 'Django site admin', 'site_header': 'Django administration', 'site_url': '/', 'has_permission': False, 'available_apps': [], 'title': 'Log in', 'app_path': '/admin/login/?next=/admin/', 'username': '', 'next': '/admin/', 'site': <django.contrib.sites.requests.RequestSite object at 0x7fe888474668>, 'site_name': '127.0.0.1:8000', 'LANGUAGE_CODE': 'en-us', 'LANGUAGE_BIDI': False}]
  File "/home/pymike00/Desktop/TestWithFreshProject/venv/lib/python3.7/site-packages/django/template/base.py", line 850, in _resolve_lookup
    (bit, current))  # missing attribute
  File "/home/pymike00/Desktop/TestWithFreshProject/venv/lib/python3.7/site-packages/django/template/base.py", line 796, in resolve
    value = self._resolve_lookup(context)
  File "/home/pymike00/Desktop/TestWithFreshProject/venv/lib/python3.7/site-packages/django/template/base.py", line 671, in resolve
    obj = self.var.resolve(context)
  File "/home/pymike00/Desktop/TestWithFreshProject/venv/lib/python3.7/site-packages/django/template/defaulttags.py", line 875, in eval
    return self.value.resolve(context, ignore_failures=True)
  File "/home/pymike00/Desktop/TestWithFreshProject/venv/lib/python3.7/site-packages/django/template/defaulttags.py", line 302, in render
    match = condition.eval(context)
  File "/home/pymike00/Desktop/TestWithFreshProject/venv/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/home/pymike00/Desktop/TestWithFreshProject/venv/lib/python3.7/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/home/pymike00/Desktop/TestWithFreshProject/venv/lib/python3.7/site

Output from Console under the Developer Tools panel (toggle Developer Tools on under Help; turn on source maps to make any tracebacks be useful by running Enable source map support for extension debugging)

[Extension Host] Python Extension: Cached data exists getEnvironmentVariables, <unknown source>
console.ts:134 [Extension Host] Python Extension: Cached data exists getEnvironmentVariables, /home/pymike00/Desktop/TestWithFreshProject/venv/lib/python3.7/site-packages/django/contrib/admin/templates/admin/base.html

The following is a screenshot of everything explained above. Chrome just hangs. No page or error is shown. Thanks for your precious work.

screen


@ohlr commented on Thu Mar 28 2019

I can confirm this bug:

Version: 1.32.3
Commit: a3db5be9b5c6ba46bb7555ec5d60178ecc2eaae4
Date: 2019-03-14T23:38:49.842Z
Electron: 3.1.6
Chrome: 66.0.3359.181
Node.js: 10.2.0
V8: 6.6.346.32
OS: Linux x64 4.15.0-46-generic

As quick fix use the following launch.json

        {
            "name": "Python: Django",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/my_project/manage.py",
            "console": "integratedTerminal",
            "args": [
                "runserver",
                "--noreload",
                "--nothreading",
            ],
            "django": false <- **if true template rendering does not work correctly**
        },

@pymike00 commented on Thu Mar 28 2019

This works on my setup as well, with "--nothreading" commented out (I have always been commenting out "--nothreading" as suggested by VSCode docs, considering that I normally develop on Chromium).
Thanks for sharing!

As quick fix use the following launch.json

    {
        "name": "Python: Django",
        "type": "python",
        "request": "launch",
        "program": "${workspaceFolder}/my_project/manage.py",
        "console": "integratedTerminal",
        "args": [
            "runserver",
            "--noreload",
            "--nothreading",
        ],
        "django": false <- **if true template rendering does not work correctly**
    },
@fabioz
Copy link
Contributor

fabioz commented Apr 4, 2019

Ok, I think I found the culprit...

The issue is that previously the VariableDoesNotExist wasn't always being gotten properly, which was fixed in the last release, so, when such an exception is raised now we properly report it.

The problem in this case is that there are some situations in which django raises that exception and it's expected to ignore it later on -- in which case we shouldn't report it.

Some examples on django ignoring the failures:

def resolve(self, context, ignore_failures=False):
if getattr(e, 'silent_variable_failure', False):

so, I need to check a better approach to detect those exceptions (possibly checking further in the chain or reading those variables to see if it should not report that failure).

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

No branches or pull requests

3 participants