Skip to content

Commit a12c482

Browse files
committed
refactor(ws): Reuse build-dir logic for script target-dir
1 parent 5ee83a2 commit a12c482

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

src/cargo/core/workspace.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -444,13 +444,14 @@ impl<'gctx> Workspace<'gctx> {
444444

445445
fn default_target_dir(&self) -> Filesystem {
446446
if self.root_maybe().is_embedded() {
447-
let hash = crate::util::hex::short_hash(&self.root_manifest().to_string_lossy());
448-
let mut rel_path = PathBuf::new();
449-
rel_path.push("target");
450-
rel_path.push(&hash[0..2]);
451-
rel_path.push(&hash[2..]);
452-
453-
self.gctx().home().join(rel_path)
447+
let default = ConfigRelativePath::new(
448+
"{cargo-cache-home}/target/{workspace-path-hash}"
449+
.to_owned()
450+
.into(),
451+
);
452+
self.gctx()
453+
.custom_build_dir(&default, self.root_manifest())
454+
.expect("template is correct")
454455
} else {
455456
Filesystem::new(self.root().join("target"))
456457
}

src/cargo/util/context/de.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,18 +564,19 @@ impl<'de, 'gctx> de::MapAccess<'de> for ValueDeserializer<'gctx> {
564564
// ... otherwise we're deserializing the `definition` field, so we need
565565
// to figure out where the field we just deserialized was defined at.
566566
match self.definition() {
567+
Definition::BuiltIn => seed.deserialize(0.into_deserializer()),
567568
Definition::Path(path) => {
568-
seed.deserialize(Tuple2Deserializer(0i32, path.to_string_lossy()))
569+
seed.deserialize(Tuple2Deserializer(1i32, path.to_string_lossy()))
569570
}
570571
Definition::Environment(env) => {
571-
seed.deserialize(Tuple2Deserializer(1i32, env.as_str()))
572+
seed.deserialize(Tuple2Deserializer(2i32, env.as_str()))
572573
}
573574
Definition::Cli(path) => {
574575
let s = path
575576
.as_ref()
576577
.map(|p| p.to_string_lossy())
577578
.unwrap_or_default();
578-
seed.deserialize(Tuple2Deserializer(2i32, s))
579+
seed.deserialize(Tuple2Deserializer(3i32, s))
579580
}
580581
}
581582
}

src/cargo/util/context/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1371,7 +1371,9 @@ impl GlobalContext {
13711371
let abs = |path: &str, def: &Definition| -> (String, PathBuf, Definition) {
13721372
let abs_path = match def {
13731373
Definition::Path(p) | Definition::Cli(Some(p)) => p.parent().unwrap().join(&path),
1374-
Definition::Environment(_) | Definition::Cli(None) => self.cwd().join(&path),
1374+
Definition::Environment(_) | Definition::Cli(None) | Definition::BuiltIn => {
1375+
self.cwd().join(&path)
1376+
}
13751377
};
13761378
(path.to_string(), abs_path, def.clone())
13771379
};

src/cargo/util/context/value.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub(crate) static FIELDS: [&str; 2] = [VALUE_FIELD, DEFINITION_FIELD];
5555
/// Location where a config value is defined.
5656
#[derive(Clone, Debug, Eq)]
5757
pub enum Definition {
58+
BuiltIn,
5859
/// Defined in a `.cargo/config`, includes the path to the file.
5960
Path(PathBuf),
6061
/// Defined in an environment variable, includes the environment key.
@@ -90,7 +91,7 @@ impl Definition {
9091
pub fn root<'a>(&'a self, gctx: &'a GlobalContext) -> &'a Path {
9192
match self {
9293
Definition::Path(p) | Definition::Cli(Some(p)) => p.parent().unwrap().parent().unwrap(),
93-
Definition::Environment(_) | Definition::Cli(None) => gctx.cwd(),
94+
Definition::Environment(_) | Definition::Cli(None) | Definition::BuiltIn => gctx.cwd(),
9495
}
9596
}
9697

@@ -102,7 +103,10 @@ impl Definition {
102103
(self, other),
103104
(Definition::Cli(_), Definition::Environment(_))
104105
| (Definition::Cli(_), Definition::Path(_))
106+
| (Definition::Cli(_), Definition::BuiltIn)
105107
| (Definition::Environment(_), Definition::Path(_))
108+
| (Definition::Environment(_), Definition::BuiltIn)
109+
| (Definition::Path(_), Definition::BuiltIn)
106110
)
107111
}
108112
}
@@ -123,6 +127,16 @@ impl fmt::Display for Definition {
123127
Definition::Path(p) | Definition::Cli(Some(p)) => p.display().fmt(f),
124128
Definition::Environment(key) => write!(f, "environment variable `{}`", key),
125129
Definition::Cli(None) => write!(f, "--config cli option"),
130+
Definition::BuiltIn => write!(f, "default"),
131+
}
132+
}
133+
}
134+
135+
impl<T> From<T> for Value<T> {
136+
fn from(val: T) -> Self {
137+
Self {
138+
val,
139+
definition: Definition::BuiltIn,
126140
}
127141
}
128142
}
@@ -236,9 +250,10 @@ impl<'de> de::Deserialize<'de> for Definition {
236250
{
237251
let (discr, value) = <(u32, String)>::deserialize(deserializer)?;
238252
match discr {
239-
0 => Ok(Definition::Path(value.into())),
240-
1 => Ok(Definition::Environment(value)),
241-
2 => {
253+
0 => Ok(Definition::BuiltIn),
254+
1 => Ok(Definition::Path(value.into())),
255+
2 => Ok(Definition::Environment(value)),
256+
3 => {
242257
let path = (value.len() > 0).then_some(value.into());
243258
Ok(Definition::Cli(path))
244259
}

0 commit comments

Comments
 (0)