From a9e169b00cafe812032745b7e1c1b3152ee8b733 Mon Sep 17 00:00:00 2001 From: shenlei Date: Tue, 3 Nov 2026 10:15:36 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=9C=AC=E5=9C=B0=20Pod=20=E7=B4=A2?= =?UTF-8?q?=E5=BC=95=E7=BC=BA=E5=B0=91=E6=89=80=E9=9C=80=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E6=97=B6=E8=87=AA=E5=8A=A8=20--repo-update=20=E9=87=8D?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- backend/services/build_service.py | 41 ++++++++++++++++++++----------- tests/test_build_service.py | 22 +++++++++++++++++ 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/backend/services/build_service.py b/backend/services/build_service.py index af21d6f..4a22288 100644 --- a/backend/services/build_service.py +++ b/backend/services/build_service.py @@ -860,6 +860,27 @@ def _pod_dir(build_dir: Path, xcode: dict = None) -> Path: return None +_POD_SPEC_MISSING = "Unable to find a specification for" + + +async def _run_pod(task_id: str, pod_dir: Path, *extra_args: str) -> tuple: + """执行 pod install,实时输出日志,返回 (退出码, 输出行)。""" + process = await asyncio.create_subprocess_exec( + "pod", "install", *extra_args, + cwd=str(pod_dir), + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.STDOUT, + ) + output = [] + async for line in process.stdout: + decoded = line.decode("utf-8", errors="replace").strip() + if decoded: + output.append(decoded) + await log_streamer.emit(task_id, decoded) + await process.wait() + return process.returncode, output + + async def run_pod_install(task_id: str, build_dir: Path, xcode: dict = None): """安装 CocoaPods 依赖""" await log_streamer.emit_step(task_id, "安装依赖") @@ -871,21 +892,13 @@ async def run_pod_install(task_id: str, build_dir: Path, xcode: dict = None): if pod_dir != build_dir: await log_streamer.emit(task_id, f"Podfile 目录: {pod_dir.relative_to(build_dir)}") - process = await asyncio.create_subprocess_exec( - "pod", "install", - cwd=str(pod_dir), - stdout=asyncio.subprocess.PIPE, - stderr=asyncio.subprocess.STDOUT, - ) + returncode, output = await _run_pod(task_id, pod_dir) + # 打包机本地的 Specs 索引过旧时找不到新版本 Pod,更新索引后重试一次 + if returncode != 0 and any(_POD_SPEC_MISSING in line for line in output): + await log_streamer.emit(task_id, "本地 Pod 索引缺少所需版本,更新索引后重试(pod install --repo-update)", level="warn") + returncode, output = await _run_pod(task_id, pod_dir, "--repo-update") - async for line in process.stdout: - decoded = line.decode("utf-8", errors="replace").strip() - if decoded: - await log_streamer.emit(task_id, decoded) - - await process.wait() - - if process.returncode != 0: + if returncode != 0: raise BuildError("pod install 失败", category="config") patched_files = await asyncio.to_thread(_patch_afnetworking_private_headers, pod_dir) diff --git a/tests/test_build_service.py b/tests/test_build_service.py index 61d34c3..3fa3890 100644 --- a/tests/test_build_service.py +++ b/tests/test_build_service.py @@ -295,6 +295,28 @@ async def test_run_pod_install_uses_xcode_subdirectory(tmp_path, log_streamer): assert mock_exec.call_args.kwargs["cwd"] == str(sub) +async def test_run_pod_install_retries_with_repo_update(tmp_path, log_streamer): + """本地 Specs 索引过旧找不到版本时,带 --repo-update 重试一次。""" + (tmp_path / "Podfile").write_text("pod 'OpenSSL-Universal', '1.1.2301'") + missing = _make_mock_process(1, b"[!] Unable to find a specification for `OpenSSL-Universal (= 1.1.2301)`\n") + + with patch("asyncio.create_subprocess_exec", side_effect=[missing, _make_mock_process()]) as mock_exec: + await run_pod_install("t1", tmp_path) + + assert mock_exec.call_args_list[0].args == ("pod", "install") + assert mock_exec.call_args_list[1].args == ("pod", "install", "--repo-update") + + +async def test_run_pod_install_other_failure_not_retried(tmp_path, log_streamer): + (tmp_path / "Podfile").write_text("pod 'SnapKit'") + + with patch("asyncio.create_subprocess_exec", return_value=_make_mock_process(1, b"[!] Some error\n")) as mock_exec: + with pytest.raises(Exception, match="pod install 失败"): + await run_pod_install("t1", tmp_path) + + assert mock_exec.call_count == 1 + + async def test_build_project_passes_scheme_as_exec_argument(tmp_path, log_streamer): """Scheme 中的 shell 特殊字符只能作为 xcodebuild 参数,不能被执行。""" build_dir = tmp_path / "build"