use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::process::{Output, Stdio};
use std::time::Duration;
use tokio::process::Command;
use tokio::time::Instant;
use crate::analysis::findings::Finding;
use crate::languages::spec::{
DEFAULT_TOOL_TIMEOUT_SECS, DiagnosticsStream, ToolArguments, ToolSpec,
};
mod ancestry;
mod go_packages;
mod markers;
mod narrow;
pub mod parsers;
mod uri;
pub(crate) use parsers::TYPE_CHECK_KIND;
pub use parsers::parse_output;
pub(crate) use ancestry::absolute;
pub(crate) use ancestry::ancestors_within;
pub(crate) use ancestry::lexically_normal;
pub(crate) use markers::configured_marker;
pub use markers::is_configured;
pub(crate) use markers::marker_names_path;
pub(crate) use narrow::joined_reported;
use narrow::retain_requested;
#[cfg(test)]
mod tests;
pub const TOOL_TIMEOUT: Duration = Duration::from_secs(DEFAULT_TOOL_TIMEOUT_SECS);
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ToolStatus {
Ok,
Skipped,
Unavailable,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ToolOutcome {
pub tool: &'static str,
pub status: ToolStatus,
pub findings: Vec<Finding>,
pub detail: String,
pub compilation_succeeded: bool,
pub excluded: Vec<String>,
}
impl ToolOutcome {
fn skipped(spec: &ToolSpec) -> Self {
let listed = spec.config_files[..spec.config_files.len().min(3)].join(", ");
Self::empty(
spec,
ToolStatus::Skipped,
format!("not configured (add one of: {listed})"),
)
}
fn unavailable(spec: &ToolSpec, detail: String) -> Self {
Self::empty(spec, ToolStatus::Unavailable, detail)
}
fn ready(spec: &ToolSpec) -> Self {
Self::empty(spec, ToolStatus::Ok, "ready".to_owned())
}
fn empty(spec: &ToolSpec, status: ToolStatus, detail: String) -> Self {
Self {
tool: spec.name,
status,
findings: Vec::new(),
detail,
compilation_succeeded: false,
excluded: Vec::new(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("{0}")]
pub struct ToolOutputError(pub String);
pub(crate) fn is_executable(path: &Path) -> bool {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
match path.metadata() {
Ok(meta) => meta.is_file() && meta.permissions().mode() & 0o111 != 0,
Err(_) => false,
}
}
#[cfg(not(unix))]
{
path.is_file()
}
}
pub fn resolve_tool(spec: &ToolSpec, root: &Path) -> Option<PathBuf> {
resolve_tool_in(spec, root, std::env::var_os("PATH").as_deref())
}
pub(crate) fn resolve_tool_in(
spec: &ToolSpec,
root: &Path,
path: Option<&std::ffi::OsStr>,
) -> Option<PathBuf> {
resolve_tool_at(spec, root, root, path)
}
fn resolve_tool_at(
spec: &ToolSpec,
repository_root: &Path,
workspace_root: &Path,
path: Option<&std::ffi::OsStr>,
) -> Option<PathBuf> {
for directory in ancestors_within(workspace_root, repository_root) {
for relative in spec.local_paths {
let candidate = directory.join(relative);
if is_executable(&candidate) {
return Some(candidate);
}
}
}
let name = spec.command.first()?;
which_first_in(name, path?).map(PathBuf::from)
}
fn which_first_in(command: &str, path: &std::ffi::OsStr) -> Option<String> {
for dir in std::env::split_paths(path) {
let candidate = dir.join(command);
if is_executable(&candidate) {
return Some(candidate.to_string_lossy().into_owned());
}
}
None
}
pub(crate) fn configuration_root(
spec: &ToolSpec,
repository_root: &Path,
file: &Path,
) -> Option<PathBuf> {
let repository_root = absolute(repository_root);
let file = if file.is_absolute() {
file.to_path_buf()
} else {
repository_root.join(file)
};
ancestors_within(file.parent()?, &repository_root)
.into_iter()
.find(|directory| is_configured(spec, directory))
}
pub fn tool_status(spec: &ToolSpec, root: &Path) -> ToolOutcome {
tool_status_at(spec, root, root)
}
pub async fn run_tool(spec: &ToolSpec, root: &Path, files: &[String]) -> ToolOutcome {
run_tool_at(spec, root, root, files).await
}
pub(crate) async fn run_tool_at(
spec: &ToolSpec,
repository_root: &Path,
workspace_root: &Path,
files: &[String],
) -> ToolOutcome {
let executable = match eligible_executable(spec, repository_root, workspace_root) {
Ok(executable) => executable,
Err(outcome) => return outcome,
};
let executable = if executable.is_relative() {
std::env::current_dir()
.map(|cwd| cwd.join(&executable))
.unwrap_or(executable)
} else {
executable
};
let mut argv: Vec<String> = Vec::with_capacity(spec.command.len() + files.len() + 2);
argv.push(executable.to_string_lossy().into_owned());
argv.extend(spec.command[1..].iter().map(|s| (*s).to_owned()));
if let Some(flag) = spec.config_flag
&& let Some(config) = configured_marker(spec, workspace_root)
{
argv.push(flag.to_owned());
argv.push(config);
}
let deadline = Instant::now() + Duration::from_secs(spec.timeout_secs);
let mut excluded = Vec::new();
let mut directories = Vec::new();
match spec.arguments {
ToolArguments::Files => argv.extend(file_arguments(files)),
ToolArguments::Project => {}
ToolArguments::GoPackages => {
let plan = match go_packages::plan(&executable, workspace_root, files, deadline).await {
Ok(plan) => plan,
Err(detail) => return ToolOutcome::unavailable(spec, detail),
};
if !plan.load_errors.is_empty() {
return ToolOutcome {
findings: plan.load_errors,
excluded: plan.excluded,
..ToolOutcome::empty(
spec,
ToolStatus::Ok,
"a package failed to load".to_owned(),
)
};
}
if plan.directories.is_empty() {
return ToolOutcome {
excluded: plan.excluded,
..ToolOutcome::empty(
spec,
ToolStatus::Skipped,
"the build excludes every file".to_owned(),
)
};
}
argv.extend(plan.directories.iter().cloned());
directories = plan.directories;
excluded = plan.excluded;
}
}
let mut outcome = execute(spec, &argv, workspace_root, files, &directories, deadline).await;
outcome.excluded = excluded;
outcome
}
async fn execute(
spec: &ToolSpec,
argv: &[String],
workspace_root: &Path,
files: &[String],
directories: &[String],
deadline: Instant,
) -> ToolOutcome {
let output = match run_bounded(&argv[0], &argv[1..], workspace_root, deadline).await {
Ok(output) => output,
Err(Bounded::Failed(err)) => {
return ToolOutcome::unavailable(
spec,
format!("{} could not be executed: {err}", spec.name),
);
}
Err(Bounded::TimedOut) => {
let context = spec.timeout_context.unwrap_or_default();
return ToolOutcome::unavailable(
spec,
format!(
"{} timed out after {}s{context}",
spec.name, spec.timeout_secs
),
);
}
};
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
let (diagnostics, other) = match spec.diagnostics_stream {
DiagnosticsStream::Stderr => (stderr.as_ref(), stdout.as_ref()),
DiagnosticsStream::Stdout => (stdout.as_ref(), stderr.as_ref()),
};
let parse_result = parse_output(
spec,
diagnostics,
files.first().map(String::as_str).unwrap_or(""),
);
let compilation_succeeded = spec.establishes_compilation && output.status.success();
match parse_result {
Ok(findings)
if findings.is_empty() && !output.status.success() && diagnostics.trim().is_empty() =>
{
ToolOutcome::unavailable(
spec,
format!(
"{} exited {} without producing diagnostics: {}",
spec.name,
exit_word(&output.status),
stream_detail(other)
),
)
}
Ok(findings)
if findings.is_empty()
&& !output.status.success()
&& spec.output_format.skips_unmatched_input() =>
{
ToolOutcome::unavailable(
spec,
format!(
"{} exited {} without a recognisable diagnostic: {}",
spec.name,
exit_word(&output.status),
stream_detail(diagnostics)
),
)
}
Ok(findings) => ToolOutcome {
tool: spec.name,
status: ToolStatus::Ok,
findings: retain_requested(spec, findings, files, directories, workspace_root),
detail: stream_detail(other),
compilation_succeeded,
excluded: Vec::new(),
},
Err(err) => ToolOutcome::unavailable(
spec,
format!("{err}. other stream: {}", stream_detail(other)),
),
}
}
pub(super) enum Bounded {
Failed(std::io::Error),
TimedOut,
}
pub(super) async fn run_bounded(
program: impl AsRef<OsStr>,
args: impl IntoIterator<Item = impl AsRef<OsStr>>,
cwd: &Path,
deadline: Instant,
) -> Result<Output, Bounded> {
let mut command = Command::new(program);
command.args(args);
command.current_dir(cwd);
command.stdin(Stdio::null());
command.stdout(Stdio::piped());
command.stderr(Stdio::piped());
command.kill_on_drop(true);
let child = command.spawn().map_err(Bounded::Failed)?;
match tokio::time::timeout_at(deadline, child.wait_with_output()).await {
Ok(result) => result.map_err(Bounded::Failed),
Err(_) => Err(Bounded::TimedOut),
}
}
fn file_arguments(files: &[String]) -> impl Iterator<Item = String> + '_ {
files.iter().map(|f| {
if f.starts_with('-') {
format!("./{f}")
} else {
f.clone()
}
})
}
pub(crate) fn tool_status_at(
spec: &ToolSpec,
repository_root: &Path,
workspace_root: &Path,
) -> ToolOutcome {
match eligible_executable(spec, repository_root, workspace_root) {
Ok(_) => ToolOutcome::ready(spec),
Err(outcome) => outcome,
}
}
fn eligible_executable(
spec: &ToolSpec,
repository_root: &Path,
workspace_root: &Path,
) -> Result<PathBuf, ToolOutcome> {
if !is_configured(spec, workspace_root) {
return Err(ToolOutcome::skipped(spec));
}
if let Some(executable) = resolve_tool_at(
spec,
repository_root,
workspace_root,
std::env::var_os("PATH").as_deref(),
) {
return Ok(executable);
}
let looked = if spec.local_paths.is_empty() {
"PATH".to_owned()
} else if absolute(repository_root) == absolute(workspace_root) {
format!("{}, then PATH", spec.local_paths.join(", "))
} else {
format!(
"{} from {} through {}, then PATH",
spec.local_paths.join(", "),
workspace_root.display(),
repository_root.display()
)
};
Err(ToolOutcome::unavailable(
spec,
format!("configured but not found (looked in {looked})"),
))
}
pub(crate) fn stream_detail(stream: &str) -> String {
let trimmed = stream.trim();
if trimmed.is_empty() {
return String::new();
}
crate::text::excerpt(trimmed, DETAIL_MAX_CHARS)
}
const DETAIL_MAX_CHARS: usize = 200;
fn exit_word(status: &std::process::ExitStatus) -> String {
status
.code()
.map_or("by signal".to_owned(), |code| code.to_string())
}