Skip to content

Comments

Reuse build (virtual) environments across resolution and installation#14338

Merged
charliermarsh merged 2 commits intomainfrom
charlie/shared-builds
Jul 1, 2025
Merged

Reuse build (virtual) environments across resolution and installation#14338
charliermarsh merged 2 commits intomainfrom
charlie/shared-builds

Conversation

@charliermarsh
Copy link
Member

@charliermarsh charliermarsh commented Jun 27, 2025

Summary

The basic idea here is that we can (should) reuse a build environment across resolution (prepare_metadata_for_build_wheel) and installation. This also happens to solve the build-PyTorch-from-source problem, since we use a consistent build environment between the invocations.

Since SourceDistributionBuilder is stateless, we instead store the builds on BuildContext, and we key them by various properties: the underlying interpreter, the configuration settings, etc. This just ensures that if we build the same package twice within a process, we don't accidentally reuse an incompatible build (virtual) environment. (Note that still drop build environments at the end of the command, and don't attempt to reuse them across processes.)

Closes #14269.

@charliermarsh charliermarsh force-pushed the charlie/shared-builds branch from b4c2b4b to de2023c Compare June 27, 2025 23:43
@charliermarsh charliermarsh marked this pull request as ready for review June 27, 2025 23:46
@charliermarsh charliermarsh temporarily deployed to uv-test-registries June 27, 2025 23:46 — with GitHub Actions Inactive
@charliermarsh charliermarsh requested review from konstin and zanieb June 27, 2025 23:50
@charliermarsh charliermarsh added the enhancement New feature or improvement to existing functionality label Jun 27, 2025
@charliermarsh charliermarsh marked this pull request as draft June 27, 2025 23:51
@charliermarsh
Copy link
Member Author

(Converting to draft while I review test failures.)

@charliermarsh charliermarsh temporarily deployed to uv-test-registries June 28, 2025 00:16 — with GitHub Actions Inactive
@charliermarsh charliermarsh force-pushed the charlie/shared-builds branch from f1b6aaf to 490cebf Compare June 28, 2025 00:22
@charliermarsh charliermarsh temporarily deployed to uv-test-registries June 28, 2025 00:25 — with GitHub Actions Inactive
@charliermarsh
Copy link
Member Author

There's a deadlock somewhere related to --no-binary but having a lot of trouble figuring out where.

@charliermarsh charliermarsh temporarily deployed to uv-test-registries June 28, 2025 00:47 — with GitHub Actions Inactive
@charliermarsh charliermarsh force-pushed the charlie/shared-builds branch 3 times, most recently from c0ee25c to 27030b6 Compare June 28, 2025 01:17
@charliermarsh charliermarsh temporarily deployed to uv-test-registries June 28, 2025 01:19 — with GitHub Actions Inactive
@charliermarsh charliermarsh marked this pull request as ready for review June 28, 2025 01:23
@charliermarsh
Copy link
Member Author

Okay, tests passing.


if let Some(builder) = self.build_context.build_arena().remove(&build_key) {
debug!("Creating build environment for: {source}");
builder.wheel(temp_dir.path()).await.map_err(Error::Build)?
Copy link
Member

Choose a reason for hiding this comment

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

After we removed it, should we re-add in case we need it again? We could have prepare_metadata_for_build_wheel, then get_requires_for_build_wheel and then build_wheel, which would be three steps.

(I want to keep the removing over borrowing/sharing as it safeguards against accidental parallelism)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah this makes sense. (I didn't want to borrow for the same reason.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@charliermarsh charliermarsh force-pushed the charlie/shared-builds branch from e4f4e13 to 67db2fd Compare July 1, 2025 14:01
Comment on lines 2330 to 2335
let wheel = builder.wheel(temp_dir.path()).await.map_err(Error::Build)?;

// Store the build context.
self.build_context
.build_arena()
.insert(build_key, builder);

wheel
Copy link
Member

Choose a reason for hiding this comment

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

nit: we can move the duplicated code below the if let.

Copy link
Member Author

Choose a reason for hiding this comment

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

Which code?

@charliermarsh charliermarsh force-pushed the charlie/shared-builds branch from 1c1e91e to 8c5f5ef Compare July 1, 2025 16:50
@charliermarsh charliermarsh temporarily deployed to uv-test-registries July 1, 2025 16:52 — with GitHub Actions Inactive
@charliermarsh charliermarsh merged commit d9f9ed4 into main Jul 1, 2025
136 checks passed
@charliermarsh charliermarsh deleted the charlie/shared-builds branch July 1, 2025 17:15
tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request Jul 6, 2025
This MR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [astral-sh/uv](https://github.com/astral-sh/uv) | patch | `0.7.16` -> `0.7.19` |

MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).

**Proposed changes to behavior should be submitted there as MRs.**

---

### Release Notes

<details>
<summary>astral-sh/uv (astral-sh/uv)</summary>

### [`v0.7.19`](https://github.com/astral-sh/uv/blob/HEAD/CHANGELOG.md#0719)

[Compare Source](astral-sh/uv@0.7.18...0.7.19)

The **[uv build backend](https://docs.astral.sh/uv/concepts/build-backend/) is now stable**, and considered ready for production use.

The uv build backend is a great choice for pure Python projects. It has reasonable defaults, with the goal of requiring zero configuration for most users, but provides flexible configuration to accommodate most Python project structures. It integrates tightly with uv, to improve messaging and user experience. It validates project metadata and structures, preventing common mistakes. And, finally, it's very fast — `uv sync` on a new project (from `uv init`) is 10-30x faster than with other build backends.

To use uv as a build backend in an existing project, add `uv_build` to the `[build-system]` section in your `pyproject.toml`:

```toml
[build-system]
requires = ["uv_build>=0.7.19,<0.8.0"]
build-backend = "uv_build"
```

In a future release, it will replace `hatchling` as the default in `uv init`. As before, uv will remain compatible with all standards-compliant build backends.

##### Python

- Add PGO distributions of Python for aarch64 Linux, which are more optimized for better performance

See the [python-build-standalone release](https://github.com/astral-sh/python-build-standalone/releases/tag/20250702) for more details.

##### Enhancements

- Ignore Python patch version for `--universal` pip compile ([#&#8203;14405](astral-sh/uv#14405))
- Update the tilde version specifier warning to include more context ([#&#8203;14335](astral-sh/uv#14335))
- Clarify behavior and hint on tool install when no executables are available ([#&#8203;14423](astral-sh/uv#14423))

##### Bug fixes

- Make project and interpreter lock acquisition non-fatal ([#&#8203;14404](astral-sh/uv#14404))
- Includes `sys.prefix` in cached environment keys to avoid `--with` collisions across projects ([#&#8203;14403](astral-sh/uv#14403))

##### Documentation

- Add a migration guide from pip to uv projects ([#&#8203;12382](astral-sh/uv#12382))

### [`v0.7.18`](https://github.com/astral-sh/uv/blob/HEAD/CHANGELOG.md#0718)

[Compare Source](astral-sh/uv@0.7.17...0.7.18)

##### Python

- Added arm64 Windows Python 3.11, 3.12, 3.13, and 3.14

  These are not downloaded by default, since x86-64 Python has broader ecosystem support on Windows.
  However, they can be requested with `cpython-<version>-windows-aarch64`.

See the [python-build-standalone release](https://github.com/astral-sh/python-build-standalone/releases/tag/20250630) for more details.

##### Enhancements

- Keep track of retries in `ManagedPythonDownload::fetch_with_retry` ([#&#8203;14378](astral-sh/uv#14378))
- Reuse build (virtual) environments across resolution and installation ([#&#8203;14338](astral-sh/uv#14338))
- Improve trace message for cached Python interpreter query ([#&#8203;14328](astral-sh/uv#14328))
- Use parsed URLs for conflicting URL error message ([#&#8203;14380](astral-sh/uv#14380))

##### Preview features

- Ignore invalid build backend settings when not building ([#&#8203;14372](astral-sh/uv#14372))

##### Bug fixes

- Fix equals-star and tilde-equals with `python_version` and `python_full_version` ([#&#8203;14271](astral-sh/uv#14271))
- Include the canonical path in the interpreter query cache key ([#&#8203;14331](astral-sh/uv#14331))
- Only drop build directories on program exit ([#&#8203;14304](astral-sh/uv#14304))
- Error instead of panic on conflict between global and subcommand flags ([#&#8203;14368](astral-sh/uv#14368))
- Consistently normalize trailing slashes on URLs with no path segments ([#&#8203;14349](astral-sh/uv#14349))

##### Documentation

- Add instructions for publishing to JFrog's Artifactory ([#&#8203;14253](astral-sh/uv#14253))
- Edits to the build backend documentation ([#&#8203;14376](astral-sh/uv#14376))

### [`v0.7.17`](https://github.com/astral-sh/uv/blob/HEAD/CHANGELOG.md#0717)

[Compare Source](astral-sh/uv@0.7.16...0.7.17)

##### Bug fixes

- Apply build constraints when resolving `--with` dependencies ([#&#8203;14340](astral-sh/uv#14340))
- Drop trailing slashes when converting index URL from URL ([#&#8203;14346](astral-sh/uv#14346))
- Ignore `UV_PYTHON_CACHE_DIR` when empty ([#&#8203;14336](astral-sh/uv#14336))
- Fix error message ordering for `pyvenv.cfg` version conflict ([#&#8203;14329](astral-sh/uv#14329))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this MR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MC42Mi4xIiwidXBkYXRlZEluVmVyIjoiNDAuNjIuMSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiUmVub3ZhdGUgQm90Il19-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or improvement to existing functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

In-tree build uv pip install . will create PEP-517 venv twice which breaks build-requires (e.g., cmake / ninja with absolute path in first venv)

2 participants