Skip to content

Commit

Permalink
Print environment
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Aug 7, 2024
1 parent f3cc8e4 commit 42cba53
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 18 deletions.
1 change: 1 addition & 0 deletions crates/uv/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,4 @@ pub(crate) struct SharedState {
/// The downloaded distributions.
pub(crate) in_flight: InFlight,
}

7 changes: 3 additions & 4 deletions crates/uv/src/commands/project/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,17 +259,13 @@ pub(crate) async fn add(
.with_pyproject_toml(pyproject.to_toml()?)
.context("Failed to update `pyproject.toml`")?;

// Initialize any shared state.
let state = SharedState::default();

// Lock and sync the environment, if necessary.
let lock = match project::lock::do_safe_lock(
locked,
frozen,
project.workspace(),
venv.interpreter(),
settings.as_ref().into(),
&state,
preview,
connectivity,
concurrency,
Expand Down Expand Up @@ -387,6 +383,9 @@ pub(crate) async fn add(
}
};

// Initialize any shared state.
let state = SharedState::default();

project::sync::do_sync(
&project,
&venv,
Expand Down
17 changes: 13 additions & 4 deletions crates/uv/src/commands/project/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ pub(crate) async fn lock(
&workspace,
&interpreter,
settings.as_ref(),
&SharedState::default(),
preview,
connectivity,
concurrency,
Expand Down Expand Up @@ -118,14 +117,24 @@ pub(super) async fn do_safe_lock(
workspace: &Workspace,
interpreter: &Interpreter,
settings: ResolverSettingsRef<'_>,
state: &SharedState,
preview: PreviewMode,
connectivity: Connectivity,
concurrency: Concurrency,
native_tls: bool,
cache: &Cache,
printer: Printer,
) -> Result<LockResult, ProjectError> {
// Use isolate state for universal resolution. When resolving, we don't enforce that the
// prioritized distributions match the current platform. So if we lock here, then try to
// install from the same state, and we end up performing a resolution during the sync (i.e.,
// for the build dependencies of a source distribution), we may try to use incompatible
// distributions.
// TODO(charlie): In universal resolution, we should still track version compatibility! We
// just need to accept versions that are platform-incompatible. That would also make us more
// likely to (e.g.) download a wheel that we'll end up using when installing. This would
// make it safe to share the state.
let state = SharedState::default();

if frozen {
// Read the existing lockfile, but don't attempt to lock the project.
let existing = read(workspace)
Expand All @@ -147,7 +156,7 @@ pub(super) async fn do_safe_lock(
interpreter,
Some(&existing),
settings,
state,
&state,
preview,
connectivity,
concurrency,
Expand Down Expand Up @@ -176,7 +185,7 @@ pub(super) async fn do_safe_lock(
interpreter,
existing.as_ref(),
settings,
state,
&state,
preview,
connectivity,
concurrency,
Expand Down
8 changes: 4 additions & 4 deletions crates/uv/src/commands/project/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,13 @@ pub(crate) async fn remove(
)
.await?;

// Initialize any shared state.
let state = SharedState::default();

// Lock and sync the environment, if necessary.
let lock = project::lock::do_safe_lock(
locked,
frozen,
project.workspace(),
venv.interpreter(),
settings.as_ref().into(),
&state,
preview,
connectivity,
concurrency,
Expand All @@ -124,11 +120,15 @@ pub(crate) async fn remove(
)
.await?;


// Perform a full sync, because we don't know what exactly is affected by the removal.
// TODO(ibraheem): Should we accept CLI overrides for this? Should we even sync here?
let extras = ExtrasSpecification::All;
let dev = true;

// Initialize any shared state.
let state = SharedState::default();

project::sync::do_sync(
&VirtualProject::Project(project),
&venv,
Expand Down
1 change: 0 additions & 1 deletion crates/uv/src/commands/project/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ pub(crate) async fn run(
project.workspace(),
venv.interpreter(),
settings.as_ref().into(),
&state,
preview,
connectivity,
concurrency,
Expand Down
7 changes: 3 additions & 4 deletions crates/uv/src/commands/project/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,12 @@ pub(crate) async fn sync(
)
.await?;

// Initialize any shared state.
let state = SharedState::default();

let lock = match do_safe_lock(
locked,
frozen,
project.workspace(),
venv.interpreter(),
settings.as_ref().into(),
&state,
preview,
connectivity,
concurrency,
Expand All @@ -102,6 +98,9 @@ pub(crate) async fn sync(
Err(err) => return Err(err.into()),
};

// Initialize any shared state.
let state = SharedState::default();

// Perform the sync operation.
do_sync(
&project,
Expand Down
1 change: 0 additions & 1 deletion crates/uv/src/commands/project/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ pub(crate) async fn tree(
&workspace,
&interpreter,
settings.as_ref(),
&SharedState::default(),
preview,
connectivity,
concurrency,
Expand Down
68 changes: 68 additions & 0 deletions crates/uv/tests/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,3 +516,71 @@ fn sync_build_isolation() -> Result<()> {

Ok(())
}

/// Avoid using incompatible versions for build dependencies that are also part of the resolved
/// environment. This is a very subtle issue, but: when locking, we don't enforce platform
/// compatibility. So, if we reuse the resolver state to install, and the install itself has to
/// preform a resolution (e.g., for the build dependencies of a source distribution), that
/// resolution may choose incompatible versions.
///
/// The key property here is that there's a shared package between the build dependencies and the
/// project dependencies.
#[test]
fn sync_reset_state() -> Result<()> {
let context = TestContext::new("3.12");

let pyproject_toml = context.temp_dir.child("pyproject.toml");
pyproject_toml.write_str(
r#"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["pydantic-core"]
[build-system]
requires = ["setuptools", "pydantic-core"]
build-backend = "setuptools.build_meta:__legacy__"
"#,
)?;

let setup_py = context.temp_dir.child("setup.py");
setup_py.write_str(indoc::indoc! { r#"
from setuptools import setup
import pydantic_core
setup(
name="project",
version="0.1.0",
packages=["project"],
install_requires=["pydantic-core"],
)
"# })?;

let src = context.temp_dir.child("project");
src.create_dir_all()?;

let init = src.child("__init__.py");
init.touch()?;


// Running `uv sync` should succeed.
uv_snapshot!(context.filters(), context.sync(), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
warning: `uv sync` is experimental and may change without warning
Resolved 3 packages in [TIME]
Prepared 3 packages in [TIME]
Installed 3 packages in [TIME]
+ project==0.1.0 (from file://[TEMP_DIR]/)
+ pydantic-core==2.17.0
+ typing-extensions==4.10.0
"###);

assert!(context.temp_dir.child("uv.lock").exists());

Ok(())
}
Empty file added foo/README.md
Empty file.
11 changes: 11 additions & 0 deletions foo/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[project]
name = "foo"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = ["numpy"]

[build-system]
requires = ["setuptools", "numpy"]
build-backend = "setuptools.build_meta:__legacy__"
3 changes: 3 additions & 0 deletions foo/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from setuptools import Extension, setup

import numpy
2 changes: 2 additions & 0 deletions foo/src/foo/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def hello() -> str:
return "Hello from foo!"
28 changes: 28 additions & 0 deletions foo/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 42cba53

Please sign in to comment.