Opened 10 years ago

Closed 10 years ago

#21770 closed Bug (fixed)

Management commands autocomplete broken

Reported by: Akis Kesoglou Owned by: Aymeric Augustin
Component: Core (Management commands) Version: dev
Severity: Release blocker Keywords: app-loading
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Tabbing for command autocompletion in manage.py raises the following error:

$ ./manage.py sql<Tab>

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/<virtualenv>/src/django/django/core/management/__init__.py", line 426, in execute_from_command_line
    utility.execute()
  File "/<virtualenv>/src/django/django/core/management/__init__.py", line 379, in execute
    self.autocomplete()
  File "/<virtualenv>/src/django/django/core/management/__init__.py", line 326, in autocomplete
    subcommands = list(get_commands()) + ['help']
  File "/<virtualenv>/bin/../lib/python3.3/functools.py", line 251, in wrapper
    result = user_function(*args, **kwds)
  File "/<virtualenv>/src/django/django/core/management/__init__.py", line 118, in get_commands
    app_configs = apps.get_app_configs()
  File "/<virtualenv>/src/django/django/apps/registry.py", line 121, in get_app_configs
    self.check_ready()
  File "/<virtualenv>/src/django/django/apps/registry.py", line 115, in check_ready
    raise RuntimeError("App registry isn't ready yet.")
RuntimeError: App registry isn't ready yet.

Bringing the issue up in #django-dev, mjtamlyn suggested moving the try/catch with django.setup() block to the beginning of ManagementUtility.execute() method, which fixed the issue. However, the change broke the test suite, specifically many tests from test_admin_scripts.

I'm not at all familiar with that (huge) part of the test suite, so i'm not sure how to go about it, even though i'm willing to help.

Python 3.3, Django@9918c11114ac3

Change History (6)

comment:1 by Aymeric Augustin, 10 years ago

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

comment:2 by Aymeric Augustin, 10 years ago

Keywords: app-loading added; AppConfig admin_script manage django-admin removed
Owner: changed from nobody to Aymeric Augustin
Status: newassigned

comment:3 by Aymeric Augustin, 10 years ago

The following patch fixes the issue:

diff --git a/django/core/management/__init__.py b/django/core/management/__init__.py
index 6d0cb7e..b6a7d53 100644
--- a/django/core/management/__init__.py
+++ b/django/core/management/__init__.py
@@ -376,7 +376,6 @@ class ManagementUtility(object):
         parser = LaxOptionParser(usage="%prog subcommand [options] [args]",
                                  version=get_version(),
                                  option_list=BaseCommand.option_list)
-        self.autocomplete()
         try:
             options, args = parser.parse_args(self.argv)
             handle_default_options(options)
@@ -391,6 +390,8 @@ class ManagementUtility(object):
         else:
             django.setup()
 
+        self.autocomplete()
+
         try:
             subcommand = self.argv[1]
         except IndexError:

It also makes autocompletion much slower. In order to discover management commands in applications referenced by an AppConfig in INSTALLED_APPS, one must populate the app registry, and that's slow.

I don't see how we can avoid it.

comment:4 by Aymeric Augustin, 10 years ago

An alternative would be to import only apps, not models.

I'm reluctant to add this to the app registry for the sole purpose of autocompletion.

comment:5 by Akis Kesoglou, 10 years ago

I tried the patch and it works here too. FWIW, I personally didn't see any noticeable difference in autocompletion delay, though the project I tried it on has only about 15-20 models including Django's.

comment:6 by Aymeric Augustin <aymeric.augustin@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

In 0a7588dd3f5ec5beef0298389661e78fd63ad6a6:

Fixed #21770 -- Updated autocomplete for app-loading.

Thanks dfunckt for the report.

Note: See TracTickets for help on using tickets.
Back to Top