From aa13521d42bfdb66bb0d89005cbe54a84eaa4fda Mon Sep 17 00:00:00 2001 From: David Lord Date: Tue, 22 Feb 2022 19:04:03 -0800 Subject: [PATCH] lazy loading preserves click context --- CHANGES.rst | 3 +++ src/flask/cli.py | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index aa4b53ea..4b962e58 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -42,6 +42,9 @@ Unreleased - Add an ``--exclude-patterns`` option to the ``flask run`` CLI command to specify patterns that will be ignored by the reloader. :issue:`4188` +- When using lazy loading (the default with the debugger), the Click + context from the ``flask run`` command remains available in the + loader thread. :issue:`4460` Version 2.0.3 diff --git a/src/flask/cli.py b/src/flask/cli.py index d59942b8..972698df 100644 --- a/src/flask/cli.py +++ b/src/flask/cli.py @@ -301,9 +301,17 @@ class DispatchingApp: self._load_in_background() def _load_in_background(self): + # Store the Click context and push it in the loader thread so + # script_info is still available. + ctx = click.get_current_context(silent=True) + def _load_app(): __traceback_hide__ = True # noqa: F841 + with self._lock: + if ctx is not None: + click.globals.push_context(ctx) + try: self._load_unlocked() except Exception as e: