From 0d2d49e77975249d36135f96dab5fcdd4f6d14d6 Mon Sep 17 00:00:00 2001 From: Terrajlz Date: Tue, 19 May 2026 13:37:38 -0400 Subject: [PATCH] fix FLASK_DEBUG env var ignored with click 8.4.0 (#6025) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In click 8.4.0, `get_parameter_source` returns `None` during eager callbacks instead of `ParameterSource.DEFAULT`. The guard in `_set_debug` only checked for `source is not None`, so it fell through and overwrote the user's `FLASK_DEBUG=1` env var with `"0"`. Treat `source is None` the same as `DEFAULT` — return early without overwriting the env var. Co-Authored-By: Claude Opus 4.6 --- src/flask/cli.py | 2 +- tests/test_cli.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/flask/cli.py b/src/flask/cli.py index 5b3116ed..14e1a17c 100644 --- a/src/flask/cli.py +++ b/src/flask/cli.py @@ -470,7 +470,7 @@ def _set_debug(ctx: click.Context, param: click.Option, value: bool) -> bool | N # that, let debug be set by env in that case. source = ctx.get_parameter_source(param.name) - if source is not None and source in ( + if source is None or source in ( ParameterSource.DEFAULT, ParameterSource.DEFAULT_MAP, ): diff --git a/tests/test_cli.py b/tests/test_cli.py index 2a34088b..d6e5d95a 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -382,6 +382,30 @@ def test_flaskgroup_debug(runner, set_debug_flag): assert result.output == f"{not set_debug_flag}\n" +def test_flaskgroup_debug_env(monkeypatch, runner): + """FLASK_DEBUG=1 env var should not be overwritten when --debug is not + explicitly passed. Regression test for click 8.4.0 where + get_parameter_source returns None during eager callbacks. + """ + monkeypatch.setenv("FLASK_DEBUG", "1") + + def create_app(): + app = Flask("flaskgroup") + return app + + @click.group(cls=FlaskGroup, create_app=create_app) + def cli(**params): + pass + + @cli.command() + def test(): + click.echo(os.environ.get("FLASK_DEBUG")) + + result = runner.invoke(cli, ["test"]) + assert result.exit_code == 0 + assert result.output.strip() == "1" + + def test_flaskgroup_nested(app, runner): cli = click.Group("cli") flask_group = FlaskGroup(name="flask", create_app=lambda: app)