Skip to content
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
2 changes: 2 additions & 0 deletions Cargo.lock

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

16 changes: 8 additions & 8 deletions MODULE.bazel.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions crates/cli-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ strum.workspace = true
strum_macros.workspace = true
fern = "0.7"
log.workspace = true
ignore = "0.4.26"
anstyle = "1.0"
clap = { version = "4.6.1", features = ["derive"] }
pyo3 = { version = "0.29.0", optional = true }
Expand Down
103 changes: 46 additions & 57 deletions crates/cli-lib/src/commands_fix.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::commands::FixArgs;
use crate::commands::Format;
use crate::linter;
use crate::commands_lint::{ApplyFixes, Input, LintCommand, run_lint_command};
use sqruff_lib::api::Mode;
use sqruff_lib::core::config::FluffConfig;
use std::path::Path;

Expand All @@ -11,41 +12,17 @@ pub(crate) fn run_fix(
collect_parse_errors: bool,
) -> i32 {
let FixArgs { paths, format } = args;
let mut linter = match linter(config, format, collect_parse_errors) {
Ok(l) => l,
Err(e) => {
eprintln!("{}", e);
return 1;
}
};
let result = match linter.lint_paths(paths, true, &ignorer) {
Ok(result) => result,
Err(e) => {
eprintln!("{}", e.value);
return 1;
}
};

if !result.has_violations() {
println!("{} files processed, nothing to fix.", result.len());
0
} else {
let any_unfixable_errors = result.has_unfixable_violations();
let files = result.len();

for mut file in result {
if !file.has_fixes() {
continue;
}
let path = std::mem::take(&mut file.path);
let fixed = file.fix_string();
std::fs::write(path, fixed).unwrap();
}

linter.formatter_mut().unwrap().completion_message(files);

any_unfixable_errors as i32
}
run_lint_command(
LintCommand {
mode: Mode::Fix,
input: Input::Paths(paths),
apply: ApplyFixes::ToDisk,
format,
},
config,
ignorer,
collect_parse_errors,
)
}

pub(crate) fn run_fix_stdin(
Expand All @@ -55,27 +32,17 @@ pub(crate) fn run_fix_stdin(
) -> i32 {
let read_in = crate::stdin::read_std_in().unwrap();

let linter = match linter(config, format, collect_parse_errors) {
Ok(l) => l,
Err(e) => {
eprintln!("{}", e);
return 1;
}
};
let result = match linter.lint_string(&read_in, None, true) {
Ok(result) => result,
Err(e) => {
eprintln!("{}", e.value);
return 1;
}
};

let has_unfixable_errors = result.has_unfixable_violations();

println!("{}", result.fix_string());

// if all fixable violations are fixable, return 0 else return 1
has_unfixable_errors as i32
run_lint_command(
LintCommand {
mode: Mode::Fix,
input: Input::Stdin(read_in),
apply: ApplyFixes::Stdout,
format,
},
config,
|_| false,
collect_parse_errors,
)
}

#[cfg(test)]
Expand Down Expand Up @@ -112,4 +79,26 @@ mod tests {
let after = std::fs::metadata(&path).unwrap().modified().unwrap();
assert_eq!(before, after);
}

#[test]
fn run_fix_writes_file_when_changes_exist() {
let mut tmp = NamedTempFile::new().unwrap();
write!(tmp, "SELECT foo bar FROM tabs").unwrap();
tmp.flush().unwrap();
let tmp = tmp.into_temp_path();
let path = tmp.to_path_buf();

let args = FixArgs {
paths: vec![path.clone()],
format: Format::Human,
};
let config = FluffConfig::from_source("[sqruff]\nrules = AL02\n", None);
let exit_code = run_fix(args, config, ignore_none, true);

assert_eq!(exit_code, 0);
assert_eq!(
std::fs::read_to_string(&path).unwrap(),
"SELECT foo AS bar FROM tabs"
);
}
}
207 changes: 180 additions & 27 deletions crates/cli-lib/src/commands_lint.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
use crate::commands::{Format, LintArgs};
use crate::linter;
use crate::reporters::Reporter;
use sqruff_lib::api::{
Engine, EngineOptions, FileReport, IgnoreMatcher, Mode, ParseErrors, PathDiscoveryOptions,
RunRequest, Source, SourceId, Workspace,
};
use sqruff_lib::core::config::FluffConfig;
use std::path::Path;
use std::borrow::Cow;
use std::path::{Path, PathBuf};

pub(crate) struct LintCommand {
pub mode: Mode,
pub input: Input,
pub apply: ApplyFixes,
pub format: Format,
}

pub(crate) enum Input {
Paths(Vec<PathBuf>),
Stdin(String),
}

pub(crate) enum ApplyFixes {
Never,
ToDisk,
Stdout,
}

pub(crate) fn run_lint(
args: LintArgs,
Expand All @@ -10,24 +33,17 @@ pub(crate) fn run_lint(
collect_parse_errors: bool,
) -> i32 {
let LintArgs { paths, format } = args;
let mut linter = match linter(config, format, collect_parse_errors) {
Ok(l) => l,
Err(e) => {
eprintln!("{}", e);
return 1;
}
};
let result = match linter.lint_paths(paths, false, &ignorer) {
Ok(result) => result,
Err(e) => {
eprintln!("{}", e.value);
return 1;
}
};

linter.formatter().unwrap().completion_message(result.len());

result.has_violations() as i32
run_lint_command(
LintCommand {
mode: Mode::Check,
input: Input::Paths(paths),
apply: ApplyFixes::Never,
format,
},
config,
ignorer,
collect_parse_errors,
)
}

pub(crate) fn run_lint_stdin(
Expand All @@ -37,22 +53,159 @@ pub(crate) fn run_lint_stdin(
) -> i32 {
let read_in = crate::stdin::read_std_in().unwrap();

let linter = match linter(config, format, collect_parse_errors) {
Ok(l) => l,
run_lint_command(
LintCommand {
mode: Mode::Check,
input: Input::Stdin(read_in),
apply: ApplyFixes::Never,
format,
},
config,
|_| false,
collect_parse_errors,
)
}

pub(crate) fn run_lint_command(
command: LintCommand,
config: FluffConfig,
ignorer: impl Fn(&Path) -> bool + Send + Sync,
collect_parse_errors: bool,
) -> i32 {
let mut reporter = Reporter::new(command.format, &config);
let workspace_root = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
let workspace = match Workspace::new(workspace_root.clone()) {
Ok(workspace) => workspace,
Err(e) => {
eprintln!("{}", e.value);
return 1;
}
};
let loaded_sources = match load_sources(&command.input, &workspace, &workspace_root, &ignorer) {
Ok(sources) => sources,
Err(e) => {
eprintln!("{}", e);
eprintln!("{}", e.value);
return 1;
}
};
let result = match linter.lint_string(&read_in, None, false) {
Ok(result) => result,
let engine = match Engine::new(
config,
EngineOptions {
parse_errors: if collect_parse_errors {
ParseErrors::Include
} else {
ParseErrors::Suppress
},
},
) {
Ok(engine) => engine,
Err(e) => {
eprintln!("{}", e.value);
return 1;
}
};
let sources = loaded_sources
.iter()
.map(|loaded| Source {
id: loaded.id.clone(),
text: Cow::Borrowed(loaded.text.as_ref()),
})
.collect();
let report = match engine.run(RunRequest {
mode: command.mode,
sources,
}) {
Ok(report) => report,
Err(e) => {
eprintln!("{}", e.value);
return 1;
}
};

let files = report.files.len();
let has_violations = report.files.iter().any(|file| !file.diagnostics.is_empty());

linter.formatter().unwrap().completion_message(1);
match command.apply {
ApplyFixes::Never => {
if let Err(error) = reporter.emit(&report) {
eprintln!("{error}");
return 1;
}
has_violations as i32
}
ApplyFixes::Stdout => {
let any_unfixable_errors = report.files.iter().any(has_unfixable_diagnostics);
if let Err(error) = reporter.emit_diagnostics(&report) {
eprintln!("{error}");
return 1;
}
for file in report.files {
if let Some(fixed_source) = file.fixed_source {
println!("{fixed_source}");
}
}

any_unfixable_errors as i32
}
ApplyFixes::ToDisk => {
if !has_violations {
println!("{files} files processed, nothing to fix.");
return 0;
}

let any_unfixable_errors = report.files.iter().any(has_unfixable_diagnostics);

if let Err(e) = workspace.apply_fixes(&report) {
eprintln!("{}", e.value);
return 1;
}

if let Err(error) = reporter.emit(&report) {
eprintln!("{error}");
return 1;
}
any_unfixable_errors as i32
}
}
}

fn load_sources(
input: &Input,
workspace: &Workspace,
working_dir: &Path,
ignorer: &(dyn Fn(&Path) -> bool + Send + Sync),
) -> Result<Vec<Source<'static>>, sqruff_lib::api::SqruffError> {
match input {
Input::Stdin(text) => Ok(vec![Source {
id: SourceId::Stdin,
text: Cow::Owned(text.clone()),
}]),
Input::Paths(paths) => {
let ignore_matcher = ClosureIgnoreMatcher { ignorer };
let options = PathDiscoveryOptions {
ignore_file_name: ".sqruffignore",
ignore_non_existent_files: false,
ignore_files: true,
working_dir: working_dir.to_path_buf(),
ignorer: Some(&ignore_matcher),
};
workspace.discover_sources(paths, &options)
}
}
}

struct ClosureIgnoreMatcher<'a> {
ignorer: &'a (dyn Fn(&Path) -> bool + Send + Sync),
}

impl IgnoreMatcher for ClosureIgnoreMatcher<'_> {
fn is_ignored(&self, path: &Path) -> bool {
(self.ignorer)(path)
}
}

result.has_violations() as i32
fn has_unfixable_diagnostics(file: &FileReport) -> bool {
file.diagnostics
.iter()
.any(|diagnostic| !diagnostic.fixable)
}
2 changes: 1 addition & 1 deletion crates/cli-lib/src/commands_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn parse_and_output_tree(
format: ParseFormat,
) -> i32 {
// Create a linter and parse the SQL
let linter = match Linter::new(config.clone(), None, None, true) {
let linter = match Linter::new(config.clone(), None, true) {
Ok(l) => l,
Err(e) => {
eprintln!("{}", e);
Expand Down
Loading