Repository: apache/airflow
File: dev/breeze/src/airflow_breeze/prepare_providers/provider_distributions.py (Python)

Code before:
                command,
                check=True,
                cwd=target_provider_root_sources_path,
                env={
                    "SOURCE_DATE_EPOCH": str(get_provider_details(provider_id).source_date_epoch),
                },
            )
        except subprocess.CalledProcessError as ex:
            get_console().print(f"[error]The command returned an error {ex}")
            raise PrepareReleasePackageErrorBuildingPackageException()
        if remove_sdist:
            get_console().print("[warning]Removing sdist file to workaround flit bug on wheel-only packages")
            # Remove the sdist file if it was created
            package_prefix = "apache_airflow_providers_" + provider_id.replace(".", "_")
            for file in (target_provider_root_sources_path / "dist").glob(f"{package_prefix}*.tar.gz"):
                get_console().print(f"[info]Removing {file} to workaround flit bug on wheel-only packages")
                file.unlink(missing_ok=True)
    elif build_backend == "hatchling":
        command = [sys.executable, "-m", "hatch", "build", "-c", "-t", "custom"]
        if distribution_format == "sdist" or distribution_format == "both":
            command += ["-t", "sdist"]
        if distribution_format == "wheel" or distribution_format == "both":
            command += ["-t", "wheel"]
        env_copy = os.environ.copy()
        env_copy["SOURCE_DATE_EPOCH"] = str(get_provider_details(provider_id).source_date_epoch)
        run_command(
            cmd=command,
            cwd=target_provider_root_sources_path,
            env=env_copy,
            check=True,
        )
        shutil.copytree(target_provider_root_sources_path / "dist", AIRFLOW_DIST_PATH, dirs_exist_ok=True)
    else:
        get_console().print(f"[error]Unknown/unsupported build backend {build_backend}")
        raise PrepareReleasePackageErrorBuildingPackageException()
    get_console().print(
        f"\n[info]Prepared provider package {provider_id} in format {distribution_format}[/]\n"
    )


def move_built_distributions_and_cleanup(
    target_provider_root_sources_path: Path,
    dist_folder: Path,
    skip_cleanup: bool,
    delete_only_build_and_dist_folders: bool = False,
):
    for file in (target_provider_root_sources_path / "dist").glob("apache*"):
        get_console().print(f"[info]Moving {file} to {dist_folder}")
        # Shutil can move packages also between filesystems
        target_file = dist_folder / file.name
        target_file.unlink(missing_ok=True)

Review comment (bug):
The flit_core branch wraps run_command in a try/except CalledProcessError, but the hatchling branch does not. If hatch build fails, the exception will propagate with a raw traceback

Code after:
                check=True,
                cwd=target_provider_root_sources_path,
                env={
                    "SOURCE_DATE_EPOCH": str(get_provider_details(provider_id).source_date_epoch),
                },
            )
        except subprocess.CalledProcessError as ex:
            get_console().print(f"[error]The command returned an error {ex}")
            raise PrepareReleasePackageErrorBuildingPackageException()
        if remove_sdist:
            get_console().print("[warning]Removing sdist file to workaround flit bug on wheel-only packages")
            # Remove the sdist file if it was created
            package_prefix = "apache_airflow_providers_" + provider_id.replace(".", "_")
            for file in (target_provider_root_sources_path / "dist").glob(f"{package_prefix}*.tar.gz"):
                get_console().print(f"[info]Removing {file} to workaround flit bug on wheel-only packages")
                file.unlink(missing_ok=True)
    elif build_backend == "hatchling":
        command = [sys.executable, "-m", "hatch", "build", "-c", "-t", "custom"]
        if distribution_format == "sdist" or distribution_format == "both":
            command += ["-t", "sdist"]
        if distribution_format == "wheel" or distribution_format == "both":
            command += ["-t", "wheel"]
        try:
            run_command(
                cmd=command,
                cwd=target_provider_root_sources_path,
                env={
                    "SOURCE_DATE_EPOCH": str(get_provider_details(provider_id).source_date_epoch),
                },
                check=True,
            )
        except subprocess.CalledProcessError as ex:
            get_console().print(f"[error]The command returned an error {ex}")
            raise PrepareReleasePackageErrorBuildingPackageException()
        shutil.copytree(target_provider_root_sources_path / "dist", AIRFLOW_DIST_PATH, dirs_exist_ok=True)
    else:
        get_console().print(f"[error]Unknown/unsupported build backend {build_backend}")
        raise PrepareReleasePackageErrorBuildingPackageException()
    get_console().print(
        f"\n[info]Prepared provider package {provider_id} in format {distribution_format}[/]\n"
    )


def move_built_distributions_and_cleanup(
    target_provider_root_sources_path: Path,
    dist_folder: Path,
    skip_cleanup: bool,
    delete_only_build_and_dist_folders: bool = False,
):
    for file in (target_provider_root_sources_path / "dist").glob("apache*"):
        get_console().print(f"[info]Moving {file} to {dist_folder}")