1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
use std::ops::Add;
use std::path::{Path, PathBuf};
use fs_err as fs;
use itertools::Itertools;
use tracing::debug;
use uv_dirs::user_uv_config_dir;
use uv_fs::Simplified;
use uv_warnings::warn_user_once;
use crate::PythonRequest;
/// The file name for Python version pins.
pub static PYTHON_VERSION_FILENAME: &str = ".python-version";
/// The file name for multiple Python version declarations.
pub static PYTHON_VERSIONS_FILENAME: &str = ".python-versions";
/// A `.python-version` or `.python-versions` file.
#[derive(Debug, Clone)]
pub struct PythonVersionFile {
/// The path to the version file.
path: PathBuf,
/// The Python version requests declared in the file.
versions: Vec<PythonRequest>,
}
/// Whether to prefer the `.python-version` or `.python-versions` file.
#[derive(Debug, Clone, Copy, Default)]
pub enum FilePreference {
#[default]
Version,
Versions,
}
#[derive(Debug, Default, Clone)]
pub struct DiscoveryOptions<'a> {
/// The path to stop discovery at.
stop_discovery_at: Option<&'a Path>,
/// Ignore Python version files.
///
/// Discovery will still run in order to display a log about the ignored file.
no_config: bool,
/// Whether `.python-version` or `.python-versions` should be preferred.
preference: FilePreference,
/// Whether to ignore local version files, and only search for a global one.
no_local: bool,
}
impl<'a> DiscoveryOptions<'a> {
#[must_use]
pub fn with_no_config(self, no_config: bool) -> Self {
Self { no_config, ..self }
}
#[must_use]
pub fn with_preference(self, preference: FilePreference) -> Self {
Self { preference, ..self }
}
#[must_use]
pub fn with_stop_discovery_at(self, stop_discovery_at: Option<&'a Path>) -> Self {
Self {
stop_discovery_at,
..self
}
}
#[must_use]
pub fn with_no_local(self, no_local: bool) -> Self {
Self { no_local, ..self }
}
}
impl PythonVersionFile {
/// Find a Python version file in the given directory or any of its parents.
pub async fn discover(
working_directory: impl AsRef<Path>,
options: &DiscoveryOptions<'_>,
) -> Result<Option<Self>, std::io::Error> {
let allow_local = !options.no_local;
let Some(path) = allow_local.then(|| {
// First, try to find a local version file.
let local = Self::find_nearest(&working_directory, options);
if local.is_none() {
// Log where we searched for the file, if not found
if let Some(stop_discovery_at) = options.stop_discovery_at {
if stop_discovery_at == working_directory.as_ref() {
debug!(
"No Python version file found in workspace: {}",
working_directory.as_ref().display()
);
} else {
debug!(
"No Python version file found between working directory `{}` and workspace root `{}`",
working_directory.as_ref().display(),
stop_discovery_at.display()
);
}
} else {
debug!(
"No Python version file found in ancestors of working directory: {}",
working_directory.as_ref().display()
);
}
}
local
}).flatten().or_else(|| {
// Search for a global config
Self::find_global(options)
}) else {
return Ok(None);
};
if options.no_config {
debug!(
"Ignoring Python version file at `{}` due to `--no-config`",
path.user_display()
);
return Ok(None);
}
// Uses `try_from_path` instead of `from_path` to avoid TOCTOU failures.
Self::try_from_path(path).await
}
fn find_global(options: &DiscoveryOptions<'_>) -> Option<PathBuf> {
let user_config_dir = user_uv_config_dir()?;
Self::find_in_directory(&user_config_dir, options)
}
fn find_nearest(path: impl AsRef<Path>, options: &DiscoveryOptions<'_>) -> Option<PathBuf> {
path.as_ref()
.ancestors()
.take_while(|path| {
// Only walk up the given directory, if any.
options
.stop_discovery_at
.and_then(Path::parent)
.map(|stop_discovery_at| stop_discovery_at != *path)
.unwrap_or(true)
})
.find_map(|path| Self::find_in_directory(path, options))
}
fn find_in_directory(path: &Path, options: &DiscoveryOptions<'_>) -> Option<PathBuf> {
let version_path = path.join(PYTHON_VERSION_FILENAME);
let versions_path = path.join(PYTHON_VERSIONS_FILENAME);
let paths = match options.preference {
FilePreference::Versions => [versions_path, version_path],
FilePreference::Version => [version_path, versions_path],
};
paths.into_iter().find(|path| path.is_file())
}
/// Try to read a Python version file at the given path.
///
/// If the file does not exist, `Ok(None)` is returned.
pub async fn try_from_path(path: PathBuf) -> Result<Option<Self>, std::io::Error> {
match fs::tokio::read_to_string(&path).await {
Ok(content) => {
debug!(
"Reading Python requests from version file at `{}`",
path.display()
);
let versions = content
.lines()
.filter(|line| {
// Skip comments and empty lines.
let trimmed = line.trim();
!(trimmed.is_empty() || trimmed.starts_with('#'))
})
.map(ToString::to_string)
.map(|version| PythonRequest::parse(&version))
.filter(|request| {
if let PythonRequest::ExecutableName(name) = request {
warn_user_once!(
"Ignoring unsupported Python request `{name}` in version file: {}",
path.display()
);
false
} else {
true
}
})
.collect();
Ok(Some(Self { path, versions }))
}
Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(err) => Err(err),
}
}
/// Read a Python version file at the given path.
///
/// If the file does not exist, an error is returned.
pub async fn from_path(path: PathBuf) -> Result<Self, std::io::Error> {
let Some(result) = Self::try_from_path(path).await? else {
return Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Version file not found".to_string(),
));
};
Ok(result)
}
/// Create a new representation of a version file at the given path.
///
/// The file will not any include versions; see [`PythonVersionFile::with_versions`].
/// The file will not be created; see [`PythonVersionFile::write`].
pub fn new(path: PathBuf) -> Self {
Self {
path,
versions: vec![],
}
}
/// Create a new representation of a global Python version file.
///
/// Returns [`None`] if the user configuration directory cannot be determined.
pub fn global() -> Option<Self> {
let path = user_uv_config_dir()?.join(PYTHON_VERSION_FILENAME);
Some(Self::new(path))
}
/// Returns `true` if the version file is a global version file.
pub fn is_global(&self) -> bool {
Self::global().is_some_and(|global| self.path() == global.path())
}
/// Return the first request declared in the file, if any.
pub fn version(&self) -> Option<&PythonRequest> {
self.versions.first()
}
/// Iterate of all versions declared in the file.
pub fn versions(&self) -> impl Iterator<Item = &PythonRequest> {
self.versions.iter()
}
/// Cast to a list of all versions declared in the file.
pub fn into_versions(self) -> Vec<PythonRequest> {
self.versions
}
/// Cast to the first version declared in the file, if any.
pub fn into_version(self) -> Option<PythonRequest> {
self.versions.into_iter().next()
}
/// Return the path to the version file.
pub fn path(&self) -> &Path {
&self.path
}
/// Return the file name of the version file (guaranteed to be one of `.python-version` or
/// `.python-versions`).
pub fn file_name(&self) -> &str {
self.path.file_name().unwrap().to_str().unwrap()
}
/// Set the versions for the file.
#[must_use]
pub fn with_versions(self, versions: Vec<PythonRequest>) -> Self {
Self {
path: self.path,
versions,
}
}
/// Update the version file on the file system.
pub async fn write(&self) -> Result<(), std::io::Error> {
debug!("Writing Python versions to `{}`", self.path.display());
if let Some(parent) = self.path.parent() {
fs_err::tokio::create_dir_all(parent).await?;
}
fs::tokio::write(
&self.path,
self.versions
.iter()
.map(PythonRequest::to_canonical_string)
.join("\n")
.add("\n")
.as_bytes(),
)
.await
}
}