Skip to content

Commit fa09d1b

Browse files
committed
refactor(ws): Reuse build-dir logic for script target-dir
1 parent cfc278b commit fa09d1b

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ impl<'de, 'gctx> de::MapAccess<'de> for ValueDeserializer<'gctx> {
556556
.unwrap_or_default();
557557
seed.deserialize(Tuple2Deserializer(2i32, s))
558558
}
559+
Definition::BuiltIn => seed.deserialize(3.into_deserializer()),
559560
}
560561
}
561562
}

src/cargo/util/context/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1375,7 +1375,9 @@ impl GlobalContext {
13751375
let abs = |path: &str, def: &Definition| -> (String, PathBuf, Definition) {
13761376
let abs_path = match def {
13771377
Definition::Path(p) | Definition::Cli(Some(p)) => p.parent().unwrap().join(&path),
1378-
Definition::Environment(_) | Definition::Cli(None) => self.cwd().join(&path),
1378+
Definition::Environment(_) | Definition::Cli(None) | Definition::BuiltIn => {
1379+
self.cwd().join(&path)
1380+
}
13791381
};
13801382
(path.to_string(), abs_path, def.clone())
13811383
};

src/cargo/util/context/value.rs

Lines changed: 15 additions & 1 deletion
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
}

0 commit comments

Comments
 (0)