Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/actions/rust-build-release/src/cross_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import hashlib
import shutil
import subprocess
import sys
import tempfile
import urllib.error
Expand Down Expand Up @@ -214,7 +215,7 @@ def version_compare(installed: str, required: str) -> bool:
required_cross_version,
]
)
except ProcessExecutionError:
except (ProcessExecutionError, subprocess.CalledProcessError):
try:
run_cmd(
local["cargo"][
Expand All @@ -227,7 +228,7 @@ def version_compare(installed: str, required: str) -> bool:
f"v{required_cross_version}",
]
)
except ProcessExecutionError:
except (ProcessExecutionError, subprocess.CalledProcessError):
if sys.platform == "win32":
typer.echo(
"::warning:: cross install failed; continuing without "
Expand Down
36 changes: 23 additions & 13 deletions .github/actions/rust-build-release/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import collections.abc as cabc # noqa: TC003
import os
import shutil
import subprocess
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

❌ New issue: Overall Code Complexity
This module has a mean cyclomatic complexity of 4.06 across 33 functions. The mean complexity threshold is 4

Suppress

import sys
import typing as typ
from pathlib import Path
Expand Down Expand Up @@ -39,8 +40,6 @@
)

if typ.TYPE_CHECKING:
import subprocess

from cmd_utils import SupportsFormulate
from cmd_utils_importer import import_cmd_utils

Expand Down Expand Up @@ -194,9 +193,9 @@ def _target_is_windows(target: str) -> bool:

def should_probe_container(host_platform: str, target: str) -> bool:
"""Determine whether container runtimes should be probed."""
if host_platform != "win32":
return True
return not _target_is_windows(target)
if host_platform == "win32":
return not _target_is_windows(target)
return target.strip().lower() != DEFAULT_HOST_TARGET.strip().lower()


def _list_installed_toolchains(rustup_exec: str) -> list[str]:
Expand Down Expand Up @@ -250,7 +249,7 @@ def _probe_runtime(name: str) -> bool:
"""Return True when *name* runtime is available, tolerating probe timeouts."""
try:
return runtime_available(name)
except ProcessTimedOut as exc:
except (ProcessTimedOut, subprocess.TimeoutExpired) as exc:
timeout = getattr(exc, "timeout", None)
duration = f" after {timeout}s" if timeout else ""
message = (
Expand Down Expand Up @@ -525,27 +524,33 @@ def _announce_build_mode(decision: _CrossDecision) -> None:
)


_UNSET_ENGINE = object()


def _configure_cross_container_engine(
decision: _CrossDecision,
*,
previous_engine: str | None | object = _UNSET_ENGINE,
) -> tuple[str | None, str | None]:
"""Ensure CROSS_CONTAINER_ENGINE matches the active cross backend."""
previous_engine = os.environ.get("CROSS_CONTAINER_ENGINE")
if previous_engine is _UNSET_ENGINE:
previous_engine = os.environ.get("CROSS_CONTAINER_ENGINE")

if not decision.use_cross:
return previous_engine, None
return typ.cast("str | None", previous_engine), None

if decision.use_cross_local_backend:
return previous_engine, None
return typ.cast("str | None", previous_engine), None

if previous_engine is not None:
return previous_engine, None
return typ.cast("str | None", previous_engine), None

engine = decision.container_engine
if engine is None:
return previous_engine, None
return typ.cast("str | None", previous_engine), None

os.environ["CROSS_CONTAINER_ENGINE"] = engine
return previous_engine, engine
return typ.cast("str | None", previous_engine), engine


def _restore_container_engine(
Expand Down Expand Up @@ -693,6 +698,9 @@ def main(
),
) -> None:
"""Build the project for *target* using *toolchain*."""
if isinstance(features, typer.models.OptionInfo):
features = ""
initial_engine = os.environ.get("CROSS_CONTAINER_ENGINE")
target_to_build = _resolve_target_argument(target)
rustup_exec = _ensure_rustup_exec()
toolchain_name, installed_names = _resolve_toolchain(
Expand Down Expand Up @@ -723,7 +731,9 @@ def main(

_announce_build_mode(decision)

previous_engine, applied_engine = _configure_cross_container_engine(decision)
previous_engine, applied_engine = _configure_cross_container_engine(
decision, previous_engine=initial_engine
)

manifest_path = _resolve_manifest_path()
manifest_argument = _manifest_argument(manifest_path)
Expand Down
4 changes: 3 additions & 1 deletion .github/actions/rust-build-release/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,9 @@ def patch_common_main_deps(
harness.patch_attr("_resolve_toolchain", lambda *_: ("stable", ["stable"]))
harness.patch_attr("_ensure_target_installed", lambda *_: True)
harness.patch_attr("configure_windows_linkers", lambda *_, **__: None)
harness.patch_attr("_configure_cross_container_engine", lambda *_: (None, None))
harness.patch_attr(
"_configure_cross_container_engine", lambda *_args, **_kwargs: (None, None)
)
harness.patch_attr("_restore_container_engine", lambda *_, **__: None)
return harness

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,12 @@ def test_installs_cross_without_container_runtime(
cross_module: ModuleType,
module_harness: HarnessFactory,
cmd_mox: CmdMox,
setup_manifest: Path,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

❌ New issue: Excess Number of Function Arguments
test_installs_cross_without_container_runtime has 5 arguments, max arguments = 4

Suppress

) -> None:
"""Installs cross even when no container runtime is available."""
cross_env = module_harness(cross_module)
app_env = module_harness(main_module)
assert setup_manifest.is_file()

default_toolchain = main_module.DEFAULT_TOOLCHAIN
rustup_stdout = f"{default_toolchain}-x86_64-unknown-linux-gnu\n"
Expand All @@ -380,6 +382,7 @@ def fake_which(name: str) -> str | None:
app_env.patch_shutil_which(fake_which)
cross_env.patch_run_cmd()
app_env.patch_run_cmd()
app_env.patch_attr("ensure_cross", cross_module.ensure_cross)

cmd_mox.replay()
main_module.main("x86_64-unknown-linux-gnu", default_toolchain)
Expand Down Expand Up @@ -444,10 +447,12 @@ def test_falls_back_to_cargo_when_runtime_unusable(
main_module: ModuleType,
cross_module: ModuleType,
module_harness: HarnessFactory,
setup_manifest: Path,
) -> None:
"""Falls back to cargo when docker exists but is unusable."""
cross_env = module_harness(cross_module)
app_env = module_harness(main_module)
assert setup_manifest.is_file()

docker_path = "/usr/bin/docker"
cross_path = "/usr/bin/cross"
Expand All @@ -462,6 +467,7 @@ def fake_which(name: str) -> str | None:

cross_env.patch_shutil_which(fake_which)
app_env.patch_shutil_which(fake_which)
app_env.patch_attr("ensure_cross", cross_module.ensure_cross)

default_toolchain = main_module.DEFAULT_TOOLCHAIN

Expand All @@ -486,6 +492,7 @@ def fake_run(

cross_env.patch_subprocess_run(fake_run)
app_env.patch_subprocess_run(fake_run)
app_env.patch_attr("_probe_runtime", lambda *_: False)

main_module.main("x86_64-unknown-linux-gnu", default_toolchain)

Expand Down
Loading
Loading