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

Add internal options for managing toolchain discovery preferences #4416

Merged
merged 1 commit into from
Jun 20, 2024

Conversation

zanieb
Copy link
Member

@zanieb zanieb commented Jun 19, 2024

Adds support for the toolchain discovery preferences outlined in #4198 but we don't expose this to users yet, I'll do that next to make it easier to review.

I've made some refactors in the toolchain discovery implementation to enable this behavior and move us towards clearer abstractions. There's still remaining work here, but I'd prefer tackle things in follow-ups instead of expanding this pull request. I plan on opening a couple before merging this.

I'd like to shift the public toolchain API to focus on discovering either an environment or a toolchain. The first would be used by commands that operate on an environment, while the latter would be used by commands that just need an interpreter to create environments. I haven't changed this here, but some of the refactors are in preparation for supporting this idea.

In brief:

  • We now allow different ordering of installed toolchain discovery based on a ToolchainPreference type. This is the type we will expose to users.
  • SystemPython was changed into an EnvironmentPreference which is used to determine if we should prefer virtual or system Python environments.
  • We drop the whole ToolchainSources selection concept, it was confusing and the error messages from it were awkward. Most of the functionality is now captured by the preference enums, but you can't do things like "only find a toolchain from the parent interpreter" as easily anymore.

@zanieb zanieb added the internal A refactor or improvement that is not user-facing label Jun 19, 2024
@zanieb zanieb changed the title Add setting for managing toolchain discovery preferences Add internal options for managing toolchain discovery preferences Jun 19, 2024
@zanieb zanieb marked this pull request as draft June 19, 2024 16:56
× No interpreter found for Python 3.100 in search path
× No interpreter found for Python 3.100 in system toolchains
Copy link
Member Author

Choose a reason for hiding this comment

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

This is less clear but will address separately.

Comment on lines 114 to 120
success: false
exit_code: 2
success: true
exit_code: 0
----- stdout -----

----- stderr -----
error: No Python interpreters found in virtual environments
Resolved 1 package in [TIME]
Audited 1 package in [TIME]
Copy link
Member Author

Choose a reason for hiding this comment

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

Interesting regression... will investigate this.

Copy link
Member Author

Choose a reason for hiding this comment

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

These tests are not properly isolated from managed toolchains because they aren't following our TestContext pattern :sigh:

Comment on lines -50 to +49
if let Some(request) = python {
Self::find_requested(&request, system, preview, cache)
} else if system.is_preferred() {
Self::find_default(preview, cache)
} else {
// First check for a parent interpreter
// We gate this check to avoid an extra log message when it is not set
if std::env::var_os("UV_INTERNAL__PARENT_INTERPRETER").is_some() {
match Self::find_parent_interpreter(system, cache) {
Ok(env) => return Ok(env),
Err(Error::NotFound(_)) => {}
Err(err) => return Err(err),
}
}

// Then a virtual environment
match Self::find_virtualenv(cache) {
Ok(venv) => Ok(venv),
Err(Error::NotFound(_)) if system.is_allowed() => {
Self::find_default(preview, cache)
}
Err(err) => Err(err),
}
}
let request = python.unwrap_or_default();
Self::find_requested(&request, environments, preference, cache)
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a big plus. We don't need to do multiple discovery attempts to get a different ordering. We just say what we want.

Copy link
Member

Choose a reason for hiding this comment

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

I like it.

Comment on lines +48 to +49
.env("UV_TEST_PYTHON_PATH", &context.python_path())
.env("UV_TOOLCHAIN_DIR", "")
Copy link
Member Author

Choose a reason for hiding this comment

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

Addresses #4416 (comment)

@zanieb zanieb marked this pull request as ready for review June 19, 2024 17:35
Base automatically changed from zb/discover-chain to main June 19, 2024 20:27
zanieb added a commit that referenced this pull request Jun 19, 2024
In preparation for changing the order dynamically in #4416
Copy link
Member

@BurntSushi BurntSushi left a comment

Choose a reason for hiding this comment

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

Nice!

Was this the refactor you were talking about in DMs? I know you mentioned it being exploratory, but one thing that I find can help here is to put things like type/function renames into one commit separate from the other changes. The renames can touch a lot of code and add a lot of noise to the diff. When split out, the "more interesting" changes can be seen in a different commit.

(I know this isn't always feasible, especially in a more exploratory refactor. But I thought it might be valuable to throw it out there.)

Comment on lines -50 to +49
if let Some(request) = python {
Self::find_requested(&request, system, preview, cache)
} else if system.is_preferred() {
Self::find_default(preview, cache)
} else {
// First check for a parent interpreter
// We gate this check to avoid an extra log message when it is not set
if std::env::var_os("UV_INTERNAL__PARENT_INTERPRETER").is_some() {
match Self::find_parent_interpreter(system, cache) {
Ok(env) => return Ok(env),
Err(Error::NotFound(_)) => {}
Err(err) => return Err(err),
}
}

// Then a virtual environment
match Self::find_virtualenv(cache) {
Ok(venv) => Ok(venv),
Err(Error::NotFound(_)) if system.is_allowed() => {
Self::find_default(preview, cache)
}
Err(err) => Err(err),
}
}
let request = python.unwrap_or_default();
Self::find_requested(&request, environments, preference, cache)
Copy link
Member

Choose a reason for hiding this comment

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

I like it.

@zanieb
Copy link
Member Author

zanieb commented Jun 20, 2024

Yeah this is a part of what I was mentioning. A lot of the exploration is figuring out how to add this setting while moving the API towards something that's easy to use correctly. I generally do try to split out renames, but yeah I didn't do a great job of that here :)

@zanieb zanieb merged commit 13e532c into main Jun 20, 2024
47 checks passed
@zanieb zanieb deleted the zb/toolchain-preference branch June 20, 2024 13:57
zanieb added a commit that referenced this pull request Jun 20, 2024
Restores the `PythonEnvironment::find` API which was removed a while
back in favor of `Toolchain::find`. As mentioned in #4416, I'm
attempting to separate the case where you want an active environment
from the case where you want an installed toolchain in order to create
environments.

I wanted to drop `EnvironmentPreference` from `Toolchain::find` and just
have us consistently consider (or not consider) virtual environments
when discovering toolchains for creating environments. Unfortunately
this caused a few things to break so I reverted that change and will
explore it separately. Because I was exploring that change, there are
some minor changes to the `Toolchain` API here.
zanieb added a commit that referenced this pull request Jun 20, 2024
…4424)

Exposes the option added in #4416. Adds `--toolchain-preference` and
`tool.uv.toolchain-preference` to configure if system or managed
toolchains are preferred. Users can opt-out of managed toolchains or
system toolchains entirely as well.
ChannyClaus pushed a commit to ChannyClaus/uv that referenced this pull request Jun 20, 2024
ChannyClaus pushed a commit to ChannyClaus/uv that referenced this pull request Jun 20, 2024
…tral-sh#4416)

Adds support for the toolchain discovery preferences outlined in
astral-sh#4198 but we don't expose this to
users yet, I'll do that next to make it easier to review.

I've made some refactors in the toolchain discovery implementation to
enable this behavior and move us towards clearer abstractions. There's
still remaining work here, but I'd prefer tackle things in follow-ups
instead of expanding this pull request. I plan on opening a couple
before merging this.

I'd like to shift the public toolchain API to focus on discovering
either an **environment** or a **toolchain**. The first would be used by
commands that operate on an environment, while the latter would be used
by commands that just need an interpreter to create environments. I
haven't changed this here, but some of the refactors are in preparation
for supporting this idea.

In brief:

- We now allow different ordering of installed toolchain discovery based
on a `ToolchainPreference` type. This is the type we will expose to
users.
- `SystemPython` was changed into an `EnvironmentPreference` which is
used to determine if we should prefer virtual or system Python
environments.
- We drop the whole `ToolchainSources` selection concept, it was
confusing and the error messages from it were awkward. Most of the
functionality is now captured by the preference enums, but you can't do
things like "only find a toolchain from the parent interpreter" as
easily anymore.
ChannyClaus pushed a commit to ChannyClaus/uv that referenced this pull request Jun 20, 2024
Restores the `PythonEnvironment::find` API which was removed a while
back in favor of `Toolchain::find`. As mentioned in astral-sh#4416, I'm
attempting to separate the case where you want an active environment
from the case where you want an installed toolchain in order to create
environments.

I wanted to drop `EnvironmentPreference` from `Toolchain::find` and just
have us consistently consider (or not consider) virtual environments
when discovering toolchains for creating environments. Unfortunately
this caused a few things to break so I reverted that change and will
explore it separately. Because I was exploring that change, there are
some minor changes to the `Toolchain` API here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
internal A refactor or improvement that is not user-facing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants