fix(backend): log git import failures once as readable messages
Why
When the import of a Git repository branch fails, the user reads a Python traceback in the task log instead of the reason. The same failure is often logged two or three times. A failed import of the default branch also stops the import of the repository's other branches.
Goal: a user who views the task log of a failed import reads one line that says what is wrong and where, for example:
Failed to import branch 'feature-x': GraphQL query 'backbone_service' (queries/backbone.gql): Violates uniqueness constraint 'name'
A failure on one branch never stops the import of another branch.
Non-goals:
- Retrying a failed import in a later sync cycle. Users can run "import current commit again" on the repository.
- Deciding how infrastructure failures should behave, such as an unreachable Infrahub API or a lock timeout.
- Errors in one repository that stop the scheduled sync of the other repositories. This happens on
developtoo and will be filed as a separate bug.
Closes IFC-3304
What changed
What users view differently:
- Readable failures: a failure caused by the repository content writes one error entry to the task log with no traceback. The entry names the
.infrahub.ymlentry and its file. Examples are:- an invalid schema file or an invalid object in an object file;
- a GraphQL query whose name already exists;
- a check or transform that references a GraphQL query that does not exist;
- a Python file with a syntax error;
- a missing object file;
- a commit that is not in the local clone.
- Unknown failures: a failure that is not recognised is logged once, with its traceback. That traceback shows which error type still needs a readable message.
- Branch status: the failed branch shows
ERROR_IMPORTin both cases. - Failure on one branch: an import failure on one branch, of any type, no longer stops the import of the other branches. This covers the scheduled sync, adding a repository, and the first sync of a new clone.
- Failed default branch when adding a repository: the add flow still imports the other branches and tells the other workers to fetch, then ends as failed.
- Connection and credential errors: these still stop the sync of that repository for the cycle, as before. The next cycle tries again. They do not mark every remaining branch
ERROR_IMPORT, because nothing would reset that status until a new commit is pushed. - Commit event failure: if sending the commit event fails after every object is imported, the branch keeps
IN_SYNCinstead of showingERROR_IMPORT. - Wrong
class_name(behavior change): a check or Python transform whose class is not defined in its module now fails the branch import with "The specified class X was not found within the module". Before, the entry was skipped without any message and its existing definition was deleted from Infrahub. Generators already failed this way. - Removed definitions: if deleting a definition that the repository no longer declares fails, the message names it, for example
Deleting CoreGraphQLQuery 'old_query', removed from the repository: <reason>. - Jinja2 dependency analysis: a failure caused by the repository content is logged without a traceback, and its message includes the cause. Other failures keep their traceback.
Implementation notes:
- One mapping function:
describe_import_errorinbackend/infrahub/git/import_errors.pyturns an exception into a readable message. To give a new error type a readable message, add acaseto this function only. - One boundary: building and applying every import run inside the same boundary in
integrator.py. The boundary setsERROR_IMPORT, logs the failure once, and raisesRepositoryImportError. A failedERROR_IMPORTwrite is logged and does not replace the import failure. - Entry names: each loop over
.infrahub.ymlentries adds the entry name and file to the exception as a note. Each kind of entry has one label helper, so an entry is named the same way whichever step fails. - Containment does not depend on the boundary:
import_branchinsync.pyimports one branch and returns any failure instead of raising it. The sync loop, the add flow and the bootstrap of a new clone all use it. - One log entry per failure: the import error entry carries
repository,branch,stepandreasonas log record fields.raise_if_branches_failednow writes its warning only for branches that failed before the import (stepcollection). Any alert rule that matched that warning for import failures needs to match the error entry instead. - Prefect tracebacks: the import steps are no longer Prefect tasks, because Prefect logs a traceback when an exception leaves a task.
RepositoryImportErrorandRepositoryBranchesFailedErrorare registered with the existing traceback filter that webhooks use. - #10822: the branch is rebased on
developafter #10822 (delete removed definitions after their dependents). The deletions run inside the boundary, in #10822's order.
What stayed the same:
- No schema, GraphQL or API changes.
- Prefect's own
Finished in state Failed(...)entry is still written when a flow fails.
Suggested review order
backend/infrahub/git/import_errors.py: the mapping function, the logging function and the exception type.backend/infrahub/git/integrator.py: the boundary inbuild_import_planandapply_import_plan, and theimport_entrylabels in each loop.backend/infrahub/git/sync.pyandtasks.py: per-branch containment.dev/knowledge/backend/git-sync.md: the "Import failures" section describes the design.- Tests.
How to review
-
Check that every place that imports a branch catches failures per branch:
RepositorySyncer.sync,RepositoryAdder.addandbootstrap_local_repository. -
Check the error types that
describe_import_errorrecognises. Some content errors still log a traceback, for example:- an unknown kind in an object file (
SchemaNotFoundError); - a plain
ImportErrorin a transform module; - a file of the wrong kind listed under objects, which the SDK raises as a plain
ValueError. IHS-301 asks the SDK to raiseValidationErrorinstead.
Each can be added later with one
case. - an unknown kind in an object file (
-
The wrong-
class_namechange makes imports fail that used to pass with the entry skipped.
Findings from the local cubic review that were not fixed
- Declined: "every pydantic
ValidationErroris mapped without a traceback". IFC-3304 lists pydanticValidationErrorin the first set of mapped errors, because most of them come from validating.infrahub.ymland the repository's files. A pydantic error raised by Infrahub's own models also loses its traceback; this can be narrowed later if it hides a real bug. - Deferred: "a failure in
add_tagsorget_commit_valuemarks the branchERROR_IMPORT". These are infrastructure failures, and how they should behave is not decided in IFC-3304.
How to test
uv run pytest backend/tests/unit/git
INFRAHUB_USE_TEST_CONTAINERS=true uv run pytest backend/tests/component/git -n 0
INFRAHUB_USE_TEST_CONTAINERS=true uv run pytest backend/tests/integration/git/test_import_removed_definitions.py -n 0
Local results on the final commit:
- Git unit, component and #10822 integration tests: 500 passed, and 1 test that was already expected to fail.
- Lint and type checks: ruff and mypy passed.
- Regression checks: the containment tests fail when the per-branch catch or the guarded status write is removed. The new unknown-query and wrong-class tests fail when their fix is removed.
- Not re-run after the rebase: the full backend unit suite and the other integration tests. CI covers them.
Impact & rollout
- Backward compatibility: a check or Python transform with a
class_namethat is not in its module now fails the import (see above). Otherwise, a branch whose import fails still showsERROR_IMPORT, as before, and the task log has fewer entries. - Config/env changes: none.
- Deployment notes: safe to deploy.
Checklist
- Tests added/updated
- Changelog entry added (
uv run towncrier create ...) - External docs updated (if user-facing or ops-facing change): not needed, the change affects log messages and import failures only
- Internal .md docs updated (internal knowledge and AI code tools knowledge)
- I have reviewed AI generated content