<commit_before>use cargo_metadata::{Message::CompilerArtifact, MetadataCommand};
use std::env;
use std::ffi::OsStr;
use std::mem;
use std::path::PathBuf;
use std::process::Command;

pub struct BuildInfo {
    cargo_target_dir: PathBuf,
}

impl BuildInfo {
    pub fn new() -> Self {
        let data = MetadataCommand::new().exec().unwrap();
        Self {
            cargo_target_dir: data.target_directory,
        }
    }

    pub fn host_lib(&self) -> PathBuf {
        if let Some(path) = option_env!("HOST_LIBS") {
            PathBuf::from(path)
        } else {
            self.cargo_target_dir.join(env!("PROFILE"))
        }
    }

    pub fn target_lib(&self) -> PathBuf {
        if let Some(path) = option_env!("TARGET_LIBS") {
            path.into()
        } else {
            let mut dir = self.cargo_target_dir.clone();
            if let Some(target) = env::var_os("CARGO_BUILD_TARGET") {
                dir.push(target);
            }
            dir.push(env!("PROFILE"));
            dir
        }
    }

    pub fn clippy_driver_path(&self) -> PathBuf {
        if let Some(path) = option_env!("CLIPPY_DRIVER_PATH") {
            PathBuf::from(path)
        } else {
            self.target_lib().join("clippy-driver")
        }
    }

    // When we'll want to use `extern crate ..` for a dependency that is used
    // both by the crate and the compiler itself, we can't simply pass -L flags
    // as we'll get a duplicate matching versions. Instead, disambiguate with
    // `--extern dep=path`.
    // See https://github.com/rust-lang/rust-clippy/issues/4015.
    pub fn third_party_crates() -> Vec<(&'static str, PathBuf)> {
        const THIRD_PARTY_CRATES: [&str; 3] = ["serde", "regex", "clippy_lints"];
        let cargo = env::var_os("CARGO");
        let cargo = cargo.as_deref().unwrap_or_else(|| OsStr::new("cargo"));
        let output = Command::new(cargo)
            .arg("build")
            .arg("--test=compile-test")
            .arg("--message-format=json")
            .output()
            .unwrap();

        let mut crates = Vec::with_capacity(THIRD_PARTY_CRATES.len());
        for message in cargo_metadata::parse_messages(output.stdout.as_slice()) {
            if let CompilerArtifact(mut artifact) = message.unwrap() {
                if let Some(&krate) = THIRD_PARTY_CRATES.iter().find(|&&krate| krate == artifact.target.name) {
                    crates.push((krate, mem::take(&mut artifact.filenames[0])));
                }
            }
        }
        crates
    }
}
<commit_msg>Add `serde_derive` to the need-to-be-disambiguated-crates list<commit_after>use cargo_metadata::{Message::CompilerArtifact, MetadataCommand};
use std::env;
use std::ffi::OsStr;
use std::mem;
use std::path::PathBuf;
use std::process::Command;

pub struct BuildInfo {
    cargo_target_dir: PathBuf,
}

impl BuildInfo {
    pub fn new() -> Self {
        let data = MetadataCommand::new().exec().unwrap();
        Self {
            cargo_target_dir: data.target_directory,
        }
    }

    pub fn host_lib(&self) -> PathBuf {
        if let Some(path) = option_env!("HOST_LIBS") {
            PathBuf::from(path)
        } else {
            self.cargo_target_dir.join(env!("PROFILE"))
        }
    }

    pub fn target_lib(&self) -> PathBuf {
        if let Some(path) = option_env!("TARGET_LIBS") {
            path.into()
        } else {
            let mut dir = self.cargo_target_dir.clone();
            if let Some(target) = env::var_os("CARGO_BUILD_TARGET") {
                dir.push(target);
            }
            dir.push(env!("PROFILE"));
            dir
        }
    }

    pub fn clippy_driver_path(&self) -> PathBuf {
        if let Some(path) = option_env!("CLIPPY_DRIVER_PATH") {
            PathBuf::from(path)
        } else {
            self.target_lib().join("clippy-driver")
        }
    }

    // When we'll want to use `extern crate ..` for a dependency that is used
    // both by the crate and the compiler itself, we can't simply pass -L flags
    // as we'll get a duplicate matching versions. Instead, disambiguate with
    // `--extern dep=path`.
    // See https://github.com/rust-lang/rust-clippy/issues/4015.
    pub fn third_party_crates() -> Vec<(&'static str, PathBuf)> {
        const THIRD_PARTY_CRATES: [&str; 4] = ["serde", "serde_derive", "regex", "clippy_lints"];
        let cargo = env::var_os("CARGO");
        let cargo = cargo.as_deref().unwrap_or_else(|| OsStr::new("cargo"));
        let output = Command::new(cargo)
            .arg("build")
            .arg("--test=compile-test")
            .arg("--message-format=json")
            .output()
            .unwrap();

        let mut crates = Vec::with_capacity(THIRD_PARTY_CRATES.len());
        for message in cargo_metadata::parse_messages(output.stdout.as_slice()) {
            if let CompilerArtifact(mut artifact) = message.unwrap() {
                if let Some(&krate) = THIRD_PARTY_CRATES.iter().find(|&&krate| krate == artifact.target.name) {
                    crates.push((krate, mem::take(&mut artifact.filenames[0])));
                }
            }
        }
        crates
    }
}

{'fraction_non_alphanumeric': 0.14357682619647355, 'fraction_numerical': 0.002325130788606859, 'mean_word_length': 2.168815224063843, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2180492', 'n_tokens_mistral': 1648, 'n_tokens_neox': 1619, 'n_words': 336}