use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::path::{Path, PathBuf};
use serde::Deserialize;
use tokio::time::Instant;
use super::{Bounded, TYPE_CHECK_KIND, exit_word, run_bounded, stream_detail};
use crate::analysis::findings::{Finding, Severity};
#[derive(Debug, Default, PartialEq)]
pub(super) struct Plan {
pub(super) directories: Vec<String>,
pub(super) excluded: Vec<String>,
pub(super) load_errors: Vec<Finding>,
}
#[derive(Debug, Default, Deserialize)]
#[serde(rename_all = "PascalCase", default)]
struct Package {
dir: Option<PathBuf>,
go_files: Vec<String>,
cgo_files: Vec<String>,
test_go_files: Vec<String>,
x_test_go_files: Vec<String>,
invalid_go_files: Vec<String>,
error: Option<PackageError>,
deps_errors: Vec<PackageError>,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
struct PackageError {
#[serde(default)]
pos: String,
err: String,
}
const LIST_FIELDS: &str =
"-json=Dir,GoFiles,CgoFiles,TestGoFiles,XTestGoFiles,InvalidGoFiles,Error,DepsErrors";
pub(super) async fn plan(
go: &Path,
workspace_root: &Path,
files: &[String],
deadline: Instant,
) -> Result<Plan, String> {
let mut by_directory: BTreeMap<String, Vec<&String>> = BTreeMap::new();
for file in files {
by_directory
.entry(directory_argument(Path::new(file)))
.or_default()
.push(file);
}
let packages = list(go, workspace_root, by_directory.keys(), deadline).await?;
let mut by_dir: HashMap<PathBuf, Package> = HashMap::new();
for package in packages {
let Some(dir) = package.dir.as_deref().map(canonical) else {
let reason = package.error.as_ref().map_or("no reason given", |e| &e.err);
return Err(format!(
"go list could not place a requested directory: {reason}"
));
};
by_dir.insert(dir, package);
}
let mut plan = Plan::default();
for (directory, requested) in by_directory {
let Some(package) = by_dir.get(&canonical(&workspace_root.join(&directory))) else {
return Err(format!("go list did not report {directory}"));
};
let members: BTreeSet<&str> = [
&package.go_files,
&package.cgo_files,
&package.test_go_files,
&package.x_test_go_files,
&package.invalid_go_files,
]
.into_iter()
.flatten()
.map(String::as_str)
.collect();
let in_build = |file: &str| {
Path::new(file)
.file_name()
.and_then(|name| name.to_str())
.is_some_and(|name| members.contains(name))
};
let (built, excluded): (Vec<&String>, Vec<&String>) =
requested.into_iter().partition(|file| in_build(file));
plan.excluded.extend(excluded.into_iter().cloned());
let Some(first) = built.first() else {
continue;
};
if package.error.is_none() && package.deps_errors.is_empty() {
plan.directories.push(directory);
} else {
let errors = package.error.iter().chain(&package.deps_errors);
plan.load_errors
.extend(errors.map(|error| load_error(error, first)));
}
}
Ok(plan)
}
fn directory_argument(file: &Path) -> String {
match file.parent() {
Some(parent) if !parent.as_os_str().is_empty() => {
format!("./{}", parent.to_string_lossy())
}
_ => ".".to_owned(),
}
}
fn canonical(path: &Path) -> PathBuf {
path.canonicalize().unwrap_or_else(|_| path.to_path_buf())
}
async fn list(
go: &Path,
workspace_root: &Path,
directories: impl Iterator<Item = &String>,
deadline: Instant,
) -> Result<Vec<Package>, String> {
let args = ["list", "-e", LIST_FIELDS]
.iter()
.map(|arg| arg.to_string())
.chain(directories.cloned());
let output = match run_bounded(go, args, workspace_root, deadline).await {
Ok(output) => output,
Err(Bounded::Failed(err)) => return Err(format!("go list could not be executed: {err}")),
Err(Bounded::TimedOut) => return Err("go list timed out".to_owned()),
};
if !output.status.success() {
return Err(format!(
"go list exited {}: {}",
exit_word(&output.status),
stream_detail(&String::from_utf8_lossy(&output.stderr))
));
}
serde_json::Deserializer::from_slice(&output.stdout)
.into_iter::<Package>()
.collect::<Result<_, _>>()
.map_err(|err| format!("go list printed what drep could not read: {err}"))
}
fn load_error(error: &PackageError, fallback: &str) -> Finding {
let (file, line, column) = position(&error.pos).unwrap_or((fallback, 1, None));
Finding::deterministic(
TYPE_CHECK_KIND.to_owned(),
Severity::Error,
file.strip_prefix("./").unwrap_or(file).to_owned(),
line,
column,
error.err.clone(),
None,
)
}
pub(super) fn position(pos: &str) -> Option<(&str, u32, Option<u32>)> {
if pos.is_empty() {
return None;
}
let Some((head, last)) = pos
.rsplit_once(':')
.and_then(|(head, last)| Some((head, last.parse::<u32>().ok()?)))
else {
return Some((pos, 1, None));
};
match head
.rsplit_once(':')
.and_then(|(file, line)| Some((file, line.parse::<u32>().ok()?)))
{
Some((file, line)) => Some((file, line, Some(last))),
None => Some((head, last, None)),
}
}