1use std::num::NonZeroUsize;
2use std::ops::Deref;
3use std::path::{Path, PathBuf};
4use std::time::Duration;
5use tracing::info_span;
6use uv_client::{DEFAULT_CONNECT_TIMEOUT, DEFAULT_READ_TIMEOUT, DEFAULT_READ_TIMEOUT_UPLOAD};
7use uv_dirs::{system_config_file, user_config_dir};
8use uv_distribution_types::Origin;
9use uv_flags::EnvironmentFlags;
10use uv_fs::Simplified;
11use uv_static::{EnvVars, InvalidEnvironmentVariable, parse_boolish_environment_variable};
12use uv_warnings::warn_user;
13
14pub use crate::combine::*;
15pub use crate::settings::*;
16
17mod combine;
18mod settings;
19
20#[derive(Debug, Clone)]
22pub struct FilesystemOptions(Options);
23
24impl FilesystemOptions {
25 pub fn into_options(self) -> Options {
27 self.0
28 }
29
30 #[must_use]
32 pub fn with_origin(self, origin: Origin) -> Self {
33 Self(self.0.with_origin(origin))
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 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 let Some(file) = system_config_file() else {
77 return Ok(None);
78 };
79
80 tracing::debug!("Found system configuration in: `{}`", file.display());
81 let options = read_file(&file)?;
82 validate_uv_toml(&file, &options)?;
83 Ok(Some(Self(options.with_origin(Origin::System))))
84 }
85
86 pub fn find(path: &Path) -> Result<Option<Self>, Error> {
91 for ancestor in path.ancestors() {
92 match Self::from_directory(ancestor) {
93 Ok(Some(options)) => {
94 return Ok(Some(options));
95 }
96 Ok(None) => {
97 }
99 Err(Error::PyprojectToml(path, err)) => {
100 warn_user!(
102 "Failed to parse `{}` during settings discovery:\n{}",
103 path.user_display().cyan(),
104 textwrap::indent(&err.to_string(), " ")
105 );
106 }
107 Err(err) => {
108 return Err(err);
110 }
111 }
112 }
113 Ok(None)
114 }
115
116 pub fn from_directory(dir: &Path) -> Result<Option<Self>, Error> {
119 let path = dir.join("uv.toml");
121 match fs_err::read_to_string(&path) {
122 Ok(content) => {
123 let options =
124 info_span!("toml::from_str filesystem options uv.toml", path = %path.display())
125 .in_scope(|| toml::from_str::<Options>(&content))
126 .map_err(|err| Error::UvToml(path.clone(), Box::new(err)))?
127 .relative_to(&std::path::absolute(dir)?)?;
128
129 let pyproject = dir.join("pyproject.toml");
132 if let Ok(content) = fs_err::read_to_string(&pyproject) {
133 let result = info_span!("toml::from_str filesystem options pyproject.toml", path = %pyproject.display())
134 .in_scope(|| toml::from_str::<PyProjectToml>(&content)).ok();
135 if let Some(options) =
136 result.and_then(|pyproject| pyproject.tool.and_then(|tool| tool.uv))
137 {
138 warn_uv_toml_masked_fields(&options);
139 }
140 }
141
142 tracing::debug!("Found workspace configuration at `{}`", path.display());
143 validate_uv_toml(&path, &options)?;
144 return Ok(Some(Self(options.with_origin(Origin::Project))));
145 }
146 Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
147 Err(err) => return Err(err.into()),
148 }
149
150 let path = dir.join("pyproject.toml");
152 match fs_err::read_to_string(&path) {
153 Ok(content) => {
154 let pyproject =
156 info_span!("toml::from_str filesystem options pyproject.toml", path = %path.display())
157 .in_scope(|| toml::from_str::<PyProjectToml>(&content))
158 .map_err(|err| Error::PyprojectToml(path.clone(), Box::new(err)))?;
159 let Some(tool) = pyproject.tool else {
160 tracing::debug!(
161 "Skipping `pyproject.toml` in `{}` (no `[tool]` section)",
162 dir.display()
163 );
164 return Ok(None);
165 };
166 let Some(options) = tool.uv else {
167 tracing::debug!(
168 "Skipping `pyproject.toml` in `{}` (no `[tool.uv]` section)",
169 dir.display()
170 );
171 return Ok(None);
172 };
173
174 let options = options.relative_to(&std::path::absolute(dir)?)?;
175
176 tracing::debug!("Found workspace configuration at `{}`", path.display());
177 return Ok(Some(Self(options)));
178 }
179 Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
180 Err(err) => return Err(err.into()),
181 }
182
183 Ok(None)
184 }
185
186 pub fn from_file(path: impl AsRef<Path>) -> Result<Self, Error> {
188 let path = path.as_ref();
189 tracing::debug!("Reading user configuration from: `{}`", path.display());
190
191 let options = read_file(path)?;
192 validate_uv_toml(path, &options)?;
193 Ok(Self(options))
194 }
195}
196
197impl From<Options> for FilesystemOptions {
198 fn from(options: Options) -> Self {
199 Self(options)
200 }
201}
202
203fn read_file(path: &Path) -> Result<Options, Error> {
205 let content = fs_err::read_to_string(path)?;
206 let options = info_span!("toml::from_str filesystem options uv.toml", path = %path.display())
207 .in_scope(|| toml::from_str::<Options>(&content))
208 .map_err(|err| Error::UvToml(path.to_path_buf(), Box::new(err)))?;
209 let options = if let Some(parent) = std::path::absolute(path)?.parent() {
210 options.relative_to(parent)?
211 } else {
212 options
213 };
214 Ok(options)
215}
216
217fn validate_uv_toml(path: &Path, options: &Options) -> Result<(), Error> {
219 let Options {
220 globals: _,
221 top_level: _,
222 install_mirrors: _,
223 publish: _,
224 add: _,
225 audit: _,
226 pip: _,
227 cache_keys: _,
228 override_dependencies: _,
229 exclude_dependencies: _,
230 constraint_dependencies: _,
231 build_constraint_dependencies: _,
232 environments,
233 required_environments,
234 conflicts,
235 workspace,
236 sources,
237 dev_dependencies,
238 default_groups,
239 dependency_groups,
240 managed,
241 package,
242 build_backend,
243 } = options;
244 if conflicts.is_some() {
248 return Err(Error::PyprojectOnlyField(path.to_path_buf(), "conflicts"));
249 }
250 if workspace.is_some() {
251 return Err(Error::PyprojectOnlyField(path.to_path_buf(), "workspace"));
252 }
253 if sources.is_some() {
254 return Err(Error::PyprojectOnlyField(path.to_path_buf(), "sources"));
255 }
256 if dev_dependencies.is_some() {
257 return Err(Error::PyprojectOnlyField(
258 path.to_path_buf(),
259 "dev-dependencies",
260 ));
261 }
262 if default_groups.is_some() {
263 return Err(Error::PyprojectOnlyField(
264 path.to_path_buf(),
265 "default-groups",
266 ));
267 }
268 if dependency_groups.is_some() {
269 return Err(Error::PyprojectOnlyField(
270 path.to_path_buf(),
271 "dependency-groups",
272 ));
273 }
274 if managed.is_some() {
275 return Err(Error::PyprojectOnlyField(path.to_path_buf(), "managed"));
276 }
277 if package.is_some() {
278 return Err(Error::PyprojectOnlyField(path.to_path_buf(), "package"));
279 }
280 if build_backend.is_some() {
281 return Err(Error::PyprojectOnlyField(
282 path.to_path_buf(),
283 "build-backend",
284 ));
285 }
286 if environments.is_some() {
287 return Err(Error::PyprojectOnlyField(
288 path.to_path_buf(),
289 "environments",
290 ));
291 }
292 if required_environments.is_some() {
293 return Err(Error::PyprojectOnlyField(
294 path.to_path_buf(),
295 "required-environments",
296 ));
297 }
298 Ok(())
299}
300
301#[allow(deprecated)]
305fn warn_uv_toml_masked_fields(options: &Options) {
306 let Options {
307 globals:
308 GlobalOptions {
309 required_version,
310 system_certs,
311 native_tls,
312 offline,
313 no_cache,
314 cache_dir,
315 preview,
316 python_preference,
317 python_downloads,
318 concurrent_downloads,
319 concurrent_builds,
320 concurrent_installs,
321 allow_insecure_host,
322 http_proxy,
323 https_proxy,
324 no_proxy,
325 },
326 top_level:
327 ResolverInstallerSchema {
328 index,
329 index_url,
330 extra_index_url,
331 no_index,
332 find_links,
333 index_strategy,
334 keyring_provider,
335 resolution,
336 prerelease,
337 fork_strategy,
338 dependency_metadata,
339 config_settings,
340 config_settings_package,
341 no_build_isolation,
342 no_build_isolation_package,
343 extra_build_dependencies,
344 extra_build_variables,
345 exclude_newer,
346 exclude_newer_package,
347 link_mode,
348 compile_bytecode,
349 no_sources,
350 no_sources_package: _,
351 upgrade,
352 upgrade_package,
353 reinstall,
354 reinstall_package,
355 no_build,
356 no_build_package,
357 no_binary,
358 no_binary_package,
359 torch_backend,
360 },
361 install_mirrors:
362 PythonInstallMirrors {
363 python_install_mirror,
364 pypy_install_mirror,
365 python_downloads_json_url,
366 },
367 publish:
368 PublishOptions {
369 publish_url,
370 trusted_publishing,
371 check_url,
372 },
373 add: AddOptions { add_bounds },
374 audit: _,
375 pip,
376 cache_keys,
377 override_dependencies,
378 exclude_dependencies,
379 constraint_dependencies,
380 build_constraint_dependencies,
381 environments: _,
382 required_environments: _,
383 conflicts: _,
384 workspace: _,
385 sources: _,
386 dev_dependencies: _,
387 default_groups: _,
388 dependency_groups: _,
389 managed: _,
390 package: _,
391 build_backend: _,
392 } = options;
393
394 let mut masked_fields = vec![];
395
396 if required_version.is_some() {
397 masked_fields.push("required-version");
398 }
399 if system_certs.is_some() {
400 masked_fields.push("system-certs");
401 }
402 if native_tls.is_some() {
403 masked_fields.push("native-tls");
404 }
405 if offline.is_some() {
406 masked_fields.push("offline");
407 }
408 if no_cache.is_some() {
409 masked_fields.push("no-cache");
410 }
411 if cache_dir.is_some() {
412 masked_fields.push("cache-dir");
413 }
414 if preview.is_some() {
415 masked_fields.push("preview");
416 }
417 if python_preference.is_some() {
418 masked_fields.push("python-preference");
419 }
420 if python_downloads.is_some() {
421 masked_fields.push("python-downloads");
422 }
423 if concurrent_downloads.is_some() {
424 masked_fields.push("concurrent-downloads");
425 }
426 if concurrent_builds.is_some() {
427 masked_fields.push("concurrent-builds");
428 }
429 if concurrent_installs.is_some() {
430 masked_fields.push("concurrent-installs");
431 }
432 if allow_insecure_host.is_some() {
433 masked_fields.push("allow-insecure-host");
434 }
435 if http_proxy.is_some() {
436 masked_fields.push("http-proxy");
437 }
438 if https_proxy.is_some() {
439 masked_fields.push("https-proxy");
440 }
441 if no_proxy.is_some() {
442 masked_fields.push("no-proxy");
443 }
444 if index.is_some() {
445 masked_fields.push("index");
446 }
447 if index_url.is_some() {
448 masked_fields.push("index-url");
449 }
450 if extra_index_url.is_some() {
451 masked_fields.push("extra-index-url");
452 }
453 if no_index.is_some() {
454 masked_fields.push("no-index");
455 }
456 if find_links.is_some() {
457 masked_fields.push("find-links");
458 }
459 if index_strategy.is_some() {
460 masked_fields.push("index-strategy");
461 }
462 if keyring_provider.is_some() {
463 masked_fields.push("keyring-provider");
464 }
465 if resolution.is_some() {
466 masked_fields.push("resolution");
467 }
468 if prerelease.is_some() {
469 masked_fields.push("prerelease");
470 }
471 if fork_strategy.is_some() {
472 masked_fields.push("fork-strategy");
473 }
474 if dependency_metadata.is_some() {
475 masked_fields.push("dependency-metadata");
476 }
477 if config_settings.is_some() {
478 masked_fields.push("config-settings");
479 }
480 if config_settings_package.is_some() {
481 masked_fields.push("config-settings-package");
482 }
483 if no_build_isolation.is_some() {
484 masked_fields.push("no-build-isolation");
485 }
486 if no_build_isolation_package.is_some() {
487 masked_fields.push("no-build-isolation-package");
488 }
489 if extra_build_dependencies.is_some() {
490 masked_fields.push("extra-build-dependencies");
491 }
492 if extra_build_variables.is_some() {
493 masked_fields.push("extra-build-variables");
494 }
495 if exclude_newer.is_some() {
496 masked_fields.push("exclude-newer");
497 }
498 if exclude_newer_package.is_some() {
499 masked_fields.push("exclude-newer-package");
500 }
501 if link_mode.is_some() {
502 masked_fields.push("link-mode");
503 }
504 if compile_bytecode.is_some() {
505 masked_fields.push("compile-bytecode");
506 }
507 if no_sources.is_some() {
508 masked_fields.push("no-sources");
509 }
510 if upgrade.is_some() {
511 masked_fields.push("upgrade");
512 }
513 if upgrade_package.is_some() {
514 masked_fields.push("upgrade-package");
515 }
516 if reinstall.is_some() {
517 masked_fields.push("reinstall");
518 }
519 if reinstall_package.is_some() {
520 masked_fields.push("reinstall-package");
521 }
522 if no_build.is_some() {
523 masked_fields.push("no-build");
524 }
525 if no_build_package.is_some() {
526 masked_fields.push("no-build-package");
527 }
528 if no_binary.is_some() {
529 masked_fields.push("no-binary");
530 }
531 if no_binary_package.is_some() {
532 masked_fields.push("no-binary-package");
533 }
534 if torch_backend.is_some() {
535 masked_fields.push("torch-backend");
536 }
537 if python_install_mirror.is_some() {
538 masked_fields.push("python-install-mirror");
539 }
540 if pypy_install_mirror.is_some() {
541 masked_fields.push("pypy-install-mirror");
542 }
543 if python_downloads_json_url.is_some() {
544 masked_fields.push("python-downloads-json-url");
545 }
546 if publish_url.is_some() {
547 masked_fields.push("publish-url");
548 }
549 if trusted_publishing.is_some() {
550 masked_fields.push("trusted-publishing");
551 }
552 if check_url.is_some() {
553 masked_fields.push("check-url");
554 }
555 if add_bounds.is_some() {
556 masked_fields.push("add-bounds");
557 }
558 if pip.is_some() {
559 masked_fields.push("pip");
560 }
561 if cache_keys.is_some() {
562 masked_fields.push("cache_keys");
563 }
564 if override_dependencies.is_some() {
565 masked_fields.push("override-dependencies");
566 }
567 if exclude_dependencies.is_some() {
568 masked_fields.push("exclude-dependencies");
569 }
570 if constraint_dependencies.is_some() {
571 masked_fields.push("constraint-dependencies");
572 }
573 if build_constraint_dependencies.is_some() {
574 masked_fields.push("build-constraint-dependencies");
575 }
576 if !masked_fields.is_empty() {
577 let field_listing = masked_fields.join("\n- ");
578 warn_user!(
579 "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- {}",
580 field_listing,
581 );
582 }
583}
584
585#[derive(thiserror::Error, Debug)]
586pub enum Error {
587 #[error(transparent)]
588 Io(#[from] std::io::Error),
589
590 #[error(transparent)]
591 Index(#[from] uv_distribution_types::IndexUrlError),
592
593 #[error("Failed to parse: `{}`", _0.user_display())]
594 PyprojectToml(PathBuf, #[source] Box<toml::de::Error>),
595
596 #[error("Failed to parse: `{}`", _0.user_display())]
597 UvToml(PathBuf, #[source] Box<toml::de::Error>),
598
599 #[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
600 )]
601 PyprojectOnlyField(PathBuf, &'static str),
602
603 #[error(transparent)]
604 InvalidEnvironmentVariable(#[from] InvalidEnvironmentVariable),
605}
606
607#[derive(Copy, Clone, Debug)]
608pub struct Concurrency {
609 pub downloads: Option<NonZeroUsize>,
610 pub builds: Option<NonZeroUsize>,
611 pub installs: Option<NonZeroUsize>,
612}
613
614#[derive(Debug, Clone, Copy)]
618pub struct EnvFlag {
619 pub value: Option<bool>,
620 pub env_var: &'static str,
621}
622
623impl EnvFlag {
624 pub fn new(env_var: &'static str) -> Result<Self, Error> {
626 Ok(Self {
627 value: parse_boolish_environment_variable(env_var)?,
628 env_var,
629 })
630 }
631}
632
633#[derive(Debug, Clone)]
638pub struct EnvironmentOptions {
639 pub skip_wheel_filename_check: Option<bool>,
640 pub hide_build_output: Option<bool>,
641 pub python_install_bin: Option<bool>,
642 pub python_install_registry: Option<bool>,
643 pub install_mirrors: PythonInstallMirrors,
644 pub log_context: Option<bool>,
645 pub lfs: Option<bool>,
646 pub http_connect_timeout: Duration,
647 pub http_read_timeout: Duration,
648 pub http_read_timeout_upload: Duration,
651 pub http_retries: u32,
652 pub concurrency: Concurrency,
653 #[cfg(feature = "tracing-durations-export")]
654 pub tracing_durations_file: Option<PathBuf>,
655 pub frozen: EnvFlag,
656 pub locked: EnvFlag,
657 pub offline: EnvFlag,
658 pub no_sync: EnvFlag,
659 pub managed_python: EnvFlag,
660 pub no_managed_python: EnvFlag,
661 pub native_tls: EnvFlag,
662 pub system_certs: EnvFlag,
663 pub preview: EnvFlag,
664 pub isolated: EnvFlag,
665 pub no_progress: EnvFlag,
666 pub no_installer_metadata: EnvFlag,
667 pub dev: EnvFlag,
668 pub no_dev: EnvFlag,
669 pub show_resolution: EnvFlag,
670 pub no_editable: EnvFlag,
671 pub no_env_file: EnvFlag,
672 pub venv_seed: EnvFlag,
673 pub venv_clear: EnvFlag,
674 pub venv_relocatable: EnvFlag,
675 pub init_bare: EnvFlag,
676}
677
678impl EnvironmentOptions {
679 pub fn new() -> Result<Self, Error> {
681 let http_read_timeout = parse_integer_environment_variable(
684 EnvVars::UV_HTTP_TIMEOUT,
685 Some("value should be an integer number of seconds"),
686 )?
687 .or(parse_integer_environment_variable(
688 EnvVars::UV_REQUEST_TIMEOUT,
689 Some("value should be an integer number of seconds"),
690 )?)
691 .or(parse_integer_environment_variable(
692 EnvVars::HTTP_TIMEOUT,
693 Some("value should be an integer number of seconds"),
694 )?)
695 .map(Duration::from_secs);
696
697 Ok(Self {
698 skip_wheel_filename_check: parse_boolish_environment_variable(
699 EnvVars::UV_SKIP_WHEEL_FILENAME_CHECK,
700 )?,
701 hide_build_output: parse_boolish_environment_variable(EnvVars::UV_HIDE_BUILD_OUTPUT)?,
702 python_install_bin: parse_boolish_environment_variable(EnvVars::UV_PYTHON_INSTALL_BIN)?,
703 python_install_registry: parse_boolish_environment_variable(
704 EnvVars::UV_PYTHON_INSTALL_REGISTRY,
705 )?,
706 concurrency: Concurrency {
707 downloads: parse_integer_environment_variable(
708 EnvVars::UV_CONCURRENT_DOWNLOADS,
709 None,
710 )?,
711 builds: parse_integer_environment_variable(EnvVars::UV_CONCURRENT_BUILDS, None)?,
712 installs: parse_integer_environment_variable(
713 EnvVars::UV_CONCURRENT_INSTALLS,
714 None,
715 )?,
716 },
717 install_mirrors: PythonInstallMirrors {
718 python_install_mirror: parse_string_environment_variable(
719 EnvVars::UV_PYTHON_INSTALL_MIRROR,
720 )?,
721 pypy_install_mirror: parse_string_environment_variable(
722 EnvVars::UV_PYPY_INSTALL_MIRROR,
723 )?,
724 python_downloads_json_url: parse_string_environment_variable(
725 EnvVars::UV_PYTHON_DOWNLOADS_JSON_URL,
726 )?,
727 },
728 log_context: parse_boolish_environment_variable(EnvVars::UV_LOG_CONTEXT)?,
729 lfs: parse_boolish_environment_variable(EnvVars::UV_GIT_LFS)?,
730 http_read_timeout_upload: parse_integer_environment_variable(
731 EnvVars::UV_UPLOAD_HTTP_TIMEOUT,
732 Some("value should be an integer number of seconds"),
733 )?
734 .map(Duration::from_secs)
735 .or(http_read_timeout)
736 .unwrap_or(DEFAULT_READ_TIMEOUT_UPLOAD),
737 http_read_timeout: http_read_timeout.unwrap_or(DEFAULT_READ_TIMEOUT),
738 http_connect_timeout: parse_integer_environment_variable(
739 EnvVars::UV_HTTP_CONNECT_TIMEOUT,
740 Some("value should be an integer number of seconds"),
741 )?
742 .map(Duration::from_secs)
743 .unwrap_or(DEFAULT_CONNECT_TIMEOUT),
744 http_retries: parse_integer_environment_variable(EnvVars::UV_HTTP_RETRIES, None)?
745 .unwrap_or(uv_client::DEFAULT_RETRIES),
746 #[cfg(feature = "tracing-durations-export")]
747 tracing_durations_file: parse_path_environment_variable(
748 EnvVars::TRACING_DURATIONS_FILE,
749 ),
750 frozen: EnvFlag::new(EnvVars::UV_FROZEN)?,
751 locked: EnvFlag::new(EnvVars::UV_LOCKED)?,
752 offline: EnvFlag::new(EnvVars::UV_OFFLINE)?,
753 no_sync: EnvFlag::new(EnvVars::UV_NO_SYNC)?,
754 managed_python: EnvFlag::new(EnvVars::UV_MANAGED_PYTHON)?,
755 no_managed_python: EnvFlag::new(EnvVars::UV_NO_MANAGED_PYTHON)?,
756 native_tls: EnvFlag::new(EnvVars::UV_NATIVE_TLS)?,
757 system_certs: EnvFlag::new(EnvVars::UV_SYSTEM_CERTS)?,
758 preview: EnvFlag::new(EnvVars::UV_PREVIEW)?,
759 isolated: EnvFlag::new(EnvVars::UV_ISOLATED)?,
760 no_progress: EnvFlag::new(EnvVars::UV_NO_PROGRESS)?,
761 no_installer_metadata: EnvFlag::new(EnvVars::UV_NO_INSTALLER_METADATA)?,
762 dev: EnvFlag::new(EnvVars::UV_DEV)?,
763 no_dev: EnvFlag::new(EnvVars::UV_NO_DEV)?,
764 show_resolution: EnvFlag::new(EnvVars::UV_SHOW_RESOLUTION)?,
765 no_editable: EnvFlag::new(EnvVars::UV_NO_EDITABLE)?,
766 no_env_file: EnvFlag::new(EnvVars::UV_NO_ENV_FILE)?,
767 venv_seed: EnvFlag::new(EnvVars::UV_VENV_SEED)?,
768 venv_clear: EnvFlag::new(EnvVars::UV_VENV_CLEAR)?,
769 venv_relocatable: EnvFlag::new(EnvVars::UV_VENV_RELOCATABLE)?,
770 init_bare: EnvFlag::new(EnvVars::UV_INIT_BARE)?,
771 })
772 }
773}
774
775fn parse_string_environment_variable(name: &'static str) -> Result<Option<String>, Error> {
777 match std::env::var(name) {
778 Ok(v) => {
779 if v.is_empty() {
780 Ok(None)
781 } else {
782 Ok(Some(v))
783 }
784 }
785 Err(e) => match e {
786 std::env::VarError::NotPresent => Ok(None),
787 std::env::VarError::NotUnicode(err) => Err(Error::InvalidEnvironmentVariable(
788 InvalidEnvironmentVariable {
789 name: name.to_string(),
790 value: err.to_string_lossy().to_string(),
791 err: "expected a valid UTF-8 string".to_string(),
792 },
793 )),
794 },
795 }
796}
797
798fn parse_integer_environment_variable<T>(
799 name: &'static str,
800 help: Option<&str>,
801) -> Result<Option<T>, Error>
802where
803 T: std::str::FromStr + Copy,
804 <T as std::str::FromStr>::Err: std::fmt::Display,
805{
806 let value = match std::env::var(name) {
807 Ok(v) => v,
808 Err(e) => {
809 return match e {
810 std::env::VarError::NotPresent => Ok(None),
811 std::env::VarError::NotUnicode(err) => Err(Error::InvalidEnvironmentVariable(
812 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 };
821 if value.is_empty() {
822 return Ok(None);
823 }
824
825 match value.parse::<T>() {
826 Ok(v) => Ok(Some(v)),
827 Err(err) => Err(Error::InvalidEnvironmentVariable(
828 InvalidEnvironmentVariable {
829 name: name.to_string(),
830 value,
831 err: if let Some(help) = help {
832 format!("{err}; {help}")
833 } else {
834 err.to_string()
835 },
836 },
837 )),
838 }
839}
840
841#[cfg(feature = "tracing-durations-export")]
842fn parse_path_environment_variable(name: &'static str) -> Option<PathBuf> {
844 let value = std::env::var_os(name)?;
845
846 if value.is_empty() {
847 return None;
848 }
849
850 Some(PathBuf::from(value))
851}
852
853impl From<&EnvironmentOptions> for EnvironmentFlags {
855 fn from(options: &EnvironmentOptions) -> Self {
856 let mut flags = Self::empty();
857 if options.skip_wheel_filename_check == Some(true) {
858 flags.insert(Self::SKIP_WHEEL_FILENAME_CHECK);
859 }
860 if options.hide_build_output == Some(true) {
861 flags.insert(Self::HIDE_BUILD_OUTPUT);
862 }
863 flags
864 }
865}