uv_settings/
lib.rs

1use std::num::NonZeroUsize;
2use std::ops::Deref;
3use std::path::{Path, PathBuf};
4use std::time::Duration;
5
6use uv_dirs::{system_config_file, user_config_dir};
7use uv_flags::EnvironmentFlags;
8use uv_fs::Simplified;
9use uv_static::EnvVars;
10use uv_warnings::warn_user;
11
12pub use crate::combine::*;
13pub use crate::settings::*;
14
15mod combine;
16mod settings;
17
18/// The [`Options`] as loaded from a configuration file on disk.
19#[derive(Debug, Clone)]
20pub struct FilesystemOptions(Options);
21
22impl FilesystemOptions {
23    /// Convert the [`FilesystemOptions`] into [`Options`].
24    pub fn into_options(self) -> Options {
25        self.0
26    }
27}
28
29impl Deref for FilesystemOptions {
30    type Target = Options;
31
32    fn deref(&self) -> &Self::Target {
33        &self.0
34    }
35}
36
37impl FilesystemOptions {
38    /// Load the user [`FilesystemOptions`].
39    pub fn user() -> Result<Option<Self>, Error> {
40        let Some(dir) = user_config_dir() else {
41            return Ok(None);
42        };
43        let root = dir.join("uv");
44        let file = root.join("uv.toml");
45
46        tracing::debug!("Searching for user configuration in: `{}`", file.display());
47        match read_file(&file) {
48            Ok(options) => {
49                tracing::debug!("Found user configuration in: `{}`", file.display());
50                validate_uv_toml(&file, &options)?;
51                Ok(Some(Self(options)))
52            }
53            Err(Error::Io(err))
54                if matches!(
55                    err.kind(),
56                    std::io::ErrorKind::NotFound
57                        | std::io::ErrorKind::NotADirectory
58                        | std::io::ErrorKind::PermissionDenied
59                ) =>
60            {
61                Ok(None)
62            }
63            Err(err) => Err(err),
64        }
65    }
66
67    pub fn system() -> Result<Option<Self>, Error> {
68        let Some(file) = system_config_file() else {
69            return Ok(None);
70        };
71
72        tracing::debug!("Found system configuration in: `{}`", file.display());
73        let options = read_file(&file)?;
74        validate_uv_toml(&file, &options)?;
75        Ok(Some(Self(options)))
76    }
77
78    /// Find the [`FilesystemOptions`] for the given path.
79    ///
80    /// The search starts at the given path and goes up the directory tree until a `uv.toml` file or
81    /// `pyproject.toml` file is found.
82    pub fn find(path: &Path) -> Result<Option<Self>, Error> {
83        for ancestor in path.ancestors() {
84            match Self::from_directory(ancestor) {
85                Ok(Some(options)) => {
86                    return Ok(Some(options));
87                }
88                Ok(None) => {
89                    // Continue traversing the directory tree.
90                }
91                Err(Error::PyprojectToml(path, err)) => {
92                    // If we see an invalid `pyproject.toml`, warn but continue.
93                    warn_user!(
94                        "Failed to parse `{}` during settings discovery:\n{}",
95                        path.user_display().cyan(),
96                        textwrap::indent(&err.to_string(), "  ")
97                    );
98                }
99                Err(err) => {
100                    // Otherwise, warn and stop.
101                    return Err(err);
102                }
103            }
104        }
105        Ok(None)
106    }
107
108    /// Load a [`FilesystemOptions`] from a directory, preferring a `uv.toml` file over a
109    /// `pyproject.toml` file.
110    pub fn from_directory(dir: &Path) -> Result<Option<Self>, Error> {
111        // Read a `uv.toml` file in the current directory.
112        let path = dir.join("uv.toml");
113        match fs_err::read_to_string(&path) {
114            Ok(content) => {
115                let options = toml::from_str::<Options>(&content)
116                    .map_err(|err| Error::UvToml(path.clone(), Box::new(err)))?
117                    .relative_to(&std::path::absolute(dir)?)?;
118
119                // If the directory also contains a `[tool.uv]` table in a `pyproject.toml` file,
120                // warn.
121                let pyproject = dir.join("pyproject.toml");
122                if let Some(pyproject) = fs_err::read_to_string(pyproject)
123                    .ok()
124                    .and_then(|content| toml::from_str::<PyProjectToml>(&content).ok())
125                {
126                    if let Some(options) = pyproject.tool.as_ref().and_then(|tool| tool.uv.as_ref())
127                    {
128                        warn_uv_toml_masked_fields(options);
129                    }
130                }
131
132                tracing::debug!("Found workspace configuration at `{}`", path.display());
133                validate_uv_toml(&path, &options)?;
134                return Ok(Some(Self(options)));
135            }
136            Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
137            Err(err) => return Err(err.into()),
138        }
139
140        // Read a `pyproject.toml` file in the current directory.
141        let path = dir.join("pyproject.toml");
142        match fs_err::read_to_string(&path) {
143            Ok(content) => {
144                // Parse, but skip any `pyproject.toml` that doesn't have a `[tool.uv]` section.
145                let pyproject: PyProjectToml = toml::from_str(&content)
146                    .map_err(|err| Error::PyprojectToml(path.clone(), Box::new(err)))?;
147                let Some(tool) = pyproject.tool else {
148                    tracing::debug!(
149                        "Skipping `pyproject.toml` in `{}` (no `[tool]` section)",
150                        dir.display()
151                    );
152                    return Ok(None);
153                };
154                let Some(options) = tool.uv else {
155                    tracing::debug!(
156                        "Skipping `pyproject.toml` in `{}` (no `[tool.uv]` section)",
157                        dir.display()
158                    );
159                    return Ok(None);
160                };
161
162                let options = options.relative_to(&std::path::absolute(dir)?)?;
163
164                tracing::debug!("Found workspace configuration at `{}`", path.display());
165                return Ok(Some(Self(options)));
166            }
167            Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
168            Err(err) => return Err(err.into()),
169        }
170
171        Ok(None)
172    }
173
174    /// Load a [`FilesystemOptions`] from a `uv.toml` file.
175    pub fn from_file(path: impl AsRef<Path>) -> Result<Self, Error> {
176        let path = path.as_ref();
177        tracing::debug!("Reading user configuration from: `{}`", path.display());
178
179        let options = read_file(path)?;
180        validate_uv_toml(path, &options)?;
181        Ok(Self(options))
182    }
183}
184
185impl From<Options> for FilesystemOptions {
186    fn from(options: Options) -> Self {
187        Self(options)
188    }
189}
190
191/// Load [`Options`] from a `uv.toml` file.
192fn read_file(path: &Path) -> Result<Options, Error> {
193    let content = fs_err::read_to_string(path)?;
194    let options = toml::from_str::<Options>(&content)
195        .map_err(|err| Error::UvToml(path.to_path_buf(), Box::new(err)))?;
196    let options = if let Some(parent) = std::path::absolute(path)?.parent() {
197        options.relative_to(parent)?
198    } else {
199        options
200    };
201    Ok(options)
202}
203
204/// Validate that an [`Options`] schema is compatible with `uv.toml`.
205fn validate_uv_toml(path: &Path, options: &Options) -> Result<(), Error> {
206    let Options {
207        globals: _,
208        top_level: _,
209        install_mirrors: _,
210        publish: _,
211        add: _,
212        pip: _,
213        cache_keys: _,
214        override_dependencies: _,
215        exclude_dependencies: _,
216        constraint_dependencies: _,
217        build_constraint_dependencies: _,
218        environments,
219        required_environments,
220        conflicts,
221        workspace,
222        sources,
223        dev_dependencies,
224        default_groups,
225        dependency_groups,
226        managed,
227        package,
228        build_backend,
229    } = options;
230    // The `uv.toml` format is not allowed to include any of the following, which are
231    // permitted by the schema since they _can_ be included in `pyproject.toml` files
232    // (and we want to use `deny_unknown_fields`).
233    if conflicts.is_some() {
234        return Err(Error::PyprojectOnlyField(path.to_path_buf(), "conflicts"));
235    }
236    if workspace.is_some() {
237        return Err(Error::PyprojectOnlyField(path.to_path_buf(), "workspace"));
238    }
239    if sources.is_some() {
240        return Err(Error::PyprojectOnlyField(path.to_path_buf(), "sources"));
241    }
242    if dev_dependencies.is_some() {
243        return Err(Error::PyprojectOnlyField(
244            path.to_path_buf(),
245            "dev-dependencies",
246        ));
247    }
248    if default_groups.is_some() {
249        return Err(Error::PyprojectOnlyField(
250            path.to_path_buf(),
251            "default-groups",
252        ));
253    }
254    if dependency_groups.is_some() {
255        return Err(Error::PyprojectOnlyField(
256            path.to_path_buf(),
257            "dependency-groups",
258        ));
259    }
260    if managed.is_some() {
261        return Err(Error::PyprojectOnlyField(path.to_path_buf(), "managed"));
262    }
263    if package.is_some() {
264        return Err(Error::PyprojectOnlyField(path.to_path_buf(), "package"));
265    }
266    if build_backend.is_some() {
267        return Err(Error::PyprojectOnlyField(
268            path.to_path_buf(),
269            "build-backend",
270        ));
271    }
272    if environments.is_some() {
273        return Err(Error::PyprojectOnlyField(
274            path.to_path_buf(),
275            "environments",
276        ));
277    }
278    if required_environments.is_some() {
279        return Err(Error::PyprojectOnlyField(
280            path.to_path_buf(),
281            "required-environments",
282        ));
283    }
284    Ok(())
285}
286
287/// Validate that an [`Options`] contains no fields that `uv.toml` would mask
288///
289/// This is essentially the inverse of [`validate_uv_toml`].
290fn warn_uv_toml_masked_fields(options: &Options) {
291    let Options {
292        globals:
293            GlobalOptions {
294                required_version,
295                native_tls,
296                offline,
297                no_cache,
298                cache_dir,
299                preview,
300                python_preference,
301                python_downloads,
302                concurrent_downloads,
303                concurrent_builds,
304                concurrent_installs,
305                allow_insecure_host,
306                http_proxy,
307                https_proxy,
308                no_proxy,
309            },
310        top_level:
311            ResolverInstallerSchema {
312                index,
313                index_url,
314                extra_index_url,
315                no_index,
316                find_links,
317                index_strategy,
318                keyring_provider,
319                resolution,
320                prerelease,
321                fork_strategy,
322                dependency_metadata,
323                config_settings,
324                config_settings_package,
325                no_build_isolation,
326                no_build_isolation_package,
327                extra_build_dependencies,
328                extra_build_variables,
329                exclude_newer,
330                exclude_newer_package,
331                link_mode,
332                compile_bytecode,
333                no_sources,
334                upgrade,
335                upgrade_package,
336                reinstall,
337                reinstall_package,
338                no_build,
339                no_build_package,
340                no_binary,
341                no_binary_package,
342                torch_backend,
343            },
344        install_mirrors:
345            PythonInstallMirrors {
346                python_install_mirror,
347                pypy_install_mirror,
348                python_downloads_json_url,
349            },
350        publish:
351            PublishOptions {
352                publish_url,
353                trusted_publishing,
354                check_url,
355            },
356        add: AddOptions { add_bounds },
357        pip,
358        cache_keys,
359        override_dependencies,
360        exclude_dependencies,
361        constraint_dependencies,
362        build_constraint_dependencies,
363        environments: _,
364        required_environments: _,
365        conflicts: _,
366        workspace: _,
367        sources: _,
368        dev_dependencies: _,
369        default_groups: _,
370        dependency_groups: _,
371        managed: _,
372        package: _,
373        build_backend: _,
374    } = options;
375
376    let mut masked_fields = vec![];
377
378    if required_version.is_some() {
379        masked_fields.push("required-version");
380    }
381    if native_tls.is_some() {
382        masked_fields.push("native-tls");
383    }
384    if offline.is_some() {
385        masked_fields.push("offline");
386    }
387    if no_cache.is_some() {
388        masked_fields.push("no-cache");
389    }
390    if cache_dir.is_some() {
391        masked_fields.push("cache-dir");
392    }
393    if preview.is_some() {
394        masked_fields.push("preview");
395    }
396    if python_preference.is_some() {
397        masked_fields.push("python-preference");
398    }
399    if python_downloads.is_some() {
400        masked_fields.push("python-downloads");
401    }
402    if concurrent_downloads.is_some() {
403        masked_fields.push("concurrent-downloads");
404    }
405    if concurrent_builds.is_some() {
406        masked_fields.push("concurrent-builds");
407    }
408    if concurrent_installs.is_some() {
409        masked_fields.push("concurrent-installs");
410    }
411    if allow_insecure_host.is_some() {
412        masked_fields.push("allow-insecure-host");
413    }
414    if http_proxy.is_some() {
415        masked_fields.push("http-proxy");
416    }
417    if https_proxy.is_some() {
418        masked_fields.push("https-proxy");
419    }
420    if no_proxy.is_some() {
421        masked_fields.push("no-proxy");
422    }
423    if index.is_some() {
424        masked_fields.push("index");
425    }
426    if index_url.is_some() {
427        masked_fields.push("index-url");
428    }
429    if extra_index_url.is_some() {
430        masked_fields.push("extra-index-url");
431    }
432    if no_index.is_some() {
433        masked_fields.push("no-index");
434    }
435    if find_links.is_some() {
436        masked_fields.push("find-links");
437    }
438    if index_strategy.is_some() {
439        masked_fields.push("index-strategy");
440    }
441    if keyring_provider.is_some() {
442        masked_fields.push("keyring-provider");
443    }
444    if resolution.is_some() {
445        masked_fields.push("resolution");
446    }
447    if prerelease.is_some() {
448        masked_fields.push("prerelease");
449    }
450    if fork_strategy.is_some() {
451        masked_fields.push("fork-strategy");
452    }
453    if dependency_metadata.is_some() {
454        masked_fields.push("dependency-metadata");
455    }
456    if config_settings.is_some() {
457        masked_fields.push("config-settings");
458    }
459    if config_settings_package.is_some() {
460        masked_fields.push("config-settings-package");
461    }
462    if no_build_isolation.is_some() {
463        masked_fields.push("no-build-isolation");
464    }
465    if no_build_isolation_package.is_some() {
466        masked_fields.push("no-build-isolation-package");
467    }
468    if extra_build_dependencies.is_some() {
469        masked_fields.push("extra-build-dependencies");
470    }
471    if extra_build_variables.is_some() {
472        masked_fields.push("extra-build-variables");
473    }
474    if exclude_newer.is_some() {
475        masked_fields.push("exclude-newer");
476    }
477    if exclude_newer_package.is_some() {
478        masked_fields.push("exclude-newer-package");
479    }
480    if link_mode.is_some() {
481        masked_fields.push("link-mode");
482    }
483    if compile_bytecode.is_some() {
484        masked_fields.push("compile-bytecode");
485    }
486    if no_sources.is_some() {
487        masked_fields.push("no-sources");
488    }
489    if upgrade.is_some() {
490        masked_fields.push("upgrade");
491    }
492    if upgrade_package.is_some() {
493        masked_fields.push("upgrade-package");
494    }
495    if reinstall.is_some() {
496        masked_fields.push("reinstall");
497    }
498    if reinstall_package.is_some() {
499        masked_fields.push("reinstall-package");
500    }
501    if no_build.is_some() {
502        masked_fields.push("no-build");
503    }
504    if no_build_package.is_some() {
505        masked_fields.push("no-build-package");
506    }
507    if no_binary.is_some() {
508        masked_fields.push("no-binary");
509    }
510    if no_binary_package.is_some() {
511        masked_fields.push("no-binary-package");
512    }
513    if torch_backend.is_some() {
514        masked_fields.push("torch-backend");
515    }
516    if python_install_mirror.is_some() {
517        masked_fields.push("python-install-mirror");
518    }
519    if pypy_install_mirror.is_some() {
520        masked_fields.push("pypy-install-mirror");
521    }
522    if python_downloads_json_url.is_some() {
523        masked_fields.push("python-downloads-json-url");
524    }
525    if publish_url.is_some() {
526        masked_fields.push("publish-url");
527    }
528    if trusted_publishing.is_some() {
529        masked_fields.push("trusted-publishing");
530    }
531    if check_url.is_some() {
532        masked_fields.push("check-url");
533    }
534    if add_bounds.is_some() {
535        masked_fields.push("add-bounds");
536    }
537    if pip.is_some() {
538        masked_fields.push("pip");
539    }
540    if cache_keys.is_some() {
541        masked_fields.push("cache_keys");
542    }
543    if override_dependencies.is_some() {
544        masked_fields.push("override-dependencies");
545    }
546    if exclude_dependencies.is_some() {
547        masked_fields.push("exclude-dependencies");
548    }
549    if constraint_dependencies.is_some() {
550        masked_fields.push("constraint-dependencies");
551    }
552    if build_constraint_dependencies.is_some() {
553        masked_fields.push("build-constraint-dependencies");
554    }
555    if !masked_fields.is_empty() {
556        let field_listing = masked_fields.join("\n- ");
557        warn_user!(
558            "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- {}",
559            field_listing,
560        );
561    }
562}
563
564#[derive(thiserror::Error, Debug)]
565pub enum Error {
566    #[error(transparent)]
567    Io(#[from] std::io::Error),
568
569    #[error(transparent)]
570    Index(#[from] uv_distribution_types::IndexUrlError),
571
572    #[error("Failed to parse: `{}`", _0.user_display())]
573    PyprojectToml(PathBuf, #[source] Box<toml::de::Error>),
574
575    #[error("Failed to parse: `{}`", _0.user_display())]
576    UvToml(PathBuf, #[source] Box<toml::de::Error>),
577
578    #[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
579    )]
580    PyprojectOnlyField(PathBuf, &'static str),
581
582    #[error("Failed to parse environment variable `{name}` with invalid value `{value}`: {err}")]
583    InvalidEnvironmentVariable {
584        name: String,
585        value: String,
586        err: String,
587    },
588}
589
590#[derive(Copy, Clone, Debug)]
591pub struct Concurrency {
592    pub downloads: Option<NonZeroUsize>,
593    pub builds: Option<NonZeroUsize>,
594    pub installs: Option<NonZeroUsize>,
595}
596
597/// A boolean flag parsed from an environment variable.
598///
599/// Stores both the value and the environment variable name for use in error messages.
600#[derive(Debug, Clone, Copy)]
601pub struct EnvFlag {
602    pub value: Option<bool>,
603    pub env_var: &'static str,
604}
605
606impl EnvFlag {
607    /// Create a new [`EnvFlag`] by parsing the given environment variable.
608    pub fn new(env_var: &'static str) -> Result<Self, Error> {
609        Ok(Self {
610            value: parse_boolish_environment_variable(env_var)?,
611            env_var,
612        })
613    }
614}
615
616/// Options loaded from environment variables.
617///
618/// This is currently a subset of all respected environment variables, most are parsed via Clap at
619/// the CLI level, however there are limited semantics in that context.
620#[derive(Debug, Clone)]
621pub struct EnvironmentOptions {
622    pub skip_wheel_filename_check: Option<bool>,
623    pub hide_build_output: Option<bool>,
624    pub python_install_bin: Option<bool>,
625    pub python_install_registry: Option<bool>,
626    pub install_mirrors: PythonInstallMirrors,
627    pub log_context: Option<bool>,
628    pub lfs: Option<bool>,
629    pub http_timeout: Duration,
630    pub http_retries: u32,
631    pub upload_http_timeout: Duration,
632    pub concurrency: Concurrency,
633    #[cfg(feature = "tracing-durations-export")]
634    pub tracing_durations_file: Option<PathBuf>,
635    pub frozen: EnvFlag,
636    pub locked: EnvFlag,
637    pub offline: EnvFlag,
638    pub no_sync: EnvFlag,
639    pub managed_python: EnvFlag,
640    pub no_managed_python: EnvFlag,
641    pub native_tls: EnvFlag,
642    pub preview: EnvFlag,
643    pub isolated: EnvFlag,
644    pub no_progress: EnvFlag,
645    pub no_installer_metadata: EnvFlag,
646    pub dev: EnvFlag,
647    pub no_dev: EnvFlag,
648    pub show_resolution: EnvFlag,
649    pub no_editable: EnvFlag,
650    pub no_env_file: EnvFlag,
651    pub venv_seed: EnvFlag,
652    pub venv_clear: EnvFlag,
653}
654
655impl EnvironmentOptions {
656    /// Create a new [`EnvironmentOptions`] from environment variables.
657    pub fn new() -> Result<Self, Error> {
658        // Timeout options, matching https://doc.rust-lang.org/nightly/cargo/reference/config.html#httptimeout
659        // `UV_REQUEST_TIMEOUT` is provided for backwards compatibility with v0.1.6
660        let http_timeout = parse_integer_environment_variable(EnvVars::UV_HTTP_TIMEOUT)?
661            .or(parse_integer_environment_variable(
662                EnvVars::UV_REQUEST_TIMEOUT,
663            )?)
664            .or(parse_integer_environment_variable(EnvVars::HTTP_TIMEOUT)?)
665            .map(Duration::from_secs);
666
667        Ok(Self {
668            skip_wheel_filename_check: parse_boolish_environment_variable(
669                EnvVars::UV_SKIP_WHEEL_FILENAME_CHECK,
670            )?,
671            hide_build_output: parse_boolish_environment_variable(EnvVars::UV_HIDE_BUILD_OUTPUT)?,
672            python_install_bin: parse_boolish_environment_variable(EnvVars::UV_PYTHON_INSTALL_BIN)?,
673            python_install_registry: parse_boolish_environment_variable(
674                EnvVars::UV_PYTHON_INSTALL_REGISTRY,
675            )?,
676            concurrency: Concurrency {
677                downloads: parse_integer_environment_variable(EnvVars::UV_CONCURRENT_DOWNLOADS)?,
678                builds: parse_integer_environment_variable(EnvVars::UV_CONCURRENT_BUILDS)?,
679                installs: parse_integer_environment_variable(EnvVars::UV_CONCURRENT_INSTALLS)?,
680            },
681            install_mirrors: PythonInstallMirrors {
682                python_install_mirror: parse_string_environment_variable(
683                    EnvVars::UV_PYTHON_INSTALL_MIRROR,
684                )?,
685                pypy_install_mirror: parse_string_environment_variable(
686                    EnvVars::UV_PYPY_INSTALL_MIRROR,
687                )?,
688                python_downloads_json_url: parse_string_environment_variable(
689                    EnvVars::UV_PYTHON_DOWNLOADS_JSON_URL,
690                )?,
691            },
692            log_context: parse_boolish_environment_variable(EnvVars::UV_LOG_CONTEXT)?,
693            lfs: parse_boolish_environment_variable(EnvVars::UV_GIT_LFS)?,
694            upload_http_timeout: parse_integer_environment_variable(
695                EnvVars::UV_UPLOAD_HTTP_TIMEOUT,
696            )?
697            .map(Duration::from_secs)
698            .or(http_timeout)
699            .unwrap_or(Duration::from_secs(15 * 60)),
700            http_timeout: http_timeout.unwrap_or(Duration::from_secs(30)),
701            http_retries: parse_integer_environment_variable(EnvVars::UV_HTTP_RETRIES)?
702                .unwrap_or(uv_client::DEFAULT_RETRIES),
703            #[cfg(feature = "tracing-durations-export")]
704            tracing_durations_file: parse_path_environment_variable(
705                EnvVars::TRACING_DURATIONS_FILE,
706            ),
707            frozen: EnvFlag::new(EnvVars::UV_FROZEN)?,
708            locked: EnvFlag::new(EnvVars::UV_LOCKED)?,
709            offline: EnvFlag::new(EnvVars::UV_OFFLINE)?,
710            no_sync: EnvFlag::new(EnvVars::UV_NO_SYNC)?,
711            managed_python: EnvFlag::new(EnvVars::UV_MANAGED_PYTHON)?,
712            no_managed_python: EnvFlag::new(EnvVars::UV_NO_MANAGED_PYTHON)?,
713            native_tls: EnvFlag::new(EnvVars::UV_NATIVE_TLS)?,
714            preview: EnvFlag::new(EnvVars::UV_PREVIEW)?,
715            isolated: EnvFlag::new(EnvVars::UV_ISOLATED)?,
716            no_progress: EnvFlag::new(EnvVars::UV_NO_PROGRESS)?,
717            no_installer_metadata: EnvFlag::new(EnvVars::UV_NO_INSTALLER_METADATA)?,
718            dev: EnvFlag::new(EnvVars::UV_DEV)?,
719            no_dev: EnvFlag::new(EnvVars::UV_NO_DEV)?,
720            show_resolution: EnvFlag::new(EnvVars::UV_SHOW_RESOLUTION)?,
721            no_editable: EnvFlag::new(EnvVars::UV_NO_EDITABLE)?,
722            no_env_file: EnvFlag::new(EnvVars::UV_NO_ENV_FILE)?,
723            venv_seed: EnvFlag::new(EnvVars::UV_VENV_SEED)?,
724            venv_clear: EnvFlag::new(EnvVars::UV_VENV_CLEAR)?,
725        })
726    }
727}
728
729/// Parse a boolean environment variable.
730///
731/// Adapted from Clap's `BoolishValueParser` which is dual licensed under the MIT and Apache-2.0.
732pub fn parse_boolish_environment_variable(name: &'static str) -> Result<Option<bool>, Error> {
733    // See `clap_builder/src/util/str_to_bool.rs`
734    // We want to match Clap's accepted values
735
736    // True values are `y`, `yes`, `t`, `true`, `on`, and `1`.
737    const TRUE_LITERALS: [&str; 6] = ["y", "yes", "t", "true", "on", "1"];
738
739    // False values are `n`, `no`, `f`, `false`, `off`, and `0`.
740    const FALSE_LITERALS: [&str; 6] = ["n", "no", "f", "false", "off", "0"];
741
742    // Converts a string literal representation of truth to true or false.
743    //
744    // `false` values are `n`, `no`, `f`, `false`, `off`, and `0` (case insensitive).
745    //
746    // Any other value will be considered as `true`.
747    fn str_to_bool(val: impl AsRef<str>) -> Option<bool> {
748        let pat: &str = &val.as_ref().to_lowercase();
749        if TRUE_LITERALS.contains(&pat) {
750            Some(true)
751        } else if FALSE_LITERALS.contains(&pat) {
752            Some(false)
753        } else {
754            None
755        }
756    }
757
758    let Some(value) = std::env::var_os(name) else {
759        return Ok(None);
760    };
761
762    let Some(value) = value.to_str() else {
763        return Err(Error::InvalidEnvironmentVariable {
764            name: name.to_string(),
765            value: value.to_string_lossy().to_string(),
766            err: "expected a valid UTF-8 string".to_string(),
767        });
768    };
769
770    let Some(value) = str_to_bool(value) else {
771        return Err(Error::InvalidEnvironmentVariable {
772            name: name.to_string(),
773            value: value.to_string(),
774            err: "expected a boolish value".to_string(),
775        });
776    };
777
778    Ok(Some(value))
779}
780
781/// Parse a string environment variable.
782fn parse_string_environment_variable(name: &'static str) -> Result<Option<String>, Error> {
783    match std::env::var(name) {
784        Ok(v) => {
785            if v.is_empty() {
786                Ok(None)
787            } else {
788                Ok(Some(v))
789            }
790        }
791        Err(e) => match e {
792            std::env::VarError::NotPresent => Ok(None),
793            std::env::VarError::NotUnicode(err) => Err(Error::InvalidEnvironmentVariable {
794                name: name.to_string(),
795                value: err.to_string_lossy().to_string(),
796                err: "expected a valid UTF-8 string".to_string(),
797            }),
798        },
799    }
800}
801
802fn parse_integer_environment_variable<T>(name: &'static str) -> Result<Option<T>, Error>
803where
804    T: std::str::FromStr + Copy,
805    <T as std::str::FromStr>::Err: std::fmt::Display,
806{
807    let value = match std::env::var(name) {
808        Ok(v) => v,
809        Err(e) => {
810            return match e {
811                std::env::VarError::NotPresent => Ok(None),
812                std::env::VarError::NotUnicode(err) => Err(Error::InvalidEnvironmentVariable {
813                    name: name.to_string(),
814                    value: err.to_string_lossy().to_string(),
815                    err: "expected a valid UTF-8 string".to_string(),
816                }),
817            };
818        }
819    };
820    if value.is_empty() {
821        return Ok(None);
822    }
823
824    match value.parse::<T>() {
825        Ok(v) => Ok(Some(v)),
826        Err(err) => Err(Error::InvalidEnvironmentVariable {
827            name: name.to_string(),
828            value,
829            err: err.to_string(),
830        }),
831    }
832}
833
834#[cfg(feature = "tracing-durations-export")]
835/// Parse a path environment variable.
836fn parse_path_environment_variable(name: &'static str) -> Option<PathBuf> {
837    let value = std::env::var_os(name)?;
838
839    if value.is_empty() {
840        return None;
841    }
842
843    Some(PathBuf::from(value))
844}
845
846/// Populate the [`EnvironmentFlags`] from the given [`EnvironmentOptions`].
847impl From<&EnvironmentOptions> for EnvironmentFlags {
848    fn from(options: &EnvironmentOptions) -> Self {
849        let mut flags = Self::empty();
850        if options.skip_wheel_filename_check == Some(true) {
851            flags.insert(Self::SKIP_WHEEL_FILENAME_CHECK);
852        }
853        if options.hide_build_output == Some(true) {
854            flags.insert(Self::HIDE_BUILD_OUTPUT);
855        }
856        flags
857    }
858}