Skip to content

Commit 2059e0f

Browse files
committed
refactor(gctx): decouple gctx from schemas
Pass in current working directory instead, so that schema types can avoid depend on GlobalContext
1 parent 5d202cc commit 2059e0f

File tree

6 files changed

+17
-17
lines changed

6 files changed

+17
-17
lines changed

src/cargo/core/compiler/compile_kind.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl CompileKind {
114114
let kinds = match (fallback, &gctx.build_config()?.target) {
115115
(_, None) | (CompileKindFallback::JustHost, _) => Ok(vec![CompileKind::Host]),
116116
(CompileKindFallback::BuildConfig, Some(build_target_config)) => {
117-
dedup(&build_target_config.values(gctx)?)
117+
dedup(&build_target_config.values(gctx.cwd())?)
118118
}
119119
};
120120

src/cargo/ops/resolve.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ pub fn add_overrides<'a>(
540540
// The path listed next to the string is the config file in which the
541541
// key was located, so we want to pop off the `.cargo/config` component
542542
// to get the directory containing the `.cargo` folder.
543-
(paths::normalize_path(&def.root(gctx).join(s)), def)
543+
(paths::normalize_path(&def.root(gctx.cwd()).join(s)), def)
544544
});
545545

546546
for (path, definition) in paths {

src/cargo/util/context/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,7 @@ impl GlobalContext {
10021002
fn string_to_path(&self, value: &str, definition: &Definition) -> PathBuf {
10031003
let is_path = value.contains('/') || (cfg!(windows) && value.contains('\\'));
10041004
if is_path {
1005-
definition.root(self).join(value)
1005+
definition.root(self.cwd()).join(value)
10061006
} else {
10071007
// A pathless name.
10081008
PathBuf::from(value)
@@ -1675,7 +1675,7 @@ impl GlobalContext {
16751675
// This handles relative file: URLs, relative to the config definition.
16761676
let base = index
16771677
.definition
1678-
.root(self)
1678+
.root(self.cwd())
16791679
.join("truncated-by-url_with_base");
16801680
// Parse val to check it is a URL, not a relative path without a protocol.
16811681
let _parsed = index.val.into_url()?;
@@ -1921,7 +1921,7 @@ impl GlobalContext {
19211921
.into_iter()
19221922
.filter_map(|(k, v)| {
19231923
if v.is_force() || self.get_env_os(&k).is_none() {
1924-
Some((k, v.resolve(self).to_os_string()))
1924+
Some((k, v.resolve(self.cwd()).to_os_string()))
19251925
} else {
19261926
None
19271927
}

src/cargo/util/context/path.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl ConfigRelativePath {
3030
/// This will always return an absolute path where it's relative to the
3131
/// location for configuration for this value.
3232
pub fn resolve_path(&self, gctx: &GlobalContext) -> PathBuf {
33-
self.0.definition.root(gctx).join(&self.0.val)
33+
self.0.definition.root(gctx.cwd()).join(&self.0.val)
3434
}
3535

3636
/// Same as [`Self::resolve_path`] but will make string replacements
@@ -72,7 +72,7 @@ impl ConfigRelativePath {
7272
});
7373
}
7474

75-
Ok(self.0.definition.root(gctx).join(&value))
75+
Ok(self.0.definition.root(gctx.cwd()).join(&value))
7676
}
7777

7878
/// Resolves this configuration-relative path to either an absolute path or

src/cargo/util/context/schema.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ use std::fmt;
66
use serde::Deserialize;
77
use serde_untagged::UntaggedEnumVisitor;
88

9+
use std::path::Path;
10+
911
use crate::CargoResult;
10-
use crate::GlobalContext;
1112

1213
use super::StringList;
1314
use super::Value;
@@ -219,14 +220,14 @@ impl<'de> Deserialize<'de> for BuildTargetConfigInner {
219220

220221
impl BuildTargetConfig {
221222
/// Gets values of `build.target` as a list of strings.
222-
pub fn values(&self, gctx: &GlobalContext) -> CargoResult<Vec<String>> {
223+
pub fn values(&self, cwd: &Path) -> CargoResult<Vec<String>> {
223224
let map = |s: &String| {
224225
if s.ends_with(".json") {
225226
// Path to a target specification file (in JSON).
226227
// <https://doc.rust-lang.org/rustc/targets/custom.html>
227228
self.inner
228229
.definition
229-
.root(gctx)
230+
.root(cwd)
230231
.join(s)
231232
.to_str()
232233
.expect("must be utf-8 in toml")
@@ -412,7 +413,7 @@ impl EnvConfigValue {
412413
}
413414
}
414415

415-
pub fn resolve<'a>(&'a self, gctx: &GlobalContext) -> Cow<'a, OsStr> {
416+
pub fn resolve<'a>(&'a self, cwd: &Path) -> Cow<'a, OsStr> {
416417
match self.inner.val {
417418
EnvConfigValueInner::Simple(ref s) => Cow::Borrowed(OsStr::new(s.as_str())),
418419
EnvConfigValueInner::WithOptions {
@@ -421,7 +422,7 @@ impl EnvConfigValue {
421422
..
422423
} => {
423424
if relative {
424-
let p = self.inner.definition.root(gctx).join(&value);
425+
let p = self.inner.definition.root(cwd).join(&value);
425426
Cow::Owned(p.into_os_string())
426427
} else {
427428
Cow::Borrowed(OsStr::new(value.as_str()))

src/cargo/util/context/value.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
//!
4343
//! [`de`]: crate::util::context::de
4444
45-
use crate::util::context::GlobalContext;
4645
use serde::de;
4746
use std::cmp::Ordering;
4847
use std::fmt;
@@ -104,12 +103,12 @@ impl Ord for Definition {
104103
impl Definition {
105104
/// Root directory where this is defined.
106105
///
107-
/// If from a file, it is the directory above `.cargo/config`.
108-
/// CLI and env are the current working directory.
109-
pub fn root<'a>(&'a self, gctx: &'a GlobalContext) -> &'a Path {
106+
/// If from a file, it is the directory above `.cargo/config.toml`.
107+
/// CLI and env use the provided current working directory.
108+
pub fn root<'a>(&'a self, cwd: &'a Path) -> &'a Path {
110109
match self {
111110
Definition::Path(p) | Definition::Cli(Some(p)) => p.parent().unwrap().parent().unwrap(),
112-
Definition::Environment(_) | Definition::Cli(None) | Definition::BuiltIn => gctx.cwd(),
111+
Definition::Environment(_) | Definition::Cli(None) | Definition::BuiltIn => cwd,
113112
}
114113
}
115114

0 commit comments

Comments
 (0)