[CI][Fix] cmake: fix 'str' object has no attribute 'removeprefix'#42513
[CI][Fix] cmake: fix 'str' object has no attribute 'removeprefix'#42513sergiitk wants to merge 3 commits into
Conversation
The `str.removeprefix` method was introduced in Python 3.9. Some CI environments still use older versions of Python, causing template rendering to fail. Replacing it with a slice-based approach for compatibility. We should revert this once we upgrade `grpc-ubuntu20-large` Kokoro base VM image.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request replaces the use of Python's str.removeprefix in templates/CMakeLists.txt.template with a slice-based alternative to support Python versions older than 3.9. The reviewer pointed out that this fix is incomplete because removeprefix is still used elsewhere in the same template file (on line 40), which will continue to cause generation failures in CI environments running older Python versions.
| download_archive( | ||
| <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>/${external_proto_library.destination} | ||
| https://storage.googleapis.com/grpc-bazel-mirror/${download_url.removeprefix("https://")} | ||
| https://storage.googleapis.com/grpc-bazel-mirror/${download_url[8:] if download_url.startswith("https://") else download_url} |
There was a problem hiding this comment.
The fix is incomplete because removeprefix is still used on line 40 of this template:
path = path.removeprefix(lib.strip_path_prefix)Since third_party_proto_import_path is called during template rendering (e.g., on line 49 and line 720), the generation will still fail with 'str' object has no attribute 'removeprefix' in CI environments running Python < 3.9.
Please also update line 40 to a compatible alternative, such as:
if path.startswith(lib.strip_path_prefix):
path = path[len(lib.strip_path_prefix):]There was a problem hiding this comment.
I saw that, but I AFAIK unlike bazel download, we don't run that particular piece of code in environments with outdated python. But if we do, I'd be happy to fix it in a follow-up PR.
|
Without this change Traceback (most recent call last):
File "tools/buildgen/_mako_renderer.py", line 60, in render_template
template.render_context(context)
File "/usr/local/lib/python3.8/dist-packages/mako/template.py", line 451, in render_context
runtime._render_context(self, self.callable_, context, *args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/mako/runtime.py", line 916, in _render_context
_exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
File "/usr/local/lib/python3.8/dist-packages/mako/runtime.py", line 943, in _exec_template
callable_(context, *args, **kwargs)
File "./templates/CMakeLists.txt.template", line 517, in render_body
[https://storage.googleapis.com/grpc-bazel-mirror/${download_url.removeprefix(](https://www.google.com/url?q=https://storage.googleapis.com/grpc-bazel-mirror/$%7Bdownload_url.removeprefix(&sa=D)"https://")}
AttributeError: 'str' object has no attribute 'removeprefix'
Traceback (most recent call last):
File "tools/buildgen/_mako_renderer.py", line 179, in <module>
main(sys.argv[1:])
File "tools/buildgen/_mako_renderer.py", line 130, in main
render_template(
File "tools/buildgen/_mako_renderer.py", line 60, in render_template
template.render_context(context)
File "/usr/local/lib/python3.8/dist-packages/mako/template.py", line 451, in render_context
runtime._render_context(self, self.callable_, context, *args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/mako/runtime.py", line 916, in _render_context
_exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
File "/usr/local/lib/python3.8/dist-packages/mako/runtime.py", line 943, in _exec_template
callable_(context, *args, **kwargs)
File "__templates_CMakeLists_txt_template", line 493, in render_body
AttributeError: 'str' object has no attribute 'removeprefix'
2026-05-28 01:47:19,534 FAILED: ./CMakeLists.txt [ret=1, pid=23436, time=0.3sec]
2026-05-28 01:47:19,534 WAITING: ETA 0.0 sec; 0 queued, 1 jobs running, 21 complete, 1 failed (load 1.00)Adhoc run |
|
Last adhoc failed with Traceback (most recent call last):
File "tools/buildgen/_mako_renderer.py", line 60, in render_template
template.render_context(context)
File "/usr/local/lib/python3.8/dist-packages/mako/template.py", line 451, in render_context
runtime._render_context(self, self.callable_, context, *args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/mako/runtime.py", line 916, in _render_context
_exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
File "/usr/local/lib/python3.8/dist-packages/mako/runtime.py", line 943, in _exec_template
callable_(context, *args, **kwargs)
File "./templates/CMakeLists.txt.template", line 729, in render_body
${src} ${third_party_proto_import_path(src)}
File "./templates/CMakeLists.txt.template", line 38, in third_party_proto_import_path
path = path.removeprefix(lib.strip_path_prefix)
AttributeError: 'str' object has no attribute 'removeprefix'
Traceback (most recent call last):
File "tools/buildgen/_mako_renderer.py", line 179, in <module>
main(sys.argv[1:])
File "tools/buildgen/_mako_renderer.py", line 130, in main
render_template(
File "tools/buildgen/_mako_renderer.py", line 60, in render_template
template.render_context(context)
File "/usr/local/lib/python3.8/dist-packages/mako/template.py", line 451, in render_context
runtime._render_context(self, self.callable_, context, *args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/mako/runtime.py", line 916, in _render_context
_exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
File "/usr/local/lib/python3.8/dist-packages/mako/runtime.py", line 943, in _exec_template
callable_(context, *args, **kwargs)
File "__templates_CMakeLists_txt_template", line 623, in render_body
File "__templates_CMakeLists_txt_template", line 137, in third_party_proto_import_path
AttributeError: 'str' object has no attribute 'removeprefix'Meaning we need to fix another |
|
Adhoc failed (expected), but the build advanced past the python syntax error. |
…pc#42513) The `str.removeprefix` method was introduced in Python 3.9. Some CI environments still use older versions of Python, causing template rendering to fail. Replacing it with a slice-based approach for compatibility. We should revert this once we upgrade `grpc-ubuntu20-large` Kokoro base VM image. The issue was introduced in grpc#42172 - but it's a CI tech debt issue. Normally we should be assuming that we run a modern python. Closes grpc#42513 COPYBARA_INTEGRATE_REVIEW=grpc#42513 from sergiitk:fix/ci/cmake-old-python-removeprefix eefdf17 PiperOrigin-RevId: 923156519
…pc#42513) The `str.removeprefix` method was introduced in Python 3.9. Some CI environments still use older versions of Python, causing template rendering to fail. Replacing it with a slice-based approach for compatibility. We should revert this once we upgrade `grpc-ubuntu20-large` Kokoro base VM image. The issue was introduced in grpc#42172 - but it's a CI tech debt issue. Normally we should be assuming that we run a modern python. Closes grpc#42513 COPYBARA_INTEGRATE_REVIEW=grpc#42513 from sergiitk:fix/ci/cmake-old-python-removeprefix eefdf17 PiperOrigin-RevId: 923156519
The
str.removeprefixmethod was introduced in Python 3.9. Some CI environments still use older versions of Python, causing template rendering to fail. Replacing it with a slice-based approach for compatibility.We should revert this once we upgrade
grpc-ubuntu20-largeKokoro base VM image.The issue was introduced in #42172 - but it's a CI tech debt issue. Normally we should be assuming that we run a modern python.