From 55a8dd00959f95242f1d461e0f9809e8f8fecd08 Mon Sep 17 00:00:00 2001 From: Simo Lin Date: Thu, 8 Jan 2026 13:09:01 -0800 Subject: [PATCH] [grpc] Fix protobuf compilation in isolated build environments (#16754) --- python/pyproject_cpu.toml | 2 +- python/pyproject_other.toml | 2 +- python/pyproject_xpu.toml | 2 +- python/setup.py | 50 +++++++++++++++++++++++-------------- 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/python/pyproject_cpu.toml b/python/pyproject_cpu.toml index 10df8a5f1..5a456266a 100644 --- a/python/pyproject_cpu.toml +++ b/python/pyproject_cpu.toml @@ -1,6 +1,6 @@ # https://docs.sglang.io/platforms/cpu_server.html [build-system] -requires = ["setuptools>=61.0", "setuptools-scm>=8.0", "wheel"] +requires = ["setuptools>=61.0", "setuptools-scm>=8.0", "wheel", "grpcio-tools==1.75.1"] build-backend = "setuptools.build_meta" [project] diff --git a/python/pyproject_other.toml b/python/pyproject_other.toml index 13c7a03bb..d767b39a1 100755 --- a/python/pyproject_other.toml +++ b/python/pyproject_other.toml @@ -1,5 +1,5 @@ [build-system] -requires = ["setuptools>=61.0", "setuptools-scm>=8.0", "wheel"] +requires = ["setuptools>=61.0", "setuptools-scm>=8.0", "wheel", "grpcio-tools==1.75.1"] build-backend = "setuptools.build_meta" [project] diff --git a/python/pyproject_xpu.toml b/python/pyproject_xpu.toml index fda33b0fa..b7e8200a5 100644 --- a/python/pyproject_xpu.toml +++ b/python/pyproject_xpu.toml @@ -1,5 +1,5 @@ [build-system] -requires = ["setuptools>=61.0", "setuptools-scm>=8.0", "wheel"] +requires = ["setuptools>=61.0", "setuptools-scm>=8.0", "wheel", "grpcio-tools==1.75.1"] build-backend = "setuptools.build_meta" [project] diff --git a/python/setup.py b/python/setup.py index 5925d80d6..e84796b1a 100644 --- a/python/setup.py +++ b/python/setup.py @@ -6,8 +6,7 @@ to automatically generate gRPC/protobuf Python files from .proto sources when building the wheel or doing editable installs. """ -import subprocess -import sys +import os from pathlib import Path from setuptools import setup @@ -32,31 +31,44 @@ def compile_proto(): output_dir = proto_path.parent proto_dir = proto_path.parent - # Build the protoc command - cmd = [ - sys.executable, - "-m", - "grpc_tools.protoc", + # Import grpc_tools.protoc directly instead of running as subprocess. + # This ensures we use the grpcio-tools installed in the build environment, + # since sys.executable may point to the main Python interpreter in + # pip's isolated build environments. + try: + import grpc_tools + from grpc_tools import protoc + except ImportError as e: + raise SetupError( + f"Failed to import grpc_tools: {e}. " + "Ensure grpcio-tools is listed in build-system.requires in pyproject.toml" + ) + + # Get the path to well-known proto files bundled with grpcio-tools + # (e.g., google/protobuf/timestamp.proto, google/protobuf/struct.proto) + grpc_tools_proto_path = Path(grpc_tools.__file__).parent / "_proto" + + # Build the protoc arguments (protoc.main expects argv-style list) + args = [ + "protoc", # argv[0] is the program name f"-I{proto_dir}", + f"-I{grpc_tools_proto_path}", # Include path for well-known protos f"--python_out={output_dir}", f"--grpc_python_out={output_dir}", f"--pyi_out={output_dir}", - proto_path.name, + str(proto_dir / proto_path.name), ] - print(f"Running: {' '.join(cmd)}") + print(f"Running protoc with args: {args[1:]}") + # Save and restore cwd since protoc may change it + original_cwd = os.getcwd() try: - subprocess.run( - cmd, - capture_output=True, - text=True, - cwd=proto_dir, - check=True, - ) - except subprocess.CalledProcessError as e: - error_msg = e.stderr or e.stdout or "Unknown error" - raise SetupError(f"protoc failed with exit code {e.returncode}: {error_msg}") + result = protoc.main(args) + if result != 0: + raise SetupError(f"protoc failed with exit code {result}") + finally: + os.chdir(original_cwd) # Fix imports in generated grpc file (change absolute to relative imports) _fix_imports(output_dir, proto_path.stem)