Skip to content

Fix airflow variables import exiting 0 even when imports fail - #72958

Open
roykoand wants to merge 1 commit into
apache:mainfrom
roykoand:fix-variables-import-exit-code
Open

roykoand wants to merge 1 commit into
apache:mainfrom
roykoand:fix-variables-import-exit-code

Conversation

@roykoand

@roykoand roykoand commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

airflow variables import counted and printed per-key failures (e.g. a key exceeding the 250-char limit enforced by Postgres/MySQL, unlike SQLite) but never caused the command to exit non-zero. A caller chaining airflow variables import vars.json && next_step would proceed as if the import had fully succeeded.

This mirrors the existing behavior of the sibling pools import command, which already does raise SystemExit(...) when any pool fails to import.

Also fixes a small grammar issue ("already exists" -> "already exist") in the two messages that describe multiple keys.


Was generative AI tooling used to co-author this PR?
  • Yes — Claude Code (Sonnet 5)

Previously, per-key failures during `airflow variables import` (e.g. a
key exceeding the 250-char limit enforced by Postgres/MySQL, unlike
SQLite) were counted and printed but never caused the command to exit
non-zero, unlike the equivalent `pools import` command. A caller
chaining `airflow variables import vars.json && next_step` would
proceed as if the import had fully succeeded.

Also fixes "already exists" -> "already exist" grammar in the two
messages describing multiple keys.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
# Test fail action - should fail when key1 already exists
Variable.set("key1", "original_value")
with pytest.raises(SystemExit, match="already exists"):
with pytest.raises(SystemExit, match="already exist"):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch!
Just one nit:
pytest.raises(match=...) is re.search, so this still matches the old already exists. — with variable_command.py reverted, this assertion passes and only the new test fails.

Suggested change
with pytest.raises(SystemExit, match="already exist"):
with pytest.raises(SystemExit, match=r"already exist\."):

f"The variables with these keys: {list(sorted(skipped))} were skipped because they already exist"
)
if fail_count:
raise SystemExit(f"{fail_count} variable(s) failed to be updated.")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pools import, the sibling this mirrors, names what failed (Failed to update pool(s): {', '.join(failed)}). Collecting the keys in the except above and listing them here would make a 200-variable import actionable — a count alone might isn't enough?

value, description = v["value"], v.get("description")
Variable.set(k, value, description, serialize_json=not isinstance(value, str))
except Exception as e:
print(f"Variable import failed: {e!r}")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
print(f"Variable import failed for key {k!r}: {e!r}")

@potiuk potiuk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, a failed import should not exit 0. Since this changes the exit code of an existing CLI command, it is effectively a clarification of the CLI's contract, and scripts that run airflow variables import will see it. So please add a newsfragment, airflow-core/newsfragments/72958.significant.rst. It should say that airflow variables import now exits non-zero when any variable fails to import, and that the variables that imported successfully stay saved (the import is not atomic), so anyone reacting to the exit code knows a partial import happened.

A few smaller points:

  • The error says how many keys failed, never which ones, and the per-key Variable import failed: {e!r} line doesn't include the key either. Collecting the failed keys and putting them in the SystemExit message, as airflow pools import does (Failed to update pool(s): ...), would make the failure actionable (see inline).
  • airflow connections import has the same "prints the error, exits 0" behaviour. It's fine to keep that out of this PR, but it would be good to fix it the same way in a follow-up so the two commands behave consistently.
  • The new test nests with mock.patch.object(...); the repo convention is @mock.patch decorators. It also doesn't check that good_key was still saved and counted ("1 of 2 variables successfully updated."), which is the partial-import behaviour the newsfragment will describe.

Drafted-by: Claude Code (Opus 5.5); reviewed by @potiuk before posting

Comment on lines +176 to +177
if fail_count:
raise SystemExit(f"{fail_count} variable(s) failed to be updated.")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Listing the failed keys here (collected next to fail_count += 1 above) would tell the operator what to fix, like pools import does:

    if failed_keys:
        raise SystemExit(f"Failed to import variable(s): {', '.join(sorted(failed_keys))}")

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants