Skip to content

[CI][Fix] cmake: fix 'str' object has no attribute 'removeprefix'#42513

Closed
sergiitk wants to merge 3 commits into
grpc:masterfrom
sergiitk:fix/ci/cmake-old-python-removeprefix
Closed

[CI][Fix] cmake: fix 'str' object has no attribute 'removeprefix'#42513
sergiitk wants to merge 3 commits into
grpc:masterfrom
sergiitk:fix/ci/cmake-old-python-removeprefix

Conversation

@sergiitk

Copy link
Copy Markdown
Member

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 #42172 - but it's a CI tech debt issue. Normally we should be assuming that we run a modern python.

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.
@sergiitk sergiitk requested a review from murgatroid99 May 28, 2026 00:24
@sergiitk sergiitk added the release notes: no Indicates if PR should not be in release notes label May 28, 2026
@sergiitk sergiitk self-assigned this May 28, 2026
@sergiitk

Copy link
Copy Markdown
Member Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread templates/CMakeLists.txt.template Outdated
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}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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):]

@sergiitk sergiitk May 28, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@sergiitk

Copy link
Copy Markdown
Member Author

Without this change grpc/core/master/linux/grpc_build_abseil-cpp_at_head is failing 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 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 grpc/core/master/linux/grpc_build_abseil-cpp_at_head to verify the fix:
https://source.cloud.google.com/results/invocations/7da888f1-583d-4edd-82ac-511bb3835f8f

@sergiitk

sergiitk commented May 28, 2026

Copy link
Copy Markdown
Member Author

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 removeprefix usage

@sergiitk

Copy link
Copy Markdown
Member Author

@sergiitk

Copy link
Copy Markdown
Member Author

Adhoc failed (expected), but the build advanced past the python syntax error.

@sergiitk sergiitk deleted the fix/ci/cmake-old-python-removeprefix branch May 29, 2026 04:36
asheshvidyut pushed a commit to asheshvidyut/grpc that referenced this pull request May 29, 2026
…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
asheshvidyut pushed a commit to a-detiste/grpc that referenced this pull request Jun 10, 2026
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release notes: no Indicates if PR should not be in release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants