Repository: microsoft/onefuzz
File: src/agent/onefuzz-task/src/tasks/coverage/generic.rs (Rust)

Code before:
        context.heartbeat.alive();

        if let Some(queue) = &self.config.input_queue {
            info!("polling queue for new coverage inputs");

            let callback = CallbackImpl::new(queue.clone(), context)?;
            self.poller.run(callback).await?;
        }

        Ok(())
    }

    async fn load_filter(&self) -> Result<CmdFilter> {
        let raw_filter_path = if let Some(raw_path) = &self.config.coverage_filter {
            raw_path
        } else {
            return Ok(CmdFilter::default());
        };

        let setup_dir = &self.config.common.setup_dir;

        // Ensure users can locate the filter relative to the setup container.
        let expand = Expand::new().setup_dir(setup_dir);
        let expanded_filter_path = expand.evaluate_value(raw_filter_path)?;

        // Determine if `coverage_filter` is already prefixed with a absolute or relative
        // path to the local `setup` directory.
        //
        // This happens when `coverage_filter` is prefixed by by `"setup/"` (due to legacy
        // server-side prefixing) or if it had been user-prefixed with the `{setup_dir}`
        // placeholder variable (which we just expanded to the full local path of the
        // `setup` dir).
        let is_absolutely_prefixed =
            expanded_filter_path.starts_with(&*setup_dir.to_string_lossy());
        let is_relatively_prefixed = expanded_filter_path.starts_with("setup/");
        let is_setup_prefixed = is_absolutely_prefixed || is_relatively_prefixed;

        let filter_path = if is_setup_prefixed {
            // The filter path should now name an existing local file (via a relative or
            // absolute path).
            expanded_filter_path
        } else {
            // The filter path is still a `setup`-relative subpath. To locate the filter
            // file, we need to prefix the subpath with the local path to the `setup` dir.
            let full = setup_dir.join(expanded_filter_path);
            full.to_string_lossy().into_owned()
        };

        let data = fs::read(&filter_path).await?;
        let def: CmdFilterDef = serde_json::from_slice(&data)?;
        let filter = CmdFilter::new(def)?;

Review comment (suggestion):
Hmm, we can do this more robustly by introducing a `find_setup_relative_file()` function which could use file existence checks and attempted expansions to handle the cases of no prefixing (a fully relativized subpath), `setup/`-prefixing, and `{setup_dir}`-prefixing. We could use that same function wherever we are prepending `{setup_dir}` to `target_exe`. Let's try that instead.

Code after:

        context.heartbeat.alive();

        if let Some(queue) = &self.config.input_queue {
            info!("polling queue for new coverage inputs");

            let callback = CallbackImpl::new(queue.clone(), context)?;
            self.poller.run(callback).await?;
        }

        Ok(())
    }

    async fn load_filter(&self) -> Result<CmdFilter> {
        let raw_filter_path = if let Some(raw_path) = &self.config.coverage_filter {
            raw_path
        } else {
            return Ok(CmdFilter::default());
        };

        let resolved =
            resolve_setup_relative_path(&self.config.common.setup_dir, raw_filter_path).await?;
        let filter_path = if let Some(path) = resolved {
            path
        } else {
            error!(
                "unable to resolve setup-relative coverage filter path: {}",
                raw_filter_path
            );
            return Ok(CmdFilter::default());
        };

        let data = fs::read(&filter_path).await?;
        let def: CmdFilterDef = serde_json::from_slice(&data)?;
        let filter = CmdFilter::new(def)?;

        Ok(filter)
    }
}

async fn deserialize_or_default<T>(path: impl AsRef<Path>) -> Result<T>
where
    T: Default + DeserializeOwned,
{
    use tokio::io::ErrorKind::NotFound;

    let data = fs::read(path).await;

    if let Err(err) = &data {
        if err.kind() == NotFound {
            return Ok(T::default());