Skip to content

Fix Python instrumentation to handle from __future__ imports#3170

Open
Claude wants to merge 2 commits intomainfrom
claude/fix-setup-gcloud-error
Open

Fix Python instrumentation to handle from __future__ imports#3170
Claude wants to merge 2 commits intomainfrom
claude/fix-setup-gcloud-error

Conversation

@Claude
Copy link
Contributor

@Claude Claude AI commented Mar 13, 2026

The Python instrumentation was prepending __file__ assignment at the start of executed files, violating Python's requirement that from __future__ imports must be first. This caused google-github-actions/setup-gcloud to fail with SyntaxError: from __future__ imports must occur at the beginning of the file.

Changes

  • Modified agent.instrumentation.python.sh: Scan file content for from __future__ imports and insert __file__ assignment after the last one (if any exist), otherwise use original prepend behavior
  • Added test case in test_auto_injection_python.sh for files with from __future__ imports

Implementation

The fix scans all lines to find the last from __future__ import statement and inserts __file__ immediately after:

# Before (incorrect):
exec('__file__="/path/to/file"\n' + file.read())

# After (correct):
lines = content.split('\n')
last_future_idx = -1
for i, line in enumerate(lines):
  if line.strip().startswith('from __future__ import'):
    last_future_idx = i
if last_future_idx >= 0:
  lines.insert(last_future_idx + 1, '__file__="/path/to/file"')
  exec('\n'.join(lines))
else:
  exec('__file__="/path/to/file"\n' + content)

This maintains backward compatibility with files that don't use from __future__ imports while fixing the gcloud.py case.

Original prompt

This section details on the original issue you should resolve

<issue_title>Another error with google-github-actions/setup-gcloud</issue_title>
<issue_description>Finally had a chance to test 5.46.0, which still generates an error with gcloud but adifferent one:

Run google-github-actions/setup-gcloud@v3
/usr/bin/tar xz --warning=no-unknown-keyword --overwrite -C /home/runner/work/_temp/af9b9d15-5bea-462b-93b2-8876276ea1f7 -f /home/runner/work/_temp/1edb1bcc-c8f6-4b63-9c5f-9bcf5e8a842e
Error: google-github-actions/setup-gcloud failed with: failed to execute command `gcloud --quiet auth login --force --cred-file /home/runner/work/<repo>/<repo>/gha-creds-513dd5e9ec25fccc.json`: Traceback (most recent call last):
  File "<string>", line 104, in <module>
    exec('__file__="/home/runner/work/_temp/af9b9d15-5bea-462b-93b2-8876276ea1f7/google-cloud-sdk/lib/gcloud.py"\n' + file.read())
    ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 21
Syntaxfrom __future__ imports must occur at the beginning of the file

Much appreciated as always for looking into these.</issue_description>

Comments on the Issue (you are @claude[agent] in this section)

@plengauer hmmm, i fixed one problem by setting the file variable manually, but i probably prepended a few imports that way ...

if my theory is correct, that should be easily fixable. unforrtuantely, again i cannot test directly, i can only try to reproduce the same error in custom test scripts because i dont have access to authentication for these services</comment_new>
<comment_new>@plengauer
https://github.com/jphalip/google-cloud-sdk/blob/master/lib/gcloud.py

the __file__ is set in line 0, so destroying the shebang as well as the imports in the process. shebang is not an issue, because we are already loaded and shebang is effectively just a comment now. </comment_new>
<comment_new>@plengauer
a quick manual test with a small python test shows this works...

the file at the link above shows no imports of style from __future__. am i looking at the wrong one?</comment_new>
<comment_new>@plengauer
this one looks more like it: https://github.com/google-cloud-sdk-unofficial/google-cloud-sdk/blob/master/lib/gcloud.py</comment_new>
<comment_new>@plengauer
now it reproduces:

  File "/home/ubuntu/Sandbox/tmp/gcloud.py", line 21
    from __future__ import absolute_import
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: from __future__ imports must occur at the beginning of the file

```</body></comment_new>
<comment_new><author>@plengauer</author><body>
fix: we have to adjust how the `__file__` variable is set in the file `agent.instrumentation.python.sh`. if the file contains `from __future__` we have to add the assignment of `__file__` after the last such import. if there is none, we can just prepend it. This should be done dynamically, without modyfing the file on disk just like now.</body></comment_new>
</comments>

…_ imports

Co-authored-by: plengauer <100447901+plengauer@users.noreply.github.com>
@Claude Claude AI changed the title [WIP] Fix another error with google-github-actions/setup-gcloud Fix Python instrumentation to handle from __future__ imports Mar 13, 2026
@Claude Claude AI requested a review from plengauer March 13, 2026 10:14
@plengauer plengauer marked this pull request as ready for review March 13, 2026 10:19
@plengauer plengauer requested a review from moflwi as a code owner March 13, 2026 10:19
Copilot AI review requested due to automatic review settings March 13, 2026 10:19
@plengauer plengauer enabled auto-merge (squash) March 13, 2026 10:19
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the Python auto-instrumentation wrapper to avoid breaking scripts that use from __future__ imports by inserting the injected __file__ assignment after the last future import, and adds a regression test for this scenario.

Changes:

  • Update Python file execution wrapper to detect from __future__ imports and insert __file__ after them.
  • Add an auto test case covering execution of a Python file containing from __future__ imports.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/usr/share/opentelemetry_shell/agent.instrumentation.python.sh Adjusts how Python file content is wrapped/exec’d so __file__ isn’t prepended before from __future__ imports.
tests/auto/test_auto_injection_python.sh Adds a regression test intended to catch the from __future__ ordering SyntaxError in instrumented execution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Another error with google-github-actions/setup-gcloud

3 participants