-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathtraverse.rs
More file actions
551 lines (486 loc) · 19.3 KB
/
traverse.rs
File metadata and controls
551 lines (486 loc) · 19.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
use super::process_file::{FileStatus, Message, process_file};
use super::{Execution, TraversalMode};
use crate::cli_options::CliOptions;
use crate::execute::diagnostics::PanicDiagnostic;
use crate::reporter::TraversalSummary;
use crate::{CliDiagnostic, CliSession};
use crossbeam::channel::{Receiver, Sender, unbounded};
use pglt_diagnostics::DiagnosticTags;
use pglt_diagnostics::{DiagnosticExt, Error, Resource, Severity};
use pglt_fs::{FileSystem, PathInterner, PgLTPath};
use pglt_fs::{TraversalContext, TraversalScope};
use pglt_workspace::dome::Dome;
use pglt_workspace::workspace::IsPathIgnoredParams;
use pglt_workspace::{Workspace, WorkspaceError};
use rustc_hash::FxHashSet;
use std::collections::BTreeSet;
use std::sync::RwLock;
use std::sync::atomic::AtomicU32;
use std::{
env::current_dir,
ffi::OsString,
panic::catch_unwind,
path::PathBuf,
sync::{
Once,
atomic::{AtomicUsize, Ordering},
},
thread,
time::{Duration, Instant},
};
pub(crate) struct TraverseResult {
pub(crate) summary: TraversalSummary,
pub(crate) evaluated_paths: BTreeSet<PgLTPath>,
pub(crate) diagnostics: Vec<Error>,
}
pub(crate) fn traverse(
execution: &Execution,
session: &mut CliSession,
cli_options: &CliOptions,
mut inputs: Vec<OsString>,
) -> Result<TraverseResult, CliDiagnostic> {
init_thread_pool();
if inputs.is_empty() {
match &execution.traversal_mode {
TraversalMode::Dummy => {
// If `--staged` or `--changed` is specified, it's acceptable for them to be empty, so ignore it.
if !execution.is_vcs_targeted() {
match current_dir() {
Ok(current_dir) => inputs.push(current_dir.into_os_string()),
Err(err) => return Err(CliDiagnostic::io_error(err)),
}
}
}
_ => {
if execution.as_stdin_file().is_none() && !cli_options.no_errors_on_unmatched {
return Err(CliDiagnostic::missing_argument(
"<INPUT>",
format!("{}", execution.traversal_mode),
));
}
}
}
}
let (interner, recv_files) = PathInterner::new();
let (sender, receiver) = unbounded();
let changed = AtomicUsize::new(0);
let unchanged = AtomicUsize::new(0);
let matches = AtomicUsize::new(0);
let skipped = AtomicUsize::new(0);
let fs = &*session.app.fs;
let workspace = &*session.app.workspace;
let max_diagnostics = execution.get_max_diagnostics();
let remaining_diagnostics = AtomicU32::new(max_diagnostics);
let printer = DiagnosticsPrinter::new(execution)
.with_verbose(cli_options.verbose)
.with_diagnostic_level(cli_options.diagnostic_level)
.with_max_diagnostics(max_diagnostics);
let (duration, evaluated_paths, diagnostics) = thread::scope(|s| {
let handler = thread::Builder::new()
.name(String::from("pglt::console"))
.spawn_scoped(s, || printer.run(receiver, recv_files))
.expect("failed to spawn console thread");
// The traversal context is scoped to ensure all the channels it
// contains are properly closed once the traversal finishes
let (elapsed, evaluated_paths) = traverse_inputs(
fs,
inputs,
&TraversalOptions {
fs,
workspace,
execution,
interner,
matches: &matches,
changed: &changed,
unchanged: &unchanged,
skipped: &skipped,
messages: sender,
remaining_diagnostics: &remaining_diagnostics,
evaluated_paths: RwLock::default(),
},
);
// wait for the main thread to finish
let diagnostics = handler.join().unwrap();
(elapsed, evaluated_paths, diagnostics)
});
let errors = printer.errors();
let warnings = printer.warnings();
let changed = changed.load(Ordering::Relaxed);
let unchanged = unchanged.load(Ordering::Relaxed);
let matches = matches.load(Ordering::Relaxed);
let skipped = skipped.load(Ordering::Relaxed);
let suggested_fixes_skipped = printer.skipped_fixes();
let diagnostics_not_printed = printer.not_printed_diagnostics();
Ok(TraverseResult {
summary: TraversalSummary {
changed,
unchanged,
duration,
errors,
matches,
warnings,
skipped,
suggested_fixes_skipped,
diagnostics_not_printed,
},
evaluated_paths,
diagnostics,
})
}
/// This function will setup the global Rayon thread pool the first time it's called
///
/// This is currently only used to assign friendly debug names to the threads of the pool
fn init_thread_pool() {
static INIT_ONCE: Once = Once::new();
INIT_ONCE.call_once(|| {
rayon::ThreadPoolBuilder::new()
.thread_name(|index| format!("pglt::worker_{index}"))
.build_global()
.expect("failed to initialize the global thread pool");
});
}
/// Initiate the filesystem traversal tasks with the provided input paths and
/// run it to completion, returning the duration of the process and the evaluated paths
fn traverse_inputs(
fs: &dyn FileSystem,
inputs: Vec<OsString>,
ctx: &TraversalOptions,
) -> (Duration, BTreeSet<PgLTPath>) {
let start = Instant::now();
fs.traversal(Box::new(move |scope: &dyn TraversalScope| {
for input in inputs {
scope.evaluate(ctx, PathBuf::from(input));
}
}));
let paths = ctx.evaluated_paths();
let dome = Dome::new(paths);
let mut iter = dome.iter();
fs.traversal(Box::new(|scope: &dyn TraversalScope| {
while let Some(path) = iter.next_config() {
scope.handle(ctx, path.to_path_buf());
}
for path in iter {
scope.handle(ctx, path.to_path_buf());
}
}));
(start.elapsed(), ctx.evaluated_paths())
}
// struct DiagnosticsReporter<'ctx> {}
struct DiagnosticsPrinter<'ctx> {
/// Execution of the traversal
execution: &'ctx Execution,
/// The maximum number of diagnostics the console thread is allowed to print
max_diagnostics: u32,
/// The approximate number of diagnostics the console will print before
/// folding the rest into the "skipped diagnostics" counter
remaining_diagnostics: AtomicU32,
/// Mutable reference to a boolean flag tracking whether the console thread
/// printed any error-level message
errors: AtomicU32,
/// Mutable reference to a boolean flag tracking whether the console thread
/// printed any warnings-level message
warnings: AtomicU32,
/// Whether the console thread should print diagnostics in verbose mode
verbose: bool,
/// The diagnostic level the console thread should print
diagnostic_level: Severity,
not_printed_diagnostics: AtomicU32,
printed_diagnostics: AtomicU32,
total_skipped_suggested_fixes: AtomicU32,
}
impl<'ctx> DiagnosticsPrinter<'ctx> {
fn new(execution: &'ctx Execution) -> Self {
Self {
errors: AtomicU32::new(0),
warnings: AtomicU32::new(0),
remaining_diagnostics: AtomicU32::new(0),
execution,
diagnostic_level: Severity::Hint,
verbose: false,
max_diagnostics: 20,
not_printed_diagnostics: AtomicU32::new(0),
printed_diagnostics: AtomicU32::new(0),
total_skipped_suggested_fixes: AtomicU32::new(0),
}
}
fn with_verbose(mut self, verbose: bool) -> Self {
self.verbose = verbose;
self
}
fn with_max_diagnostics(mut self, value: u32) -> Self {
self.max_diagnostics = value;
self
}
fn with_diagnostic_level(mut self, value: Severity) -> Self {
self.diagnostic_level = value;
self
}
fn errors(&self) -> u32 {
self.errors.load(Ordering::Relaxed)
}
fn warnings(&self) -> u32 {
self.warnings.load(Ordering::Relaxed)
}
fn not_printed_diagnostics(&self) -> u32 {
self.not_printed_diagnostics.load(Ordering::Relaxed)
}
fn skipped_fixes(&self) -> u32 {
self.total_skipped_suggested_fixes.load(Ordering::Relaxed)
}
/// Checks if the diagnostic we received from the thread should be considered or not. Logic:
/// - it should not be considered if its severity level is lower than the one provided via CLI;
/// - it should not be considered if it's a verbose diagnostic and the CLI **didn't** request a `--verbose` option.
fn should_skip_diagnostic(&self, severity: Severity, diagnostic_tags: DiagnosticTags) -> bool {
if severity < self.diagnostic_level {
return true;
}
if diagnostic_tags.is_verbose() && !self.verbose {
return true;
}
false
}
/// Count the diagnostic, and then returns a boolean that tells if it should be printed
fn should_print(&self) -> bool {
let printed_diagnostics = self.printed_diagnostics.load(Ordering::Relaxed);
let should_print = printed_diagnostics < self.max_diagnostics;
if should_print {
self.printed_diagnostics.fetch_add(1, Ordering::Relaxed);
self.remaining_diagnostics.store(
self.max_diagnostics.saturating_sub(printed_diagnostics),
Ordering::Relaxed,
);
} else {
self.not_printed_diagnostics.fetch_add(1, Ordering::Relaxed);
}
should_print
}
fn run(&self, receiver: Receiver<Message>, interner: Receiver<PathBuf>) -> Vec<Error> {
let mut paths: FxHashSet<String> = FxHashSet::default();
let mut diagnostics_to_print = vec![];
while let Ok(msg) = receiver.recv() {
match msg {
Message::SkippedFixes {
skipped_suggested_fixes,
} => {
self.total_skipped_suggested_fixes
.fetch_add(skipped_suggested_fixes, Ordering::Relaxed);
}
Message::Failure => {
self.errors.fetch_add(1, Ordering::Relaxed);
}
Message::Error(mut err) => {
let location = err.location();
if self.should_skip_diagnostic(err.severity(), err.tags()) {
continue;
}
if err.severity() == Severity::Warning {
// *warnings += 1;
self.warnings.fetch_add(1, Ordering::Relaxed);
// self.warnings.set(self.warnings.get() + 1)
}
if let Some(Resource::File(file_path)) = location.resource.as_ref() {
// Retrieves the file name from the file ID cache, if it's a miss
// flush entries from the interner channel until it's found
let file_name = match paths.get(*file_path) {
Some(path) => Some(path),
None => loop {
match interner.recv() {
Ok(path) => {
paths.insert(path.display().to_string());
if path.display().to_string() == *file_path {
break paths.get(&path.display().to_string());
}
}
// In case the channel disconnected without sending
// the path we need, print the error without a file
// name (normally this should never happen)
Err(_) => break None,
}
},
};
if let Some(path) = file_name {
err = err.with_file_path(path.as_str());
}
}
let should_print = self.should_print();
if should_print {
diagnostics_to_print.push(err);
}
}
Message::Diagnostics {
name,
content,
diagnostics,
skipped_diagnostics,
} => {
self.not_printed_diagnostics
.fetch_add(skipped_diagnostics, Ordering::Relaxed);
// is CI mode we want to print all the diagnostics
for diag in diagnostics {
let severity = diag.severity();
if self.should_skip_diagnostic(severity, diag.tags()) {
continue;
}
if severity == Severity::Error {
self.errors.fetch_add(1, Ordering::Relaxed);
}
if severity == Severity::Warning {
self.warnings.fetch_add(1, Ordering::Relaxed);
}
let should_print = self.should_print();
if should_print {
let diag = diag.with_file_path(&name).with_file_source_code(&content);
diagnostics_to_print.push(diag)
}
}
}
}
}
diagnostics_to_print
}
}
/// Context object shared between directory traversal tasks
pub(crate) struct TraversalOptions<'ctx, 'app> {
/// Shared instance of [FileSystem]
pub(crate) fs: &'app dyn FileSystem,
/// Instance of [Workspace] used by this instance of the CLI
pub(crate) workspace: &'ctx dyn Workspace,
/// Determines how the files should be processed
pub(crate) execution: &'ctx Execution,
/// File paths interner cache used by the filesystem traversal
interner: PathInterner,
/// Shared atomic counter storing the number of changed files
changed: &'ctx AtomicUsize,
/// Shared atomic counter storing the number of unchanged files
unchanged: &'ctx AtomicUsize,
/// Shared atomic counter storing the number of unchanged files
matches: &'ctx AtomicUsize,
/// Shared atomic counter storing the number of skipped files
skipped: &'ctx AtomicUsize,
/// Channel sending messages to the display thread
pub(crate) messages: Sender<Message>,
/// The approximate number of diagnostics the console will print before
/// folding the rest into the "skipped diagnostics" counter
pub(crate) remaining_diagnostics: &'ctx AtomicU32,
/// List of paths that should be processed
pub(crate) evaluated_paths: RwLock<BTreeSet<PgLTPath>>,
}
impl TraversalOptions<'_, '_> {
pub(crate) fn increment_changed(&self, path: &PgLTPath) {
self.changed.fetch_add(1, Ordering::Relaxed);
self.evaluated_paths
.write()
.unwrap()
.replace(path.to_written());
}
pub(crate) fn increment_unchanged(&self) {
self.unchanged.fetch_add(1, Ordering::Relaxed);
}
pub(crate) fn increment_matches(&self, num_matches: usize) {
self.matches.fetch_add(num_matches, Ordering::Relaxed);
}
/// Send a message to the display thread
pub(crate) fn push_message(&self, msg: impl Into<Message>) {
self.messages.send(msg.into()).ok();
}
pub(crate) fn protected_file(&self, pglt_path: &PgLTPath) {
self.push_diagnostic(WorkspaceError::protected_file(pglt_path.display().to_string()).into())
}
}
impl TraversalContext for TraversalOptions<'_, '_> {
fn interner(&self) -> &PathInterner {
&self.interner
}
fn evaluated_paths(&self) -> BTreeSet<PgLTPath> {
self.evaluated_paths.read().unwrap().clone()
}
fn push_diagnostic(&self, error: Error) {
self.push_message(error);
}
fn can_handle(&self, pglt_path: &PgLTPath) -> bool {
let path = pglt_path.as_path();
let is_valid_file = self.fs.path_is_file(path)
&& path
.extension()
.is_some_and(|ext| ext == "sql" || ext == "pg");
if self.fs.path_is_dir(path) || self.fs.path_is_symlink(path) || is_valid_file {
// handle:
// - directories
// - symlinks
// - unresolved symlinks
// e.g `symlink/subdir` where symlink points to a directory that includes `subdir`.
// Note that `symlink/subdir` is not an existing file.
let can_handle = !self
.workspace
.is_path_ignored(IsPathIgnoredParams {
pglt_path: pglt_path.clone(),
})
.unwrap_or_else(|err| {
self.push_diagnostic(err.into());
false
});
return can_handle;
}
// bail on fifo and socket files
if !is_valid_file {
return false;
}
match self.execution.traversal_mode() {
TraversalMode::Dummy => true,
TraversalMode::Check { .. } => true,
}
}
fn handle_path(&self, path: PgLTPath) {
handle_file(self, &path)
}
fn store_path(&self, path: PgLTPath) {
self.evaluated_paths
.write()
.unwrap()
.insert(PgLTPath::new(path.as_path()));
}
}
/// This function wraps the [process_file] function implementing the traversal
/// in a [catch_unwind] block and emit diagnostics in case of error (either the
/// traversal function returns Err or panics)
fn handle_file(ctx: &TraversalOptions, path: &PgLTPath) {
match catch_unwind(move || process_file(ctx, path)) {
Ok(Ok(FileStatus::Changed)) => {
ctx.increment_changed(path);
}
Ok(Ok(FileStatus::Unchanged)) => {
ctx.increment_unchanged();
}
Ok(Ok(FileStatus::SearchResult(num_matches, msg))) => {
ctx.increment_unchanged();
ctx.increment_matches(num_matches);
ctx.push_message(msg);
}
Ok(Ok(FileStatus::Message(msg))) => {
ctx.increment_unchanged();
ctx.push_message(msg);
}
Ok(Ok(FileStatus::Protected(file_path))) => {
ctx.increment_unchanged();
ctx.push_diagnostic(WorkspaceError::protected_file(file_path).into());
}
Ok(Ok(FileStatus::Ignored)) => {}
Ok(Err(err)) => {
ctx.increment_unchanged();
ctx.skipped.fetch_add(1, Ordering::Relaxed);
ctx.push_message(err);
}
Err(err) => {
let message = match err.downcast::<String>() {
Ok(msg) => format!("processing panicked: {msg}"),
Err(err) => match err.downcast::<&'static str>() {
Ok(msg) => format!("processing panicked: {msg}"),
Err(_) => String::from("processing panicked"),
},
};
ctx.push_message(
PanicDiagnostic { message }.with_file_path(path.display().to_string()),
);
}
}
}