Skip to content

Commit c4227cd

Browse files
author
Debug Agent
committed
fix(build): honor platform on local single-arch loads
1 parent cb7fecd commit c4227cd

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

openhands-agent-server/openhands/agent_server/docker/build.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,8 @@ def build_with_telemetry(opts: BuildOptions) -> BuildResult:
769769
if push:
770770
args += ["--platform", ",".join(opts.platforms), "--push"]
771771
else:
772+
if len(opts.platforms) == 1:
773+
args += ["--platform", opts.platforms[0]]
772774
args += ["--load"]
773775

774776
for t in tags:
@@ -842,7 +844,7 @@ def build_with_telemetry(opts: BuildOptions) -> BuildResult:
842844
logger.info(
843845
f"[build] Building target='{opts.target}' image='{opts.image}' "
844846
f"custom_tags='{opts.custom_tags}' from base='{opts.base_image}' "
845-
f"for platforms='{opts.platforms if push else 'local-arch'}'"
847+
f"for platforms='{opts.platforms}'"
846848
)
847849
logger.info(
848850
f"[build] Git ref='{opts.git_ref}' sha='{opts.git_sha}' "

tests/agent_server/test_docker_build.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,13 +569,64 @@ def fake_run(cmd: list[str], cwd: str | None = None):
569569
cmd, cwd = docker_calls[0]
570570
assert cwd == str(ctx)
571571
assert "--load" in cmd
572+
assert "--platform" in cmd and "linux/amd64" in cmd
572573
assert "--target" in cmd and "source-minimal" in cmd
573574
assert "--build-arg" in cmd
574575
assert "BASE_IMAGE=python:3.12" in cmd
575576
for tag in opts.all_tags:
576577
assert tag in cmd
577578

578579

580+
def test_local_build_with_multiple_platforms_skips_platform_flag(tmp_path: Path):
581+
from openhands.agent_server.docker.build import (
582+
BuildOptions,
583+
_default_sdk_project_root,
584+
build,
585+
)
586+
587+
ctx = tmp_path / "ctx"
588+
ctx.mkdir()
589+
docker_calls: list[tuple[list[str], str | None]] = []
590+
591+
def fake_run(cmd: list[str], cwd: str | None = None):
592+
if cmd[:3] != ["docker", "buildx", "build"]:
593+
raise AssertionError(f"unexpected command: {cmd}")
594+
docker_calls.append((cmd, cwd))
595+
return subprocess.CompletedProcess(cmd, 0, stdout="ok", stderr="")
596+
597+
opts = BuildOptions(
598+
base_image="python:3.12",
599+
custom_tags="python",
600+
git_sha="abc1234567890",
601+
git_ref="refs/heads/main",
602+
target="source-minimal",
603+
platforms=["linux/amd64", "linux/arm64"],
604+
push=False,
605+
sdk_project_root=_default_sdk_project_root(),
606+
)
607+
608+
with (
609+
patch(
610+
"openhands.agent_server.docker.build._make_build_context", return_value=ctx
611+
),
612+
patch("openhands.agent_server.docker.build._run", side_effect=fake_run),
613+
patch(
614+
"openhands.agent_server.docker.build._active_buildx_driver",
615+
return_value="docker-container",
616+
),
617+
patch(
618+
"openhands.agent_server.docker.build._default_local_cache_dir",
619+
return_value=tmp_path / "cache",
620+
),
621+
patch("openhands.agent_server.docker.build.shutil.rmtree"),
622+
):
623+
build(opts)
624+
625+
cmd = docker_calls[0][0]
626+
assert "--load" in cmd
627+
assert "--platform" not in cmd
628+
629+
579630
def test_build_can_reuse_same_prebuilt_sdist_multiple_times(tmp_path: Path):
580631
from openhands.agent_server.docker.build import (
581632
BuildOptions,

0 commit comments

Comments
 (0)