Initialize all --prefix subdirectories (#4895)

## Summary

We need to initialize the same directories that we create in `bare.rs`,
since the installer expects them to exist.

Closes #4892.
This commit is contained in:
Charlie Marsh 2024-07-08 09:15:25 -05:00 committed by GitHub
parent 2d651fe264
commit b9d661012d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 26 additions and 36 deletions

View file

@ -127,23 +127,21 @@ impl PythonEnvironment {
} }
/// Create a [`PythonEnvironment`] from an existing [`Interpreter`] and `--target` directory. /// Create a [`PythonEnvironment`] from an existing [`Interpreter`] and `--target` directory.
#[must_use] pub fn with_target(self, target: Target) -> std::io::Result<Self> {
pub fn with_target(self, target: Target) -> Self {
let inner = Arc::unwrap_or_clone(self.0); let inner = Arc::unwrap_or_clone(self.0);
Self(Arc::new(PythonEnvironmentShared { Ok(Self(Arc::new(PythonEnvironmentShared {
interpreter: inner.interpreter.with_target(target), interpreter: inner.interpreter.with_target(target)?,
..inner ..inner
})) })))
} }
/// Create a [`PythonEnvironment`] from an existing [`Interpreter`] and `--prefix` directory. /// Create a [`PythonEnvironment`] from an existing [`Interpreter`] and `--prefix` directory.
#[must_use] pub fn with_prefix(self, prefix: Prefix) -> std::io::Result<Self> {
pub fn with_prefix(self, prefix: Prefix) -> Self {
let inner = Arc::unwrap_or_clone(self.0); let inner = Arc::unwrap_or_clone(self.0);
Self(Arc::new(PythonEnvironmentShared { Ok(Self(Arc::new(PythonEnvironmentShared {
interpreter: inner.interpreter.with_prefix(prefix), interpreter: inner.interpreter.with_prefix(prefix)?,
..inner ..inner
})) })))
} }
/// Returns the root (i.e., `prefix`) of the Python interpreter. /// Returns the root (i.e., `prefix`) of the Python interpreter.

View file

@ -124,21 +124,21 @@ impl Interpreter {
} }
/// Return a new [`Interpreter`] to install into the given `--target` directory. /// Return a new [`Interpreter`] to install into the given `--target` directory.
#[must_use] pub fn with_target(self, target: Target) -> io::Result<Self> {
pub fn with_target(self, target: Target) -> Self { target.init()?;
Self { Ok(Self {
target: Some(target), target: Some(target),
..self ..self
} })
} }
/// Return a new [`Interpreter`] to install into the given `--prefix` directory. /// Return a new [`Interpreter`] to install into the given `--prefix` directory.
#[must_use] pub fn with_prefix(self, prefix: Prefix) -> io::Result<Self> {
pub fn with_prefix(self, prefix: Prefix) -> Self { prefix.init(self.virtualenv())?;
Self { Ok(Self {
prefix: Some(prefix), prefix: Some(prefix),
..self ..self
} })
} }
/// Return the [`Interpreter`] for the base executable, if it's available. /// Return the [`Interpreter`] for the base executable, if it's available.

View file

@ -25,8 +25,10 @@ impl Prefix {
} }
/// Initialize the `--prefix` directory. /// Initialize the `--prefix` directory.
pub fn init(&self) -> std::io::Result<()> { pub fn init(&self, virtualenv: &Scheme) -> std::io::Result<()> {
fs_err::create_dir_all(&self.0)?; for site_packages in self.site_packages(virtualenv) {
fs_err::create_dir_all(site_packages)?;
}
Ok(()) Ok(())
} }

View file

@ -136,15 +136,13 @@ pub(crate) async fn pip_install(
"Using `--target` directory at {}", "Using `--target` directory at {}",
target.root().user_display() target.root().user_display()
); );
target.init()?; environment.with_target(target)?
environment.with_target(target)
} else if let Some(prefix) = prefix { } else if let Some(prefix) = prefix {
debug!( debug!(
"Using `--prefix` directory at {}", "Using `--prefix` directory at {}",
prefix.root().user_display() prefix.root().user_display()
); );
prefix.init()?; environment.with_prefix(prefix)?
environment.with_prefix(prefix)
} else { } else {
environment environment
}; };

View file

@ -134,15 +134,13 @@ pub(crate) async fn pip_sync(
"Using `--target` directory at {}", "Using `--target` directory at {}",
target.root().user_display() target.root().user_display()
); );
target.init()?; environment.with_target(target)?
environment.with_target(target)
} else if let Some(prefix) = prefix { } else if let Some(prefix) = prefix {
debug!( debug!(
"Using `--prefix` directory at {}", "Using `--prefix` directory at {}",
prefix.root().user_display() prefix.root().user_display()
); );
prefix.init()?; environment.with_prefix(prefix)?
environment.with_prefix(prefix)
} else { } else {
environment environment
}; };

View file

@ -67,15 +67,13 @@ pub(crate) async fn pip_uninstall(
"Using `--target` directory at {}", "Using `--target` directory at {}",
target.root().user_display() target.root().user_display()
); );
target.init()?; environment.with_target(target)?
environment.with_target(target)
} else if let Some(prefix) = prefix { } else if let Some(prefix) = prefix {
debug!( debug!(
"Using `--prefix` directory at {}", "Using `--prefix` directory at {}",
prefix.root().user_display() prefix.root().user_display()
); );
prefix.init()?; environment.with_prefix(prefix)?
environment.with_prefix(prefix)
} else { } else {
environment environment
}; };

View file

@ -5104,10 +5104,6 @@ fn target_no_build_isolation() -> Result<()> {
/// Sync to a `--prefix` directory. /// Sync to a `--prefix` directory.
#[test] #[test]
#[cfg_attr(
target_os = "macos",
ignore = "On macOS, we fail to reflink due to a non-existent site-packages directory"
)]
fn prefix() -> Result<()> { fn prefix() -> Result<()> {
let context = TestContext::new("3.12"); let context = TestContext::new("3.12");