Skip to main content

uv_settings/
lib.rs

1use std::num::NonZeroUsize;
2use std::ops::Deref;
3use std::path::{Path, PathBuf};
4use std::str::FromStr;
5use std::time::Duration;
6use tracing::info_span;
7use uv_client::{DEFAULT_CONNECT_TIMEOUT, DEFAULT_READ_TIMEOUT, DEFAULT_READ_TIMEOUT_UPLOAD};
8use uv_configuration::RequiredVersion;
9use uv_dirs::{system_config_file, user_config_dir};
10use uv_distribution_types::Origin;
11use uv_flags::EnvironmentFlags;
12use uv_fs::Simplified;
13use uv_normalize::{GroupName, PackageName};
14use uv_pep440::Version;
15use uv_redacted::DisplaySafeUrl;
16use uv_static::{EnvVars, InvalidEnvironmentVariable, parse_boolish_environment_variable};
17use uv_torch::AmdGpuArchitecture;
18use uv_warnings::warn_user;
19
20pub use crate::combine::*;
21pub use crate::settings::*;
22
23mod combine;
24mod settings;
25
26/// The [`Options`] as loaded from a configuration file on disk.
27#[derive(Debug, Clone)]
28pub struct FilesystemOptions(Options);
29
30impl FilesystemOptions {
31    /// Convert the [`FilesystemOptions`] into [`Options`].
32    pub fn into_options(self) -> Options {
33        self.0
34    }
35}
36
37impl Deref for FilesystemOptions {
38    type Target = Options;
39
40    fn deref(&self) -> &Self::Target {
41        &self.0
42    }
43}
44
45impl FilesystemOptions {
46    /// Load the user [`FilesystemOptions`].
47    pub fn user() -> Result<Option<Self>, Error> {
48        let Some(dir) = user_config_dir() else {
49            return Ok(None);
50        };
51        let root = dir.join("uv");
52        let file = root.join("uv.toml");
53
54        tracing::debug!("Searching for user configuration in: `{}`", file.display());
55        match read_file(&file) {
56            Ok(options) => {
57                tracing::debug!("Found user configuration in: `{}`", file.display());
58                validate_uv_toml(&file, &options)?;
59                Ok(Some(Self(options.with_origin(Origin::User))))
60            }
61            Err(Error::Io(err))
62                if matches!(
63                    err.kind(),
64                    std::io::ErrorKind::NotFound
65                        | std::io::ErrorKind::NotADirectory
66                        | std::io::ErrorKind::PermissionDenied
67                ) =>
68            {
69                Ok(None)
70            }
71            Err(err) => Err(err),
72        }
73    }
74
75    pub fn system() -> Result<Option<Self>, Error> {
76        if parse_boolish_environment_variable(EnvVars::UV_NO_SYSTEM_CONFIG)? == Some(true) {
77            return Ok(None);
78        }
79
80        let Some(file) = system_config_file() else {
81            return Ok(None);
82        };
83
84        tracing::debug!("Found system configuration in: `{}`", file.display());
85        let options = read_file(&file)?;
86        validate_uv_toml(&file, &options)?;
87        Ok(Some(Self(options.with_origin(Origin::System))))
88    }
89
90    /// Find the [`FilesystemOptions`] for the given path.
91    ///
92    /// The search starts at the given path and goes up the directory tree until a `uv.toml` file or
93    /// `pyproject.toml` file is found.
94    pub fn find(path: &Path) -> Result<Option<Self>, Error> {
95        for ancestor in path.ancestors() {
96            match Self::from_directory(ancestor) {
97                Ok(Some(options)) => {
98                    return Ok(Some(options));
99                }
100                Ok(None) => {
101                    // Continue traversing the directory tree.
102                }
103                Err(Error::PyprojectToml(path, err)) => {
104                    // If we see an invalid `pyproject.toml`, warn but continue.
105                    warn_user!(
106                        "Failed to parse `{}` during settings discovery:\n{}",
107                        path.user_display().cyan(),
108                        textwrap::indent(&err.to_string(), "  ")
109                    );
110                }
111                Err(err) => {
112                    // Otherwise, warn and stop.
113                    return Err(err);
114                }
115            }
116        }
117        Ok(None)
118    }
119
120    /// Load a [`FilesystemOptions`] from a directory, preferring a `uv.toml` file over a
121    /// `pyproject.toml` file.
122    fn from_directory(dir: &Path) -> Result<Option<Self>, Error> {
123        // Read a `uv.toml` file in the current directory.
124        let path = dir.join("uv.toml");
125        match fs_err::read_to_string(&path) {
126            Ok(content) => {
127                let options =
128                    info_span!("toml::from_str filesystem options uv.toml", path = %path.display())
129                        .in_scope(|| toml::from_str::<Options>(&content))
130                        .map_err(|err| {
131                            check_uv_toml_required_version(
132                                &path,
133                                &content,
134                                Error::UvToml(path.clone(), Box::new(err)),
135                            )
136                        })?
137                        .relative_to(&std::path::absolute(dir)?)?;
138
139                // If the directory also contains a `[tool.uv]` table in a `pyproject.toml` file,
140                // warn.
141                let pyproject = dir.join("pyproject.toml");
142                if let Ok(content) = fs_err::read_to_string(&pyproject) {
143                    let result = info_span!("toml::from_str filesystem options pyproject.toml", path = %pyproject.display())
144                        .in_scope(|| toml::from_str::<PyProjectToml>(&content)).ok();
145                    if let Some(options) =
146                        result.and_then(|pyproject| pyproject.tool.and_then(|tool| tool.uv))
147                    {
148                        warn_uv_toml_masked_fields(&options);
149                    }
150                }
151
152                tracing::debug!("Found workspace configuration at `{}`", path.display());
153                validate_uv_toml(&path, &options)?;
154                return Ok(Some(Self(options.with_origin(Origin::Project))));
155            }
156            Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
157            Err(err) => return Err(err.into()),
158        }
159
160        // Read a `pyproject.toml` file in the current directory.
161        let path = dir.join("pyproject.toml");
162        match fs_err::read_to_string(&path) {
163            Ok(content) => {
164                // Parse, but skip any `pyproject.toml` that doesn't have a `[tool.uv]` section.
165                let pyproject =
166                    info_span!("toml::from_str filesystem options pyproject.toml", path = %path.display())
167                        .in_scope(|| toml::from_str::<PyProjectToml>(&content))
168                        .map_err(|err| {
169                            check_pyproject_required_version(&path, &content, err)
170                        })?;
171                let Some(tool) = pyproject.tool else {
172                    tracing::debug!(
173                        "Skipping `pyproject.toml` in `{}` (no `[tool]` section)",
174                        dir.display()
175                    );
176                    return Ok(None);
177                };
178                let Some(options) = tool.uv else {
179                    tracing::debug!(
180                        "Skipping `pyproject.toml` in `{}` (no `[tool.uv]` section)",
181                        dir.display()
182                    );
183                    return Ok(None);
184                };
185
186                let options = options.relative_to(&std::path::absolute(dir)?)?;
187
188                tracing::debug!("Found workspace configuration at `{}`", path.display());
189                return Ok(Some(Self(options)));
190            }
191            Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
192            Err(err) => return Err(err.into()),
193        }
194
195        Ok(None)
196    }
197
198    /// Load a [`FilesystemOptions`] from a `uv.toml` file.
199    pub fn from_file(path: impl AsRef<Path>) -> Result<Self, Error> {
200        let path = path.as_ref();
201        tracing::debug!("Reading user configuration from: `{}`", path.display());
202
203        let options = read_file(path)?;
204        validate_uv_toml(path, &options)?;
205        Ok(Self(options))
206    }
207}
208
209impl From<Options> for FilesystemOptions {
210    fn from(options: Options) -> Self {
211        Self(options)
212    }
213}
214
215/// Load [`Options`] from a `uv.toml` file.
216fn read_file(path: &Path) -> Result<Options, Error> {
217    let content = fs_err::read_to_string(path)?;
218    let options = info_span!("toml::from_str filesystem options uv.toml", path = %path.display())
219        .in_scope(|| toml::from_str::<Options>(&content))
220        .map_err(|err| {
221            check_uv_toml_required_version(
222                path,
223                &content,
224                Error::UvToml(path.to_path_buf(), Box::new(err)),
225            )
226        })?;
227    let options = if let Some(parent) = std::path::absolute(path)?.parent() {
228        options.relative_to(parent)?
229    } else {
230        options
231    };
232    Ok(options)
233}
234
235/// If `required_version` is set and incompatible with the running uv, return the corresponding
236/// [`Error::RequiredVersion`].
237fn required_version_mismatch(required_version: Option<RequiredVersion>) -> Option<Error> {
238    let required_version = required_version?;
239    let package_version = Version::from_str(uv_version::version())
240        .expect("uv crate version to be a valid PEP 440 version");
241    if required_version.contains(&package_version) {
242        None
243    } else {
244        Some(Error::RequiredVersion {
245            required_version,
246            package_version,
247        })
248    }
249}
250
251/// On a `pyproject.toml` settings parse error, check whether `tool.uv.required-version` should
252/// take precedence over that error.
253fn check_pyproject_required_version(path: &Path, content: &str, source: toml::de::Error) -> Error {
254    let fallback = || Error::PyprojectToml(path.to_path_buf(), Box::new(source));
255    let Ok(pyproject) = info_span!(
256        "toml::from_str filesystem required-version pyproject.toml",
257        path = %path.display()
258    )
259    .in_scope(|| toml::from_str::<PyProjectRequiredVersionToml>(content)) else {
260        return fallback();
261    };
262
263    let required_version = pyproject
264        .tool
265        .and_then(|tool| tool.uv)
266        .and_then(|uv| uv.required_version);
267    required_version_mismatch(required_version).unwrap_or_else(fallback)
268}
269
270/// On a `uv.toml` settings parse or schema error, check whether top-level `required-version`
271/// should take precedence over that error.
272fn check_uv_toml_required_version(path: &Path, content: &str, source: Error) -> Error {
273    let Ok(uv_toml) = info_span!(
274        "toml::from_str filesystem required-version uv.toml",
275        path = %path.display()
276    )
277    .in_scope(|| toml::from_str::<UvRequiredVersionToml>(content)) else {
278        return source;
279    };
280    required_version_mismatch(uv_toml.required_version).unwrap_or(source)
281}
282
283/// Validate that an [`Options`] schema is compatible with `uv.toml`.
284fn validate_uv_toml(path: &Path, options: &Options) -> Result<(), Error> {
285    // A `required-version` mismatch takes precedence over a schema error.
286    if let Some(err) = required_version_mismatch(options.globals.required_version.clone()) {
287        return Err(err);
288    }
289    let Options {
290        globals: _,
291        top_level: _,
292        install_mirrors: _,
293        publish: _,
294        add: _,
295        audit: _,
296        pip: _,
297        cache_keys: _,
298        override_dependencies: _,
299        exclude_dependencies: _,
300        constraint_dependencies: _,
301        build_constraint_dependencies: _,
302        environments,
303        required_environments,
304        conflicts,
305        workspace,
306        sources,
307        dev_dependencies,
308        default_groups,
309        dependency_groups,
310        managed,
311        package,
312        build_backend,
313    } = options;
314    // The `uv.toml` format is not allowed to include any of the following, which are
315    // permitted by the schema since they _can_ be included in `pyproject.toml` files
316    // (and we want to use `deny_unknown_fields`).
317    if conflicts.is_some() {
318        return Err(Error::PyprojectOnlyField(path.to_path_buf(), "conflicts"));
319    }
320    if workspace.is_some() {
321        return Err(Error::PyprojectOnlyField(path.to_path_buf(), "workspace"));
322    }
323    if sources.is_some() {
324        return Err(Error::PyprojectOnlyField(path.to_path_buf(), "sources"));
325    }
326    if dev_dependencies.is_some() {
327        return Err(Error::PyprojectOnlyField(
328            path.to_path_buf(),
329            "dev-dependencies",
330        ));
331    }
332    if default_groups.is_some() {
333        return Err(Error::PyprojectOnlyField(
334            path.to_path_buf(),
335            "default-groups",
336        ));
337    }
338    if dependency_groups.is_some() {
339        return Err(Error::PyprojectOnlyField(
340            path.to_path_buf(),
341            "dependency-groups",
342        ));
343    }
344    if managed.is_some() {
345        return Err(Error::PyprojectOnlyField(path.to_path_buf(), "managed"));
346    }
347    if package.is_some() {
348        return Err(Error::PyprojectOnlyField(path.to_path_buf(), "package"));
349    }
350    if build_backend.is_some() {
351        return Err(Error::PyprojectOnlyField(
352            path.to_path_buf(),
353            "build-backend",
354        ));
355    }
356    if environments.is_some() {
357        return Err(Error::PyprojectOnlyField(
358            path.to_path_buf(),
359            "environments",
360        ));
361    }
362    if required_environments.is_some() {
363        return Err(Error::PyprojectOnlyField(
364            path.to_path_buf(),
365            "required-environments",
366        ));
367    }
368    Ok(())
369}
370
371/// Validate that an [`Options`] contains no fields that `uv.toml` would mask
372///
373/// This is essentially the inverse of [`validate_uv_toml`].
374#[allow(deprecated)]
375fn warn_uv_toml_masked_fields(options: &Options) {
376    let Options {
377        globals:
378            GlobalOptions {
379                required_version,
380                system_certs,
381                native_tls,
382                offline,
383                no_cache,
384                cache_dir,
385                preview,
386                python_preference,
387                python_downloads,
388                concurrent_downloads,
389                concurrent_builds,
390                concurrent_installs,
391                allow_insecure_host,
392                http_proxy,
393                https_proxy,
394                no_proxy,
395            },
396        top_level:
397            ResolverInstallerSchema {
398                index,
399                index_url,
400                extra_index_url,
401                no_index,
402                find_links,
403                index_strategy,
404                keyring_provider,
405                resolution,
406                prerelease,
407                fork_strategy,
408                dependency_metadata,
409                config_settings,
410                config_settings_package,
411                no_build_isolation,
412                no_build_isolation_package,
413                extra_build_dependencies,
414                extra_build_variables,
415                exclude_newer,
416                exclude_newer_package,
417                link_mode,
418                compile_bytecode,
419                no_sources,
420                no_sources_package: _,
421                upgrade,
422                upgrade_package,
423                reinstall,
424                reinstall_package,
425                no_build,
426                no_build_package,
427                no_binary,
428                no_binary_package,
429                torch_backend,
430            },
431        install_mirrors:
432            PythonInstallMirrors {
433                python_install_mirror,
434                pypy_install_mirror,
435                python_downloads_json_url,
436            },
437        publish:
438            PublishOptions {
439                publish_url,
440                trusted_publishing,
441                check_url,
442            },
443        add: AddOptions { add_bounds },
444        audit: _,
445        pip,
446        cache_keys,
447        override_dependencies,
448        exclude_dependencies,
449        constraint_dependencies,
450        build_constraint_dependencies,
451        environments: _,
452        required_environments: _,
453        conflicts: _,
454        workspace: _,
455        sources: _,
456        dev_dependencies: _,
457        default_groups: _,
458        dependency_groups: _,
459        managed: _,
460        package: _,
461        build_backend: _,
462    } = options;
463
464    let mut masked_fields = vec![];
465
466    if required_version.is_some() {
467        masked_fields.push("required-version");
468    }
469    if system_certs.is_some() {
470        masked_fields.push("system-certs");
471    }
472    if native_tls.is_some() {
473        masked_fields.push("native-tls");
474    }
475    if offline.is_some() {
476        masked_fields.push("offline");
477    }
478    if no_cache.is_some() {
479        masked_fields.push("no-cache");
480    }
481    if cache_dir.is_some() {
482        masked_fields.push("cache-dir");
483    }
484    if preview.is_some() {
485        masked_fields.push("preview");
486    }
487    if python_preference.is_some() {
488        masked_fields.push("python-preference");
489    }
490    if python_downloads.is_some() {
491        masked_fields.push("python-downloads");
492    }
493    if concurrent_downloads.is_some() {
494        masked_fields.push("concurrent-downloads");
495    }
496    if concurrent_builds.is_some() {
497        masked_fields.push("concurrent-builds");
498    }
499    if concurrent_installs.is_some() {
500        masked_fields.push("concurrent-installs");
501    }
502    if allow_insecure_host.is_some() {
503        masked_fields.push("allow-insecure-host");
504    }
505    if http_proxy.is_some() {
506        masked_fields.push("http-proxy");
507    }
508    if https_proxy.is_some() {
509        masked_fields.push("https-proxy");
510    }
511    if no_proxy.is_some() {
512        masked_fields.push("no-proxy");
513    }
514    if index.is_some() {
515        masked_fields.push("index");
516    }
517    if index_url.is_some() {
518        masked_fields.push("index-url");
519    }
520    if extra_index_url.is_some() {
521        masked_fields.push("extra-index-url");
522    }
523    if no_index.is_some() {
524        masked_fields.push("no-index");
525    }
526    if find_links.is_some() {
527        masked_fields.push("find-links");
528    }
529    if index_strategy.is_some() {
530        masked_fields.push("index-strategy");
531    }
532    if keyring_provider.is_some() {
533        masked_fields.push("keyring-provider");
534    }
535    if resolution.is_some() {
536        masked_fields.push("resolution");
537    }
538    if prerelease.is_some() {
539        masked_fields.push("prerelease");
540    }
541    if fork_strategy.is_some() {
542        masked_fields.push("fork-strategy");
543    }
544    if dependency_metadata.is_some() {
545        masked_fields.push("dependency-metadata");
546    }
547    if config_settings.is_some() {
548        masked_fields.push("config-settings");
549    }
550    if config_settings_package.is_some() {
551        masked_fields.push("config-settings-package");
552    }
553    if no_build_isolation.is_some() {
554        masked_fields.push("no-build-isolation");
555    }
556    if no_build_isolation_package.is_some() {
557        masked_fields.push("no-build-isolation-package");
558    }
559    if extra_build_dependencies.is_some() {
560        masked_fields.push("extra-build-dependencies");
561    }
562    if extra_build_variables.is_some() {
563        masked_fields.push("extra-build-variables");
564    }
565    if exclude_newer.is_some() {
566        masked_fields.push("exclude-newer");
567    }
568    if exclude_newer_package.is_some() {
569        masked_fields.push("exclude-newer-package");
570    }
571    if link_mode.is_some() {
572        masked_fields.push("link-mode");
573    }
574    if compile_bytecode.is_some() {
575        masked_fields.push("compile-bytecode");
576    }
577    if no_sources.is_some() {
578        masked_fields.push("no-sources");
579    }
580    if upgrade.is_some() {
581        masked_fields.push("upgrade");
582    }
583    if upgrade_package.is_some() {
584        masked_fields.push("upgrade-package");
585    }
586    if reinstall.is_some() {
587        masked_fields.push("reinstall");
588    }
589    if reinstall_package.is_some() {
590        masked_fields.push("reinstall-package");
591    }
592    if no_build.is_some() {
593        masked_fields.push("no-build");
594    }
595    if no_build_package.is_some() {
596        masked_fields.push("no-build-package");
597    }
598    if no_binary.is_some() {
599        masked_fields.push("no-binary");
600    }
601    if no_binary_package.is_some() {
602        masked_fields.push("no-binary-package");
603    }
604    if torch_backend.is_some() {
605        masked_fields.push("torch-backend");
606    }
607    if python_install_mirror.is_some() {
608        masked_fields.push("python-install-mirror");
609    }
610    if pypy_install_mirror.is_some() {
611        masked_fields.push("pypy-install-mirror");
612    }
613    if python_downloads_json_url.is_some() {
614        masked_fields.push("python-downloads-json-url");
615    }
616    if publish_url.is_some() {
617        masked_fields.push("publish-url");
618    }
619    if trusted_publishing.is_some() {
620        masked_fields.push("trusted-publishing");
621    }
622    if check_url.is_some() {
623        masked_fields.push("check-url");
624    }
625    if add_bounds.is_some() {
626        masked_fields.push("add-bounds");
627    }
628    if pip.is_some() {
629        masked_fields.push("pip");
630    }
631    if cache_keys.is_some() {
632        masked_fields.push("cache_keys");
633    }
634    if override_dependencies.is_some() {
635        masked_fields.push("override-dependencies");
636    }
637    if exclude_dependencies.is_some() {
638        masked_fields.push("exclude-dependencies");
639    }
640    if constraint_dependencies.is_some() {
641        masked_fields.push("constraint-dependencies");
642    }
643    if build_constraint_dependencies.is_some() {
644        masked_fields.push("build-constraint-dependencies");
645    }
646    if !masked_fields.is_empty() {
647        let field_listing = masked_fields.join("\n- ");
648        warn_user!(
649            "Found both a `uv.toml` file and a `[tool.uv]` section in an adjacent `pyproject.toml`. The following fields from `[tool.uv]` will be ignored in favor of the `uv.toml` file:\n- {}",
650            field_listing,
651        );
652    }
653}
654
655#[derive(thiserror::Error, Debug)]
656pub enum Error {
657    #[error(transparent)]
658    Io(#[from] std::io::Error),
659
660    #[error(transparent)]
661    Index(#[from] uv_distribution_types::IndexUrlError),
662
663    #[error("Failed to parse: `{}`", _0.user_display())]
664    PyprojectToml(PathBuf, #[source] Box<toml::de::Error>),
665
666    #[error("Failed to parse: `{}`", _0.user_display())]
667    UvToml(PathBuf, #[source] Box<toml::de::Error>),
668
669    #[error("Failed to parse: `{}`. The `{}` field is not allowed in a `uv.toml` file. `{}` is only applicable in the context of a project, and should be placed in a `pyproject.toml` file instead.", _0.user_display(), _1, _1
670    )]
671    PyprojectOnlyField(PathBuf, &'static str),
672
673    #[error(
674        "Required uv version `{required_version}` does not match the running version `{package_version}`"
675    )]
676    RequiredVersion {
677        required_version: RequiredVersion,
678        package_version: Version,
679    },
680
681    #[error(transparent)]
682    InvalidEnvironmentVariable(#[from] InvalidEnvironmentVariable),
683}
684
685#[derive(Copy, Clone, Debug)]
686pub struct Concurrency {
687    pub downloads: Option<NonZeroUsize>,
688    pub builds: Option<NonZeroUsize>,
689    pub installs: Option<NonZeroUsize>,
690}
691
692/// A boolean flag parsed from an environment variable.
693///
694/// Stores both the value and the environment variable name for use in error messages.
695#[derive(Debug, Clone, Copy)]
696pub struct EnvFlag {
697    pub value: Option<bool>,
698    pub env_var: &'static str,
699}
700
701impl EnvFlag {
702    /// Create a new [`EnvFlag`] by parsing the given environment variable.
703    fn new(env_var: &'static str) -> Result<Self, Error> {
704        Ok(Self {
705            value: parse_boolish_environment_variable(env_var)?,
706            env_var,
707        })
708    }
709}
710
711/// Options loaded from environment variables.
712///
713/// This is currently a subset of all respected environment variables, most are parsed via Clap at
714/// the CLI level, however there are limited semantics in that context.
715#[derive(Debug, Clone)]
716pub struct EnvironmentOptions {
717    pub skip_wheel_filename_check: Option<bool>,
718    pub hide_build_output: Option<bool>,
719    pub python_install_bin: Option<bool>,
720    pub python_install_registry: Option<bool>,
721    pub python_no_registry: EnvFlag,
722    pub install_mirrors: PythonInstallMirrors,
723    pub log_context: Option<bool>,
724    pub lfs: Option<bool>,
725    pub cuda_driver_version: Option<Version>,
726    pub amd_gpu_architecture: Option<AmdGpuArchitecture>,
727    pub http_connect_timeout: Duration,
728    pub http_read_timeout: Duration,
729    /// There's no upload timeout in reqwest, instead we have to use a read timeout as upload
730    /// timeout.
731    pub http_read_timeout_upload: Duration,
732    pub http_retries: u32,
733    pub concurrency: Concurrency,
734    #[cfg(feature = "tracing-durations-export")]
735    pub tracing_durations_file: Option<PathBuf>,
736    pub frozen: EnvFlag,
737    pub locked: EnvFlag,
738    pub offline: EnvFlag,
739    pub no_sync: EnvFlag,
740    pub managed_python: EnvFlag,
741    pub no_managed_python: EnvFlag,
742    pub native_tls: EnvFlag,
743    pub system_certs: EnvFlag,
744    pub preview: EnvFlag,
745    pub isolated: EnvFlag,
746    pub no_progress: EnvFlag,
747    pub no_installer_metadata: EnvFlag,
748    pub dev: EnvFlag,
749    pub no_dev: EnvFlag,
750    pub show_resolution: EnvFlag,
751    pub no_editable: EnvFlag,
752    pub no_install_project: EnvFlag,
753    pub no_install_workspace: EnvFlag,
754    pub no_install_local: EnvFlag,
755    pub only_install_project: EnvFlag,
756    pub only_install_workspace: EnvFlag,
757    pub only_install_local: EnvFlag,
758    pub no_env_file: EnvFlag,
759    pub no_group: Option<Vec<GroupName>>,
760    pub no_binary_package: Option<Vec<PackageName>>,
761    pub no_build_package: Option<Vec<PackageName>>,
762    pub no_sources_package: Option<Vec<PackageName>>,
763    pub venv_seed: EnvFlag,
764    pub venv_clear: EnvFlag,
765    pub venv_relocatable: EnvFlag,
766    pub init_bare: EnvFlag,
767    pub malware_check: EnvFlag,
768    pub malware_check_url: Option<DisplaySafeUrl>,
769}
770
771impl EnvironmentOptions {
772    /// Create a new [`EnvironmentOptions`] from environment variables.
773    pub fn new() -> Result<Self, Error> {
774        // Timeout options, matching https://doc.rust-lang.org/nightly/cargo/reference/config.html#httptimeout
775        // `UV_REQUEST_TIMEOUT` is provided for backwards compatibility with v0.1.6
776        let http_read_timeout = parse_integer_environment_variable(
777            EnvVars::UV_HTTP_TIMEOUT,
778            Some("value should be an integer number of seconds"),
779        )?
780        .or(parse_integer_environment_variable(
781            EnvVars::UV_REQUEST_TIMEOUT,
782            Some("value should be an integer number of seconds"),
783        )?)
784        .or(parse_integer_environment_variable(
785            EnvVars::HTTP_TIMEOUT,
786            Some("value should be an integer number of seconds"),
787        )?)
788        .map(Duration::from_secs);
789
790        Ok(Self {
791            skip_wheel_filename_check: parse_boolish_environment_variable(
792                EnvVars::UV_SKIP_WHEEL_FILENAME_CHECK,
793            )?,
794            hide_build_output: parse_boolish_environment_variable(EnvVars::UV_HIDE_BUILD_OUTPUT)?,
795            python_install_bin: parse_boolish_environment_variable(EnvVars::UV_PYTHON_INSTALL_BIN)?,
796            python_install_registry: parse_boolish_environment_variable(
797                EnvVars::UV_PYTHON_INSTALL_REGISTRY,
798            )?,
799            python_no_registry: EnvFlag::new(EnvVars::UV_PYTHON_NO_REGISTRY)?,
800            concurrency: Concurrency {
801                downloads: parse_integer_environment_variable(
802                    EnvVars::UV_CONCURRENT_DOWNLOADS,
803                    None,
804                )?,
805                builds: parse_integer_environment_variable(EnvVars::UV_CONCURRENT_BUILDS, None)?,
806                installs: parse_integer_environment_variable(
807                    EnvVars::UV_CONCURRENT_INSTALLS,
808                    None,
809                )?,
810            },
811            install_mirrors: PythonInstallMirrors {
812                python_install_mirror: parse_string_environment_variable(
813                    EnvVars::UV_PYTHON_INSTALL_MIRROR,
814                )?,
815                pypy_install_mirror: parse_string_environment_variable(
816                    EnvVars::UV_PYPY_INSTALL_MIRROR,
817                )?,
818                python_downloads_json_url: parse_string_environment_variable(
819                    EnvVars::UV_PYTHON_DOWNLOADS_JSON_URL,
820                )?,
821            },
822            log_context: parse_boolish_environment_variable(EnvVars::UV_LOG_CONTEXT)?,
823            lfs: parse_boolish_environment_variable(EnvVars::UV_GIT_LFS)?,
824            cuda_driver_version: parse_typed_environment_variable(
825                EnvVars::UV_CUDA_DRIVER_VERSION,
826                None,
827            )?,
828            amd_gpu_architecture: parse_typed_environment_variable(
829                EnvVars::UV_AMD_GPU_ARCHITECTURE,
830                None,
831            )?,
832            http_read_timeout_upload: parse_integer_environment_variable(
833                EnvVars::UV_UPLOAD_HTTP_TIMEOUT,
834                Some("value should be an integer number of seconds"),
835            )?
836            .map(Duration::from_secs)
837            .or(http_read_timeout)
838            .unwrap_or(DEFAULT_READ_TIMEOUT_UPLOAD),
839            http_read_timeout: http_read_timeout.unwrap_or(DEFAULT_READ_TIMEOUT),
840            http_connect_timeout: parse_integer_environment_variable(
841                EnvVars::UV_HTTP_CONNECT_TIMEOUT,
842                Some("value should be an integer number of seconds"),
843            )?
844            .map(Duration::from_secs)
845            .unwrap_or(DEFAULT_CONNECT_TIMEOUT),
846            http_retries: parse_integer_environment_variable(EnvVars::UV_HTTP_RETRIES, None)?
847                .unwrap_or(uv_client::DEFAULT_RETRIES),
848            #[cfg(feature = "tracing-durations-export")]
849            tracing_durations_file: parse_path_environment_variable(
850                EnvVars::TRACING_DURATIONS_FILE,
851            ),
852            frozen: EnvFlag::new(EnvVars::UV_FROZEN)?,
853            locked: EnvFlag::new(EnvVars::UV_LOCKED)?,
854            offline: EnvFlag::new(EnvVars::UV_OFFLINE)?,
855            no_sync: EnvFlag::new(EnvVars::UV_NO_SYNC)?,
856            managed_python: EnvFlag::new(EnvVars::UV_MANAGED_PYTHON)?,
857            no_managed_python: EnvFlag::new(EnvVars::UV_NO_MANAGED_PYTHON)?,
858            native_tls: EnvFlag::new(EnvVars::UV_NATIVE_TLS)?,
859            system_certs: EnvFlag::new(EnvVars::UV_SYSTEM_CERTS)?,
860            preview: EnvFlag::new(EnvVars::UV_PREVIEW)?,
861            isolated: EnvFlag::new(EnvVars::UV_ISOLATED)?,
862            no_progress: EnvFlag::new(EnvVars::UV_NO_PROGRESS)?,
863            no_installer_metadata: EnvFlag::new(EnvVars::UV_NO_INSTALLER_METADATA)?,
864            dev: EnvFlag::new(EnvVars::UV_DEV)?,
865            no_dev: EnvFlag::new(EnvVars::UV_NO_DEV)?,
866            show_resolution: EnvFlag::new(EnvVars::UV_SHOW_RESOLUTION)?,
867            no_editable: EnvFlag::new(EnvVars::UV_NO_EDITABLE)?,
868            no_install_project: EnvFlag::new(EnvVars::UV_NO_INSTALL_PROJECT)?,
869            no_install_workspace: EnvFlag::new(EnvVars::UV_NO_INSTALL_WORKSPACE)?,
870            no_install_local: EnvFlag::new(EnvVars::UV_NO_INSTALL_LOCAL)?,
871            only_install_project: EnvFlag::new(EnvVars::UV_ONLY_INSTALL_PROJECT)?,
872            only_install_workspace: EnvFlag::new(EnvVars::UV_ONLY_INSTALL_WORKSPACE)?,
873            only_install_local: EnvFlag::new(EnvVars::UV_ONLY_INSTALL_LOCAL)?,
874            no_env_file: EnvFlag::new(EnvVars::UV_NO_ENV_FILE)?,
875            no_group: parse_name_list_environment_variable(EnvVars::UV_NO_GROUP)?,
876            no_binary_package: parse_name_list_environment_variable(EnvVars::UV_NO_BINARY_PACKAGE)?,
877            no_build_package: parse_name_list_environment_variable(EnvVars::UV_NO_BUILD_PACKAGE)?,
878            no_sources_package: parse_name_list_environment_variable(
879                EnvVars::UV_NO_SOURCES_PACKAGE,
880            )?,
881            venv_seed: EnvFlag::new(EnvVars::UV_VENV_SEED)?,
882            venv_clear: EnvFlag::new(EnvVars::UV_VENV_CLEAR)?,
883            venv_relocatable: EnvFlag::new(EnvVars::UV_VENV_RELOCATABLE)?,
884            init_bare: EnvFlag::new(EnvVars::UV_INIT_BARE)?,
885            malware_check: EnvFlag::new(EnvVars::UV_MALWARE_CHECK)?,
886            malware_check_url: parse_string_environment_variable(EnvVars::UV_MALWARE_CHECK_URL)?
887                .map(|value| {
888                    value.parse::<DisplaySafeUrl>().map_err(|err| {
889                        Error::InvalidEnvironmentVariable(InvalidEnvironmentVariable {
890                            name: EnvVars::UV_MALWARE_CHECK_URL.to_string(),
891                            value,
892                            err: err.to_string(),
893                        })
894                    })
895                })
896                .transpose()?,
897        })
898    }
899}
900
901/// Parse a string environment variable.
902fn parse_string_environment_variable(name: &'static str) -> Result<Option<String>, Error> {
903    match std::env::var(name) {
904        Ok(v) => {
905            if v.is_empty() {
906                Ok(None)
907            } else {
908                Ok(Some(v))
909            }
910        }
911        Err(e) => match e {
912            std::env::VarError::NotPresent => Ok(None),
913            std::env::VarError::NotUnicode(err) => Err(Error::InvalidEnvironmentVariable(
914                InvalidEnvironmentVariable {
915                    name: name.to_string(),
916                    value: err.to_string_lossy().to_string(),
917                    err: "expected a valid UTF-8 string".to_string(),
918                },
919            )),
920        },
921    }
922}
923
924/// Parse an environment variable containing a whitespace-delimited list of names.
925fn parse_name_list_environment_variable<T>(name: &'static str) -> Result<Option<Vec<T>>, Error>
926where
927    T: FromStr,
928    <T as FromStr>::Err: std::fmt::Display,
929{
930    let Some(value) = parse_string_environment_variable(name)? else {
931        return Ok(None);
932    };
933
934    let names = value
935        .split_whitespace()
936        .map(|entry| {
937            entry.parse::<T>().map_err(|err| {
938                Error::InvalidEnvironmentVariable(InvalidEnvironmentVariable {
939                    name: name.to_string(),
940                    value: value.clone(),
941                    err: err.to_string(),
942                })
943            })
944        })
945        .collect::<Result<Vec<_>, _>>()?;
946
947    if names.is_empty() {
948        Ok(None)
949    } else {
950        Ok(Some(names))
951    }
952}
953
954fn parse_typed_environment_variable<T>(
955    name: &'static str,
956    help: Option<&str>,
957) -> Result<Option<T>, Error>
958where
959    T: std::str::FromStr,
960    <T as std::str::FromStr>::Err: std::fmt::Display,
961{
962    let value = match std::env::var(name) {
963        Ok(v) => v,
964        Err(e) => {
965            return match e {
966                std::env::VarError::NotPresent => Ok(None),
967                std::env::VarError::NotUnicode(err) => Err(Error::InvalidEnvironmentVariable(
968                    InvalidEnvironmentVariable {
969                        name: name.to_string(),
970                        value: err.to_string_lossy().to_string(),
971                        err: "expected a valid UTF-8 string".to_string(),
972                    },
973                )),
974            };
975        }
976    };
977    if value.is_empty() {
978        return Ok(None);
979    }
980
981    match value.parse::<T>() {
982        Ok(v) => Ok(Some(v)),
983        Err(err) => Err(Error::InvalidEnvironmentVariable(
984            InvalidEnvironmentVariable {
985                name: name.to_string(),
986                value,
987                err: if let Some(help) = help {
988                    format!("{err}; {help}")
989                } else {
990                    err.to_string()
991                },
992            },
993        )),
994    }
995}
996
997fn parse_integer_environment_variable<T>(
998    name: &'static str,
999    help: Option<&str>,
1000) -> Result<Option<T>, Error>
1001where
1002    T: std::str::FromStr + Copy,
1003    <T as std::str::FromStr>::Err: std::fmt::Display,
1004{
1005    let value = match std::env::var(name) {
1006        Ok(v) => v,
1007        Err(e) => {
1008            return match e {
1009                std::env::VarError::NotPresent => Ok(None),
1010                std::env::VarError::NotUnicode(err) => Err(Error::InvalidEnvironmentVariable(
1011                    InvalidEnvironmentVariable {
1012                        name: name.to_string(),
1013                        value: err.to_string_lossy().to_string(),
1014                        err: "expected a valid UTF-8 string".to_string(),
1015                    },
1016                )),
1017            };
1018        }
1019    };
1020    if value.is_empty() {
1021        return Ok(None);
1022    }
1023
1024    match value.parse::<T>() {
1025        Ok(v) => Ok(Some(v)),
1026        Err(err) => Err(Error::InvalidEnvironmentVariable(
1027            InvalidEnvironmentVariable {
1028                name: name.to_string(),
1029                value,
1030                err: if let Some(help) = help {
1031                    format!("{err}; {help}")
1032                } else {
1033                    err.to_string()
1034                },
1035            },
1036        )),
1037    }
1038}
1039
1040#[cfg(feature = "tracing-durations-export")]
1041/// Parse a path environment variable.
1042fn parse_path_environment_variable(name: &'static str) -> Option<PathBuf> {
1043    let value = std::env::var_os(name)?;
1044
1045    if value.is_empty() {
1046        return None;
1047    }
1048
1049    Some(PathBuf::from(value))
1050}
1051
1052/// Populate the [`EnvironmentFlags`] from the given [`EnvironmentOptions`].
1053impl From<&EnvironmentOptions> for EnvironmentFlags {
1054    fn from(options: &EnvironmentOptions) -> Self {
1055        let mut flags = Self::empty();
1056        if options.skip_wheel_filename_check == Some(true) {
1057            flags.insert(Self::SKIP_WHEEL_FILENAME_CHECK);
1058        }
1059        if options.hide_build_output == Some(true) {
1060            flags.insert(Self::HIDE_BUILD_OUTPUT);
1061        }
1062        flags
1063    }
1064}