Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exclusions in build.watch getting applied to all components #2554

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 57 additions & 40 deletions src/commands/watch/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use anyhow::Context;
use async_trait::async_trait;
use spin_common::ui::quoted_path;
use spin_manifest::schema::v2;
use watchexec::filter::Filterer;

#[async_trait]
pub(crate) trait FilterFactory: Send + Sync {
Expand All @@ -15,7 +16,7 @@ pub(crate) trait FilterFactory: Send + Sync {
manifest_file: &Path,
manifest_dir: &Path,
manifest: &v2::AppManifest,
) -> anyhow::Result<Arc<watchexec_filterer_globset::GlobsetFilterer>>;
) -> anyhow::Result<Arc<dyn Filterer>>;
}

pub(crate) struct ArtifactFilterFactory {
Expand All @@ -33,7 +34,7 @@ impl FilterFactory for ArtifactFilterFactory {
manifest_file: &Path,
manifest_dir: &Path,
manifest: &v2::AppManifest,
) -> anyhow::Result<Arc<watchexec_filterer_globset::GlobsetFilterer>> {
) -> anyhow::Result<Arc<dyn Filterer>> {
let manifest_glob = if self.skip_build {
vec![manifest_path_to_watch(manifest_file)?]
} else {
Expand Down Expand Up @@ -63,17 +64,9 @@ impl FilterFactory for ArtifactFilterFactory {
.into_iter()
.chain(wasm_globs)
.chain(asset_globs)
.map(|s| (s, None))
.collect::<Vec<_>>();

let filterer = watchexec_filterer_globset::GlobsetFilterer::new(
manifest_dir,
artifact_globs,
standard_ignores(),
[],
[],
)
.await?;
let filterer = globset_filter(manifest_dir, artifact_globs).await?;

Ok(Arc::new(filterer))
}
Expand All @@ -95,28 +88,22 @@ impl FilterFactory for BuildFilterFactory {
manifest_file: &Path,
manifest_dir: &Path,
manifest: &v2::AppManifest,
) -> anyhow::Result<Arc<watchexec_filterer_globset::GlobsetFilterer>> {
let manifest_glob = vec![manifest_path_to_watch(manifest_file)?];
let src_globs = manifest
.components
.iter()
.flat_map(|(cid, c)| create_source_globs(cid.as_ref(), c))
.collect::<Vec<_>>();
) -> anyhow::Result<Arc<dyn Filterer>> {
let mut filterers: Vec<Box<dyn Filterer>> =
Vec::with_capacity(manifest.components.len() + 1);

let build_globs = manifest_glob
.into_iter()
.chain(src_globs)
.map(|s| (s, None))
.collect::<Vec<_>>();
let manifest_globs = vec![manifest_path_to_watch(manifest_file)?];
let manifest_filterer = globset_filter(manifest_dir, manifest_globs).await?;

let filterer = watchexec_filterer_globset::GlobsetFilterer::new(
manifest_dir,
build_globs,
standard_ignores(),
[],
[],
)
.await?;
filterers.push(Box::new(manifest_filterer));

for (cid, c) in &manifest.components {
let build_globs = create_source_globs(cid.as_ref(), c);
let build_filterer = globset_filter(manifest_dir, build_globs).await?;
filterers.push(Box::new(build_filterer));
}

let filterer = CompositeFilterer { filterers };

Ok(Arc::new(filterer))
}
Expand Down Expand Up @@ -152,22 +139,31 @@ impl FilterFactory for ManifestFilterFactory {
manifest_file: &Path,
manifest_dir: &Path,
_: &v2::AppManifest,
) -> anyhow::Result<Arc<watchexec_filterer_globset::GlobsetFilterer>> {
) -> anyhow::Result<Arc<dyn Filterer>> {
let manifest_glob = manifest_path_to_watch(manifest_file)?;

let filterer = watchexec_filterer_globset::GlobsetFilterer::new(
manifest_dir,
vec![(manifest_glob, None)],
standard_ignores(),
[],
[],
)
.await?;
let filterer = globset_filter(manifest_dir, [manifest_glob]).await?;

Ok(Arc::new(filterer))
}
}

async fn globset_filter(
manifest_dir: &Path,
globs: impl IntoIterator<Item = String>,
) -> anyhow::Result<watchexec_filterer_globset::GlobsetFilterer> {
let filterer = watchexec_filterer_globset::GlobsetFilterer::new(
manifest_dir,
globs.into_iter().map(|s| (s, None)),
standard_ignores(),
[],
[],
)
.await?;

Ok(filterer)
}

// Although manifest dir must be absolute, and most things are safer with abs
// file paths, the manifest _path_ for the watchers must be relative to manifest dir
fn manifest_path_to_watch(path: &Path) -> anyhow::Result<String> {
Expand All @@ -185,3 +181,24 @@ fn standard_ignores() -> Vec<(String, Option<PathBuf>)> {
.map(|pat| (pat.to_owned(), None))
.collect()
}

#[derive(Debug)]
struct CompositeFilterer {
filterers: Vec<Box<dyn watchexec::filter::Filterer>>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Perhaps put a Vec<Box<dyn watchexec::filter::Filterer>> behind a type alias? Not sure if that is cleaner so I'll leave that up to you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, to my mind it doesn't occur enough, or have a distinct semantic concept other than "vector of filterers", to merit the indirection. I agree Rust's insistence on box-dyn ceremony makes it harder to read than it needs to be though. If we want to do this I think I'd alias the box-dyn and make this Vec<AnyFilterer>...

}

impl watchexec::filter::Filterer for CompositeFilterer {
fn check_event(
&self,
event: &watchexec::event::Event,
priority: watchexec::event::Priority,
) -> Result<bool, watchexec::error::RuntimeError> {
// We are interested in a change if _any_ component is interested in it
for f in &self.filterers {
if f.check_event(event, priority)? {
return Ok(true);
}
}
Ok(false)
}
}
Loading