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

Discover all .nix files in nil diagnostics #127

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
40 changes: 40 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/nil/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ argh = "0.1.10"
async-lsp = { version = "0.2.0", features = ["tokio"] }
codespan-reporting = "0.11.1"
ide = { path = "../ide" }
ignore = "0.4.22"
log = "0.4.17"
lsp-types = "0.95.0"
macro_rules_attribute = "0.2.0"
Expand Down
123 changes: 94 additions & 29 deletions crates/nil/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
use anyhow::{Context, Result};
use argh::FromArgs;
use codespan_reporting::diagnostic::Severity;
use codespan_reporting::term::termcolor::WriteColor;
use ide::{AnalysisHost, Severity};
use ide::AnalysisHost;
use ide::Change;
use ide::FileId;
use ide::FileSet;
use ide::SourceRoot;
use ide::VfsPath;
use ignore::types::TypesBuilder;
use ignore::WalkBuilder;
use std::io::IsTerminal;
use std::path::{Path, PathBuf};
use std::sync::Arc;
Expand Down Expand Up @@ -39,15 +47,15 @@ enum Subcommand {

#[derive(Debug, FromArgs)]
#[argh(subcommand, name = "diagnostics")]
/// Check and print diagnostics for a file.
/// Check and print diagnostics for Nix files.
/// Exit with non-zero code if there are any diagnostics. (`1` for errors, `2` if only warnings)
/// WARNING: The output format is for human and should not be relied on.
struct DiagnosticsArgs {
/// nix file to check, or read from stdin for `-`.
/// nix files to check, or read from stdin for `-`.
/// NB. You need `--` before `-` for paths starting with `-`,
/// to disambiguous it from flags.
#[argh(positional)]
path: PathBuf,
paths: Vec<PathBuf>,
}

#[derive(Debug, FromArgs)]
Expand Down Expand Up @@ -76,6 +84,8 @@ fn main() {
return;
}

setup_logger();

if let Some(subcommand) = args.subcommand {
return match subcommand {
Subcommand::Diagnostics(args) => main_diagnostics(args),
Expand All @@ -84,8 +94,6 @@ fn main() {
};
}

setup_logger();

if !args.stdio && (io::stdin().is_terminal() || io::stdout().is_terminal()) {
// TODO: Make this a hard error.
eprintln!(
Expand All @@ -111,32 +119,12 @@ fn main() {
}

fn main_diagnostics(args: DiagnosticsArgs) {
use codespan_reporting::term::termcolor::{ColorChoice, StandardStream};

let ret = (|| -> Result<Option<Severity>> {
let path = &*args.path;

let src = if path.as_os_str() == "-" {
io::read_to_string(io::stdin().lock()).context("Failed to read from stdin")?
} else {
fs::read_to_string(path).context("Failed to read file")?
};

let (analysis, file) = AnalysisHost::new_single_file(&src);
let diags = analysis
.snapshot()
.diagnostics(file)
.expect("No cancellation");

let mut writer = StandardStream::stdout(ColorChoice::Auto);
emit_diagnostics(path, &src, &mut writer, &mut diags.iter().cloned())?;
let ret = diagnostics_for_files(args.paths);

Ok(diags.iter().map(|diag| diag.severity()).max())
})();
match ret {
Ok(None) => process::exit(0),
Ok(Some(max_severity)) => {
if max_severity > Severity::Warning {
if max_severity > ide::Severity::Warning {
process::exit(1)
} else {
process::exit(0)
Expand All @@ -149,6 +137,83 @@ fn main_diagnostics(args: DiagnosticsArgs) {
}
}

fn diagnostics_for_files(mut roots: Vec<PathBuf>) -> Result<Option<ide::Severity>> {
use codespan_reporting::term::termcolor::{ColorChoice, StandardStream};
let mut walk = if roots.is_empty() {
WalkBuilder::new(".")
} else {
WalkBuilder::new(roots.pop().expect("non_empty vec has an item"))
};

walk.types({
let mut builder = TypesBuilder::new();
builder
.add("nix", "*.nix")
.expect("parsing glob '*.nix' will never fail");
builder.select("nix");
builder.build().expect("building types will never fail")
});

for root in roots {
walk.add(root);
}

let walk = walk.build();

let mut sources = Vec::new();
let mut file_set = FileSet::default();
let mut change = Change::default();
for (id, entry) in walk.enumerate() {
let entry = match entry {
Ok(entry) => entry,
Err(err) => {
tracing::error!("{err}");
continue;
}
};

let file = FileId(id as u32);
let src = if entry.is_stdin() {
io::read_to_string(io::stdin().lock()).context("Failed to read from stdin")?
} else {
if entry.path().is_dir() {
continue;
}
fs::read_to_string(entry.path()).context("Failed to read file")?
};

let path = entry.into_path();
let src: Arc<str> = src.into();
change.change_file(file, src.clone());
file_set.insert(file, VfsPath::new(&path));
sources.push((file, path, src));
}

change.set_roots(vec![SourceRoot::new_local(file_set, None)]);

let mut analysis = AnalysisHost::new();
analysis.apply_change(change);

let snapshot = analysis.snapshot();

let mut writer = StandardStream::stdout(ColorChoice::Auto);
let mut max_severity = None;
for (id, path, src) in sources {
let diagnostics = snapshot.diagnostics(id).expect("No cancellation");
emit_diagnostics(&path, &src, &mut writer, &mut diagnostics.iter().cloned())?;

max_severity = std::cmp::max(
max_severity,
diagnostics
.iter()
.map(|diagnostic| diagnostic.severity())
.max(),
);
}

Ok(max_severity)
}

fn main_parse(args: ParseArgs) {
use codespan_reporting::term::termcolor::{ColorChoice, StandardStream};

Expand Down Expand Up @@ -255,7 +320,7 @@ fn emit_diagnostics(
writer: &mut dyn WriteColor,
diags: &mut dyn Iterator<Item = ide::Diagnostic>,
) -> Result<()> {
use codespan_reporting::diagnostic::{Diagnostic, Label, Severity};
use codespan_reporting::diagnostic::{Diagnostic, Label};
use codespan_reporting::files::SimpleFiles;
use codespan_reporting::term;

Expand Down