Improve logging for environment locking (#3792)

```
DEBUG Acquired lock for `.venv`
```

instead of

```
DEBUG Trying to lock if free: .venv/.lock
```

At trace level, this includes the pre-lock message as well

```
TRACE Checking lock for `.venv`
DEBUG Acquired lock for `.venv`
```

We'll still display the lock file path when something goes wrong
This commit is contained in:
Zanie Blue 2024-05-23 10:27:07 -04:00 committed by GitHub
parent ddfbee1cb6
commit 81eff0ecc8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 9 deletions

View file

@ -4,7 +4,7 @@ use std::path::{Path, PathBuf};
use fs2::FileExt;
use fs_err as fs;
use tempfile::NamedTempFile;
use tracing::{debug, error, warn};
use tracing::{debug, error, trace, warn};
use uv_warnings::warn_user;
@ -289,9 +289,12 @@ pub struct LockedFile(fs_err::File);
impl LockedFile {
pub fn acquire(path: impl AsRef<Path>, resource: impl Display) -> Result<Self, std::io::Error> {
let file = fs_err::File::create(path.as_ref())?;
debug!("Trying to lock if free: {}", path.as_ref().user_display());
trace!("Checking lock for `{resource}`");
match file.file().try_lock_exclusive() {
Ok(()) => Ok(Self(file)),
Ok(()) => {
debug!("Acquired lock for `{resource}`");
Ok(Self(file))
}
Err(err) => {
// Log error code and enum kind to help debugging more exotic failures
debug!("Try lock error, waiting for exclusive lock: {:?}", err);

View file

@ -200,18 +200,15 @@ impl PythonEnvironment {
pub fn lock(&self) -> Result<LockedFile, std::io::Error> {
if let Some(target) = self.0.interpreter.target() {
// If we're installing into a `--target`, use a target-specific lock file.
LockedFile::acquire(
target.root().join(".lock"),
target.root().simplified_display(),
)
LockedFile::acquire(target.root().join(".lock"), target.root().user_display())
} else if self.0.interpreter.is_virtualenv() {
// If the environment a virtualenv, use a virtualenv-specific lock file.
LockedFile::acquire(self.0.root.join(".lock"), self.0.root.simplified_display())
LockedFile::acquire(self.0.root.join(".lock"), self.0.root.user_display())
} else {
// Otherwise, use a global lock file.
LockedFile::acquire(
env::temp_dir().join(format!("uv-{}.lock", cache_key::digest(&self.0.root))),
self.0.root.simplified_display(),
self.0.root.user_display(),
)
}
}