Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix direct_url in python PEP660 editable wheels #20486

Merged
merged 4 commits into from
Feb 6, 2024

Conversation

cognifloyd
Copy link
Member

We were using the first entry in build_time_source_roots to populate the editable wheel's direct_url. However, that does not work as I expected it to because build_time_source_roots gets sorted before creating the DistBuildRequest, so the first entry is not actually the source_root of the python_distribution target.

To fix this, we capture the dist's source_root before the source_roots get sorted and use that when building the PEP660 editable wheel.

Functionally, the editable wheels get installed just fine. However, the output of pip list in an exported venv might show the wrong path if there happens to be a source_root that sorted before the source_root of the distribution. Importing the python code still works, however, as all of the source_roots are included in the .pth file. Thus, this is a cosmetic / correctness bug.

This is the relevant section of the pip list output for the StackStorm project so you can see how odd it looks to have the wrong path showing:

dist/export/python/virtualenvs/st2/3.8.18/bin/pip list
Package                        Version    Editable project location
------------------------------ ---------- -------------------------------------------------------------------------
[snip]
st2actions                     3.9.dev0   /home/cognifloyd/p/st2sandbox/st2.git/st2actions
st2api                         3.9.dev0   /home/cognifloyd/p/st2sandbox/st2.git/st2actions
st2auth                        3.9.dev0   /home/cognifloyd/p/st2sandbox/st2.git/st2auth
st2client                      3.9.dev0   /home/cognifloyd/p/st2sandbox/st2.git/st2client
st2common                      3.9.dev0   /home/cognifloyd/p/st2sandbox/st2.git/st2actions
st2reactor                     3.9.dev0   /home/cognifloyd/p/st2sandbox/st2.git/st2client
st2stream                      3.9.dev0   /home/cognifloyd/p/st2sandbox/st2.git/st2api
st2tests                       3.9.dev0   /home/cognifloyd/p/st2sandbox/st2.git/contrib/runners/python_runner
stackstorm-runner-action-chain 3.9.dev0   /home/cognifloyd/p/st2sandbox/st2.git/contrib/runners/action_chain_runner
stackstorm-runner-announcement 3.9.dev0   /home/cognifloyd/p/st2sandbox/st2.git/contrib/runners/announcement_runner
stackstorm-runner-http         3.9.dev0   /home/cognifloyd/p/st2sandbox/st2.git/contrib/runners/http_runner
stackstorm-runner-inquirer     3.9.dev0   /home/cognifloyd/p/st2sandbox/st2.git/contrib/runners/inquirer_runner
stackstorm-runner-local        3.9.dev0   /home/cognifloyd/p/st2sandbox/st2.git/contrib/runners/local_runner
stackstorm-runner-noop         3.9.dev0   /home/cognifloyd/p/st2sandbox/st2.git/contrib/runners/noop_runner
stackstorm-runner-orquesta     3.9.dev0   /home/cognifloyd/p/st2sandbox/st2.git/contrib/runners/orquesta_runner
stackstorm-runner-python       3.9.dev0   /home/cognifloyd/p/st2sandbox/st2.git/contrib/runners/python_runner
stackstorm-runner-remote       3.9.dev0   /home/cognifloyd/p/st2sandbox/st2.git/contrib/runners/remote_runner
stackstorm-runner-winrm        3.9.dev0   /home/cognifloyd/p/st2sandbox/st2.git/contrib/runners/winrm_runner
[snip]

We were using the first entry in `build_time_source_roots` to populate
the editable wheel's `direct_url`. However, that does not work as I
expected it to because `build_time_source_roots` gets sorted before
creating the `DistBuildRequest`, so the first entry is not actually
the `source_root` of the `python_distribution` target.

To fix this, we capture the dist's source_root before the source_roots
get sorted and use that when building the PEP660 editable wheel.
@cognifloyd cognifloyd added needs-cherrypick category:bugfix Bug fixes for released features backend: Python Python backend-related issues labels Feb 4, 2024
@cognifloyd cognifloyd added this to the 2.19.x milestone Feb 4, 2024
@cognifloyd cognifloyd requested a review from benjyw February 4, 2024 06:16
@cognifloyd cognifloyd self-assigned this Feb 4, 2024
@cognifloyd cognifloyd enabled auto-merge (squash) February 6, 2024 03:10
@cognifloyd cognifloyd merged commit 2cef051 into main Feb 6, 2024
24 checks passed
@cognifloyd cognifloyd deleted the cognifloyd/fix_pep660_direct_url branch February 6, 2024 03:47
WorkerPants pushed a commit that referenced this pull request Feb 6, 2024
We were using the first entry in `build_time_source_roots` to populate
the editable wheel's `direct_url`. However, that does not work as I
expected it to because `build_time_source_roots` gets sorted before
creating the `DistBuildRequest`, so the first entry is not actually the
`source_root` of the `python_distribution` target.

To fix this, we capture the dist's source_root before the source_roots
get sorted and use that when building the PEP660 editable wheel.

Functionally, the editable wheels get installed just fine. However, the
output of `pip list` in an exported venv might show the wrong path if
there happens to be a source_root that sorted before the source_root of
the distribution. Importing the python code still works, however, as all
of the source_roots are included in the `.pth` file. Thus, this is a
cosmetic / correctness bug.
@WorkerPants
Copy link
Member

I tried to automatically cherry-pick this change back to each relevant milestone, so that it is available in those older releases of Pants.

✔️ 2.19.x

Successfully opened #20494.


Thanks again for your contributions!

🤖 Beep Boop here's my run link

cognifloyd added a commit that referenced this pull request Feb 6, 2024
… (#20494)

We were using the first entry in `build_time_source_roots` to populate
the editable wheel's `direct_url`. However, that does not work as I
expected it to because `build_time_source_roots` gets sorted before
creating the `DistBuildRequest`, so the first entry is not actually the
`source_root` of the `python_distribution` target.

To fix this, we capture the dist's source_root before the source_roots
get sorted and use that when building the PEP660 editable wheel.

Functionally, the editable wheels get installed just fine. However, the
output of `pip list` in an exported venv might show the wrong path if
there happens to be a source_root that sorted before the source_root of
the distribution. Importing the python code still works, however, as all
of the source_roots are included in the `.pth` file. Thus, this is a
cosmetic / correctness bug.

Co-authored-by: Jacob Floyd <[email protected]>
@cognifloyd cognifloyd modified the milestones: 2.19.x, 2.18.x Feb 24, 2024
WorkerPants pushed a commit that referenced this pull request Feb 24, 2024
We were using the first entry in `build_time_source_roots` to populate
the editable wheel's `direct_url`. However, that does not work as I
expected it to because `build_time_source_roots` gets sorted before
creating the `DistBuildRequest`, so the first entry is not actually the
`source_root` of the `python_distribution` target.

To fix this, we capture the dist's source_root before the source_roots
get sorted and use that when building the PEP660 editable wheel.

Functionally, the editable wheels get installed just fine. However, the
output of `pip list` in an exported venv might show the wrong path if
there happens to be a source_root that sorted before the source_root of
the distribution. Importing the python code still works, however, as all
of the source_roots are included in the `.pth` file. Thus, this is a
cosmetic / correctness bug.
@WorkerPants
Copy link
Member

I tried to automatically cherry-pick this change back to each relevant milestone, so that it is available in those older releases of Pants.

✔️ 2.18.x

Successfully opened #20602.

❌ 2.19.x

I was unable to cherry-pick this PR to 2.19.x, likely due to merge-conflicts.

Steps to Cherry-Pick locally

To resolve:

  1. (Ensure your git working directory is clean)
  2. Run the following script to reproduce the merge-conflicts:
    git fetch https://github.com/pantsbuild/pants main \
      && git fetch https://github.com/pantsbuild/pants 2.19.x \
      && git checkout -b cherry-pick-20486-to-2.19.x FETCH_HEAD \
      && git cherry-pick 2cef0511d756a7a0f0beb157550fcdf47e5c6778
  3. Fix the merge conflicts and commit the changes
  4. Run build-support/cherry_pick/make_pr.sh "20486" "2.19.x"

Please note that I cannot re-run CI if a job fails. Please work with your PR approver(s) to re-run CI if necessary.

❌ 2.20.x

I was unable to cherry-pick this PR to 2.20.x, likely due to merge-conflicts.

Steps to Cherry-Pick locally

To resolve:

  1. (Ensure your git working directory is clean)
  2. Run the following script to reproduce the merge-conflicts:
    git fetch https://github.com/pantsbuild/pants main \
      && git fetch https://github.com/pantsbuild/pants 2.20.x \
      && git checkout -b cherry-pick-20486-to-2.20.x FETCH_HEAD \
      && git cherry-pick 2cef0511d756a7a0f0beb157550fcdf47e5c6778
  3. Fix the merge conflicts and commit the changes
  4. Run build-support/cherry_pick/make_pr.sh "20486" "2.20.x"

Please note that I cannot re-run CI if a job fails. Please work with your PR approver(s) to re-run CI if necessary.


When you're done manually cherry-picking, please remove the needs-cherrypick label on this PR.

Thanks again for your contributions!

🤖 Beep Boop here's my run link

@WorkerPants WorkerPants added the auto-cherry-picking-failed Auto Cherry-Picking Failed label Feb 24, 2024
cognifloyd added a commit that referenced this pull request Feb 24, 2024
… (#20602)

We were using the first entry in `build_time_source_roots` to populate
the editable wheel's `direct_url`. However, that does not work as I
expected it to because `build_time_source_roots` gets sorted before
creating the `DistBuildRequest`, so the first entry is not actually the
`source_root` of the `python_distribution` target.

To fix this, we capture the dist's source_root before the source_roots
get sorted and use that when building the PEP660 editable wheel.

Functionally, the editable wheels get installed just fine. However, the
output of `pip list` in an exported venv might show the wrong path if
there happens to be a source_root that sorted before the source_root of
the distribution. Importing the python code still works, however, as all
of the source_roots are included in the `.pth` file. Thus, this is a
cosmetic / correctness bug.

Co-authored-by: Jacob Floyd <[email protected]>
@cognifloyd
Copy link
Member Author

Dang. This was not a complete fix for this issue. I'll have to push a follow-up PR to finish fixing it.

cognifloyd added a commit that referenced this pull request Apr 16, 2024
We were using the first entry in `source_roots_result.path_to_root` to
populate the editable wheel's `direct_url`. However, that does not work
as I expected it to because `SourceRootsRequest.dirs` and
`SourceRootsResult.path_to_root` both get sorted as soon as the
dataclass is created (if not before).

PR #20486 was my first attempt at fixing this, but that wasn't enough.
Since the heuristics used before #20486 and in #20486 were both
inaccurate due to dataclasses getting sorted, this abandons the
heuristics altogether, explicitly requesting the source root for the
`python_distribution` target.
cognifloyd added a commit that referenced this pull request Apr 18, 2024
We were using the first entry in `source_roots_result.path_to_root` to
populate the editable wheel's `direct_url`. However, that does not work
as I expected it to because `SourceRootsRequest.dirs` and
`SourceRootsResult.path_to_root` both get sorted as soon as the
dataclass is created (if not before).

PR #20486 was my first attempt at fixing this, but that wasn't enough.
Since the heuristics used before #20486 and in #20486 were both
inaccurate due to dataclasses getting sorted, this abandons the
heuristics altogether, explicitly requesting the source root for the
`python_distribution` target.

This time, I added a test to make sure direct_url.json has the contents
we expect.

Note: This is ultimately a cosmetic bug. The direct_url gets printed in
the output of `pip list`, but is otherwise not really exposed anywhere.
So, it's odd when it's the wrong directory, but does not break any
functionality.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-cherry-picking-failed Auto Cherry-Picking Failed backend: Python Python backend-related issues category:bugfix Bug fixes for released features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants