[grpc] Fix protobuf compilation in isolated build environments (#16754)
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
# https://docs.sglang.io/platforms/cpu_server.html
|
# https://docs.sglang.io/platforms/cpu_server.html
|
||||||
[build-system]
|
[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"
|
build-backend = "setuptools.build_meta"
|
||||||
|
|
||||||
[project]
|
[project]
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
[build-system]
|
[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"
|
build-backend = "setuptools.build_meta"
|
||||||
|
|
||||||
[project]
|
[project]
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
[build-system]
|
[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"
|
build-backend = "setuptools.build_meta"
|
||||||
|
|
||||||
[project]
|
[project]
|
||||||
|
|||||||
+31
-19
@@ -6,8 +6,7 @@ to automatically generate gRPC/protobuf Python files from .proto sources
|
|||||||
when building the wheel or doing editable installs.
|
when building the wheel or doing editable installs.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import subprocess
|
import os
|
||||||
import sys
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from setuptools import setup
|
from setuptools import setup
|
||||||
@@ -32,31 +31,44 @@ def compile_proto():
|
|||||||
output_dir = proto_path.parent
|
output_dir = proto_path.parent
|
||||||
proto_dir = proto_path.parent
|
proto_dir = proto_path.parent
|
||||||
|
|
||||||
# Build the protoc command
|
# Import grpc_tools.protoc directly instead of running as subprocess.
|
||||||
cmd = [
|
# This ensures we use the grpcio-tools installed in the build environment,
|
||||||
sys.executable,
|
# since sys.executable may point to the main Python interpreter in
|
||||||
"-m",
|
# pip's isolated build environments.
|
||||||
"grpc_tools.protoc",
|
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{proto_dir}",
|
||||||
|
f"-I{grpc_tools_proto_path}", # Include path for well-known protos
|
||||||
f"--python_out={output_dir}",
|
f"--python_out={output_dir}",
|
||||||
f"--grpc_python_out={output_dir}",
|
f"--grpc_python_out={output_dir}",
|
||||||
f"--pyi_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:
|
try:
|
||||||
subprocess.run(
|
result = protoc.main(args)
|
||||||
cmd,
|
if result != 0:
|
||||||
capture_output=True,
|
raise SetupError(f"protoc failed with exit code {result}")
|
||||||
text=True,
|
finally:
|
||||||
cwd=proto_dir,
|
os.chdir(original_cwd)
|
||||||
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}")
|
|
||||||
|
|
||||||
# Fix imports in generated grpc file (change absolute to relative imports)
|
# Fix imports in generated grpc file (change absolute to relative imports)
|
||||||
_fix_imports(output_dir, proto_path.stem)
|
_fix_imports(output_dir, proto_path.stem)
|
||||||
|
|||||||
Reference in New Issue
Block a user