Skip to content

Commit

Permalink
Fix assets builder misses crate root for build-plan (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
boozook committed May 4, 2024
1 parent e5a1b1e commit 977ec5a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
14 changes: 12 additions & 2 deletions cargo/src/assets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,21 @@ pub fn build<'cfg>(config: &'cfg Config) -> CargoResult<AssetsArtifacts<'cfg>> {
AssetKind::Package => "",
AssetKind::Dev => "dev-",
};

let dep_root = dependency.manifest_path().parent().unwrap();

config.log()
.status("Build", format!("{kind_prefix}assets for {}", dep_pkg_id));
config.log().verbose(|mut log| {
let s = format!("destination: {}", dest.as_relative_to_root(config).display());
log.status("", s)
let dest = format!("destination: {}", dest.as_relative_to_root(config).display());
log.status("", dest);
let src = format!("root {}", dep_root.as_relative_to_root(config).display());
log.status("", src);
if dep_root != &plan.path {
let path = plan.plan.crate_root();
let src = format!("root (plan) {}", path.as_relative_to_root(config).display());
log.status("", src);
}
});


Expand Down
19 changes: 14 additions & 5 deletions support/build/src/assets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,23 @@ pub fn apply_build_plan<'l, 'r, P: AsRef<Path>>(plan: BuildPlan<'l, 'r>,
AssetsBuildMethod::Link => &link_method,
};

let mut results = HashMap::with_capacity(plan.as_inner().len());
for entry in plan.into_inner().drain(..) {
let (mut plan, crate_root) = plan.into_parts();
// let mut results = HashMap::with_capacity(plan.as_inner().len());
let mut results = HashMap::with_capacity(plan.len());
// for entry in plan.into_inner().drain(..) {
for entry in plan.drain(..) {
let current: Vec<_> = match &entry {
Mapping::AsIs(inc, ..) => vec![method(&inc.source(), &inc.target(), false)],
Mapping::Into(inc, ..) => vec![method(&inc.source(), &inc.target(), true)],
Mapping::AsIs(inc, ..) => {
let source = abs_or_rel_crate_any(inc.source(), crate_root);
vec![method(&source, &inc.target(), false)]
},
Mapping::Into(inc, ..) => {
let source = abs_or_rel_crate_any(inc.source(), crate_root);
vec![method(&source, &inc.target(), true)]
},
Mapping::ManyInto { sources, target, .. } => {
sources.iter()
.map(|inc| (inc.source(), target.join(inc.target())))
.map(|inc| (abs_or_rel_crate_any(inc.source(), crate_root), target.join(inc.target())))
.map(|(ref source, ref target)| method(source, target, false))
.collect()
},
Expand Down
34 changes: 30 additions & 4 deletions support/build/src/assets/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ pub fn build_plan<'l, 'r, 'c: 'l, V>(env: &'c Env,
let key = PathBuf::from(k.as_str());
let value = v.as_str();
let into_dir = k.as_str().ends_with(MAIN_SEPARATOR_STR);
let source_exists = Path::new(value).try_exists()?;
let source_exists = {
let p = Path::new(value);
p.is_absolute()
.then(|| p.try_exists())
.unwrap_or_else(|| crate_root.join(p).try_exists())?
};

let mapping = match (source_exists, into_dir) {
(true, true) => Mapping::Into(Match::new(value, key), (k, v)),
Expand Down Expand Up @@ -189,6 +194,25 @@ pub fn build_plan<'l, 'r, 'c: 'l, V>(env: &'c Env,
}


/// Make path relative to `crate_root` if it isn't absolute, checking existence.
/// Returns `None` if path doesn't exist.
pub fn existing_abs_rel_to_crate_root<'t, P1, P2>(p: P1, root: P2) -> std::io::Result<Option<Cow<'t, Path>>>
where P1: 't + AsRef<Path> + Into<Cow<'t, Path>>,
P2: AsRef<Path> {
let p = if p.as_ref().is_absolute() && p.as_ref().try_exists()? {
Some(p.into())
} else {
let abs = root.as_ref().join(p);
if abs.try_exists()? {
Some(Cow::Owned(abs))
} else {
None
}
};
Ok(p)
}


fn glob_matches_any<'a, I: IntoIterator<Item = &'a Glob<'a>>>(path: &Path, exprs: I) -> bool {
exprs.into_iter().any(|glob| glob.is_match(path))
}
Expand Down Expand Up @@ -282,10 +306,12 @@ impl BuildPlan<'_, '_> {
})
}

pub fn serializable_flatten(
pub fn iter_flatten(
&self)
-> impl Iterator<Item = (PathBuf, (PathBuf, Option<std::time::SystemTime>))> + '_ {
let pair = |inc: &Match| (inc.target().to_path_buf(), inc.source().to_path_buf());
-> impl Iterator<Item = (MappingKind, PathBuf, (PathBuf, Option<std::time::SystemTime>))> + '_ {
let pair = |inc: &Match| {
(inc.target().to_path_buf(), abs_or_rel_crate_any(inc.source(), self.crate_root).to_path_buf())
};

self.as_inner()
.iter()
Expand Down

0 comments on commit 977ec5a

Please sign in to comment.