code: def retry_building_docs_if_needed(
      all_build_errors: dict[str, list[DocBuildError]],
      all_spelling_errors: dict[str, list[SpellingError]],
      args: argparse.Namespace,
      docs_only: bool,
      jobs: int,
      package_build_errors: dict[str, list[DocBuildError]],
      originally_built_packages: list[str],
      rebuild_all_packages: bool,
  ) -> dict[str, list[DocBuildError]]:
      to_retry_packages = [
          package_name
          for package_name, errors in package_build_errors.items()
          if any(
              any((m in e.message) for m in ERRORS_ELIGIBLE_TO_REBUILD)
              for e in errors
          )
      ]
      if not to_retry_packages:
          console.print(
              "[green]No packages to retry. No more passes are needed.[/]"
          )
          return package_build_errors
      console.print(
          "[warning] Some packages failed to build due to dependencies. We need another pass.[/]"
      )
      # if we are rebuilding all packages, we need to retry all packages
      # even if there is one package to rebuild only
      if rebuild_all_packages:
          console.print(
              "[warning]Rebuilding all originally built package as this is the first build pass:[/]"
          )
          to_retry_packages = originally_built_packages
      console.print(
          f"[bright_blue]Packages to rebuild: {to_retry_packages}[/]"
      )
      for package_name in to_retry_packages:
          if package_name in all_build_errors:
              del all_build_errors[package_name]
          if package_name in all_spelling_errors:
              del all_spelling_errors[package_name]
      (
          package_build_errors,
          package_spelling_errors,
      ) = build_docs_for_packages(
          packages_to_build=to_retry_packages,
          docs_only=docs_only,
          spellcheck_only=False,
          jobs=jobs,
          verbose=args.verbose,
      )
      if package_build_errors:
          all_build_errors.update(package_build_errors)
      if package_spelling_errors:
          all_spelling_errors.update(package_spelling_errors)
      return package_build_errors
  
imports: from __future__ import annotations
  import argparse
  import itertools
  import multiprocessing
  import os
  import sys
  from collections import defaultdict
  from typing import Any, Callable, Iterable, NamedTuple, TypeVar
  from rich.console import Console
  from tabulate import tabulate
  from docs.exts.docs_build import dev_index_generator, lint_checks
  from docs.exts.docs_build.code_utils import CONSOLE_WIDTH, PROVIDER_INIT_FILE
  from docs.exts.docs_build.docs_builder import DOCS_DIR, AirflowDocsBuilder, get_available_packages
  from docs.exts.docs_build.errors import DocBuildError, display_errors_summary
  from docs.exts.docs_build.fetch_inventories import fetch_inventories
  from docs.exts.docs_build.github_action_utils import with_group
  from docs.exts.docs_build.package_filter import process_package_filters
  from docs.exts.docs_build.spelling_checks import SpellingError, display_spelling_error_summary
label_desc: Use image node_id 16 for calling a global function with example usage: retry_building_docs_if_needed(all_build_errors, all_spelling_errors, args, docs_only, jobs, package_build_errors, originally_built_packages, rebuild_all_packages) and returns: package_build_errors, package_build_errors