| 1 |
// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 |
|
| 3 |
// https://doc.rust-lang.org/nightly/cargo/reference/config.html#hierarchical-structure |
| 4 |
// |
| 5 |
// > Cargo allows local configuration for a particular package as well as global |
| 6 |
// > configuration. It looks for configuration files in the current directory |
| 7 |
// > and all parent directories. If, for example, Cargo were invoked in |
| 8 |
// > `/projects/foo/bar/baz`, then the following configuration files would be |
| 9 |
// > probed for and unified in this order: |
| 10 |
// > |
| 11 |
// > - `/projects/foo/bar/baz/.cargo/config.toml` |
| 12 |
// > - `/projects/foo/bar/.cargo/config.toml` |
| 13 |
// > - `/projects/foo/.cargo/config.toml` |
| 14 |
// > - `/projects/.cargo/config.toml` |
| 15 |
// > - `/.cargo/config.toml` |
| 16 |
// > - `$CARGO_HOME/config.toml` which defaults to: |
| 17 |
// > - Windows: `%USERPROFILE%\.cargo\config.toml` |
| 18 |
// > - Unix: `$HOME/.cargo/config.toml` |
| 19 |
|
| 20 |
use core::ops; |
| 21 |
use std::path::{Path, PathBuf}; |
| 22 |
|
| 23 |
fn config_path(path: &Path) -> Option<PathBuf> { |
| 24 |
// https://doc.rust-lang.org/nightly/cargo/reference/config.html#hierarchical-structure |
| 25 |
// |
| 26 |
// > Cargo also reads config files without the `.toml` extension, |
| 27 |
// > such as `.cargo/config`. Support for the `.toml` extension was |
| 28 |
// > added in version 1.39 and is the preferred form. If both files |
| 29 |
// > exist, Cargo will use the file without the extension. |
| 30 |
let config = path.join("config"); |
| 31 |
if config.exists() { |
| 32 |
return Some(config); |
| 33 |
} |
| 34 |
let config = path.join("config.toml"); |
| 35 |
if config.exists() { |
| 36 |
return Some(config); |
| 37 |
} |
| 38 |
None |
| 39 |
} |
| 40 |
|
| 41 |
// This is okay even on Windows since Rust 1.85: https://github.com/rust-lang/rust/pull/132515 |
| 42 |
// TODO(semver): Remove this wrapper in next breaking release. |
| 43 |
pub fn home_dir() -> Option<PathBuf> { |
| 44 |
#[allow(deprecated)] |
| 45 |
std::env::home_dir() |
| 46 |
} |
| 47 |
pub fn cargo_home_with_cwd(cwd: &Path) -> Option<PathBuf> { |
| 48 |
// Follow the cargo's behavior. |
| 49 |
// https://github.com/rust-lang/cargo/blob/0.80.0/crates/home/src/lib.rs#L77-L86 |
| 50 |
// https://github.com/rust-lang/cargo/blob/0.80.0/crates/home/src/env.rs#L63-L77 |
| 51 |
match std::env::var_os("CARGO_HOME").filter(|h| !h.is_empty()).map(PathBuf::from) { |
| 52 |
Some(home) => { |
| 53 |
if home.is_absolute() { |
| 54 |
Some(home) |
| 55 |
} else { |
| 56 |
Some(cwd.join(home)) |
| 57 |
} |
| 58 |
} |
| 59 |
_ => Some(home_dir()?.join(".cargo")), |
| 60 |
} |
| 61 |
} |
| 62 |
pub fn rustup_home_with_cwd(cwd: &Path) -> Option<PathBuf> { |
| 63 |
// Follow the cargo's behavior. |
| 64 |
// https://github.com/rust-lang/cargo/blob/0.80.0/crates/home/src/lib.rs#L114-L123 |
| 65 |
// https://github.com/rust-lang/cargo/blob/0.80.0/crates/home/src/env.rs#L92-L106 |
| 66 |
match std::env::var_os("RUSTUP_HOME").filter(|h| !h.is_empty()).map(PathBuf::from) { |
| 67 |
Some(home) => { |
| 68 |
if home.is_absolute() { |
| 69 |
Some(home) |
| 70 |
} else { |
| 71 |
Some(cwd.join(home)) |
| 72 |
} |
| 73 |
} |
| 74 |
_ => Some(home_dir()?.join(".rustup")), |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
/// An iterator over Cargo configuration file paths. |
| 79 |
#[derive(Debug)] |
| 80 |
#[must_use = "iterators are lazy and do nothing unless consumed"] |
| 81 |
pub(crate) struct WalkInner<'a, P> { |
| 82 |
ancestors: std::path::Ancestors<'a>, |
| 83 |
cargo_home: Option<P>, |
| 84 |
} |
| 85 |
|
| 86 |
impl<'a, P: ops::Deref<Target = Path>> WalkInner<'a, P> { |
| 87 |
/// Creates an iterator over Cargo configuration file paths from the given path |
| 88 |
/// and `CARGO_HOME` path. |
| 89 |
pub(crate) fn with_cargo_home(current_dir: &'a Path, cargo_home: Option<P>) -> Self { |
| 90 |
Self { ancestors: current_dir.ancestors(), cargo_home } |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
impl<P: ops::Deref<Target = Path>> Iterator for WalkInner<'_, P> { |
| 95 |
type Item = PathBuf; |
| 96 |
fn next(&mut self) -> Option<Self::Item> { |
| 97 |
for p in self.ancestors.by_ref() { |
| 98 |
let p = p.join(".cargo"); |
| 99 |
// dedup CARGO_HOME |
| 100 |
if self.cargo_home.as_deref() == Some(&p) { |
| 101 |
self.cargo_home = None; |
| 102 |
} |
| 103 |
if let Some(p) = config_path(&p) { |
| 104 |
return Some(p); |
| 105 |
} |
| 106 |
} |
| 107 |
config_path(&self.cargo_home.take()?) |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
// TODO(semver): Remove in next breaking release? (if no one using this) |
| 112 |
/// An iterator over Cargo configuration file paths. |
| 113 |
#[derive(Debug)] |
| 114 |
#[must_use = "iterators are lazy and do nothing unless consumed"] |
| 115 |
pub struct Walk<'a>(WalkInner<'a, PathBuf>); |
| 116 |
|
| 117 |
impl<'a> Walk<'a> { |
| 118 |
/// Creates an iterator over Cargo configuration file paths from the given path. |
| 119 |
pub fn new(current_dir: &'a Path) -> Self { |
| 120 |
Self::with_cargo_home(current_dir, cargo_home_with_cwd(current_dir)) |
| 121 |
} |
| 122 |
|
| 123 |
/// Creates an iterator over Cargo configuration file paths from the given path |
| 124 |
/// and `CARGO_HOME` path. |
| 125 |
pub fn with_cargo_home(current_dir: &'a Path, cargo_home: Option<PathBuf>) -> Self { |
| 126 |
Self(WalkInner::with_cargo_home(current_dir, cargo_home)) |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
impl Iterator for Walk<'_> { |
| 131 |
type Item = PathBuf; |
| 132 |
fn next(&mut self) -> Option<Self::Item> { |
| 133 |
self.0.next() |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
#[cfg(test)] |
| 138 |
mod tests { |
| 139 |
use fs_err as fs; |
| 140 |
|
| 141 |
use super::*; |
| 142 |
|
| 143 |
#[test] |
| 144 |
fn walk() { |
| 145 |
let tmp = tempfile::tempdir().unwrap(); |
| 146 |
let p = tmp.path(); |
| 147 |
let home = &p.join("a/.cargo"); |
| 148 |
let cwd = &p.join("a/b/c"); |
| 149 |
fs::create_dir_all(home).unwrap(); |
| 150 |
fs::write(p.join("a/.cargo/config"), "").unwrap(); |
| 151 |
fs::create_dir_all(p.join("a/b/.cargo")).unwrap(); |
| 152 |
fs::write(p.join("a/b/.cargo/config"), "").unwrap(); |
| 153 |
fs::write(p.join("a/b/.cargo/config.toml"), "").unwrap(); |
| 154 |
fs::create_dir_all(p.join("a/b/c/.cargo")).unwrap(); |
| 155 |
fs::write(p.join("a/b/c/.cargo/config.toml"), "").unwrap(); |
| 156 |
let mut w = Walk::with_cargo_home(cwd, Some(home.clone())); |
| 157 |
assert_eq!(w.next(), Some(p.join("a/b/c/.cargo/config.toml"))); |
| 158 |
assert_eq!(w.next(), Some(p.join("a/b/.cargo/config"))); |
| 159 |
assert_eq!(w.next(), Some(p.join("a/.cargo/config"))); |
| 160 |
assert_eq!(w.next(), None); |
| 161 |
} |
| 162 |
} |