forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck.rs
More file actions
545 lines (494 loc) · 16.7 KB
/
Copy pathcheck.rs
File metadata and controls
545 lines (494 loc) · 16.7 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
use std::collections::HashSet;
use camino::Utf8PathBuf;
use common::{
InputDb,
config::{Config, WorkspaceConfig},
};
use driver::DriverDataBase;
use driver::cli_target::{CliTarget, resolve_cli_target};
use hir::hir_def::{HirIngot, TopLevelMod};
use mir::build_runtime_package;
use salsa::Setter;
use url::Url;
use crate::dependency_diagnostics::CompilationDiagnostics;
use crate::report::{
copy_input_into_report, create_dir_all_utf8, create_report_staging_dir, enable_panic_report,
normalize_report_out_path, tar_gz_dir, write_report_meta,
};
use crate::workspace_ingot::{
INGOT_REQUIRES_WORKSPACE_ROOT, WorkspaceMemberRef, ingot_has_source_files,
select_workspace_member_paths,
};
#[derive(Debug, Clone)]
struct ReportContext {
root_dir: Utf8PathBuf,
}
fn write_report_file(report: &ReportContext, rel: &str, contents: &str) {
let path = report.root_dir.join(rel);
if let Some(parent) = path.parent() {
let _ = std::fs::create_dir_all(parent);
}
let _ = std::fs::write(path, contents);
}
#[allow(clippy::too_many_arguments)]
pub fn check(
path: &Utf8PathBuf,
ingot: Option<&str>,
force_standalone: bool,
profile: &str,
dump_mir: bool,
report_out: Option<&Utf8PathBuf>,
report_failed_only: bool,
recovery_mode: bool,
) -> Result<bool, String> {
let mut db = DriverDataBase::default();
db.compiler_options()
.set_recovery_mode(&mut db)
.to(recovery_mode);
db.compilation_settings()
.set_profile(&mut db)
.to(profile.into());
let report_root = report_out
.map(|out| -> Result<_, String> {
let staging = create_report_staging_dir("target/fe-check-report-staging")?;
let out = normalize_report_out_path(out)?;
Ok((out, staging))
})
.transpose()?;
let report_ctx = report_root
.as_ref()
.map(|(_, staging)| -> Result<_, String> {
let inputs_dir = staging.join("inputs");
create_dir_all_utf8(&inputs_dir)?;
create_dir_all_utf8(&staging.join("artifacts"))?;
create_dir_all_utf8(&staging.join("errors"))?;
write_report_meta(staging, "fe check report", None);
Ok(ReportContext {
root_dir: staging.clone(),
})
})
.transpose()?;
let target = match resolve_cli_target(&mut db, path, force_standalone) {
Ok(target) => target,
Err(message) => {
if let Some(report) = report_ctx.as_ref() {
write_report_file(report, "errors/cli_target.txt", &format!("{message}\n"));
}
if let Some((out, staging)) = report_root {
let has_errors = true;
let should_write = !report_failed_only || has_errors;
if should_write {
write_check_manifest(&staging, path, dump_mir, has_errors);
if let Err(err) = tar_gz_dir(&staging, &out) {
eprintln!("Error: failed to write report `{out}`: {err}");
eprintln!("Report staging directory left at `{staging}`");
} else {
let _ = std::fs::remove_dir_all(&staging);
println!("wrote report: {out}");
}
} else {
let _ = std::fs::remove_dir_all(&staging);
}
}
return Err(message);
}
};
if let Some(report) = report_ctx.as_ref() {
let inputs_dir = report.root_dir.join("inputs");
let source = match &target {
CliTarget::StandaloneFile(file) => file,
CliTarget::Directory(dir) => dir,
};
if let Err(err) = copy_input_into_report(source, &inputs_dir) {
write_report_file(report, "errors/report_inputs.txt", &format!("{err}\n"));
}
}
let _panic_guard = report_ctx
.as_ref()
.map(|report| enable_panic_report(report.root_dir.join("errors/panic_full.txt")));
let has_errors = match target {
CliTarget::StandaloneFile(file_path) => {
if ingot.is_some() {
eprintln!("Error: {INGOT_REQUIRES_WORKSPACE_ROOT}");
true
} else {
check_single_file(&mut db, &file_path, dump_mir, report_ctx.as_ref())
}
}
CliTarget::Directory(dir_path) => {
check_directory(&mut db, &dir_path, ingot, dump_mir, report_ctx.as_ref())
}
};
if let Some((out, staging)) = report_root {
let should_write = !report_failed_only || has_errors;
if should_write {
write_check_manifest(&staging, path, dump_mir, has_errors);
if let Err(err) = tar_gz_dir(&staging, &out) {
eprintln!("Error: failed to write report `{out}`: {err}");
eprintln!("Report staging directory left at `{staging}`");
} else {
let _ = std::fs::remove_dir_all(&staging);
println!("wrote report: {out}");
}
} else {
let _ = std::fs::remove_dir_all(&staging);
}
}
Ok(has_errors)
}
#[allow(clippy::too_many_arguments)]
fn check_directory(
db: &mut DriverDataBase,
dir_path: &Utf8PathBuf,
ingot: Option<&str>,
dump_mir: bool,
report: Option<&ReportContext>,
) -> bool {
let ingot_url = match dir_url(dir_path) {
Ok(url) => url,
Err(message) => {
eprintln!("{message}");
return true;
}
};
let had_init_diagnostics = driver::init_ingot(db, &ingot_url);
if had_init_diagnostics {
if let Some(report) = report {
write_report_file(
report,
"errors/diagnostics.txt",
"compilation errors while initializing ingot",
);
}
return true;
}
let config = match config_from_db(db, &ingot_url) {
Ok(Some(config)) => config,
Ok(None) => {
if ingot.is_some() {
eprintln!("Error: {INGOT_REQUIRES_WORKSPACE_ROOT}");
return true;
}
eprintln!("Error: No fe.toml file found in the root directory");
return true;
}
Err(err) => {
eprintln!("Error: {err}");
return true;
}
};
match config {
Config::Workspace(workspace) => {
check_workspace(db, dir_path, *workspace, ingot, dump_mir, report)
}
Config::Ingot(_) => {
if ingot.is_some() {
eprintln!("Error: {INGOT_REQUIRES_WORKSPACE_ROOT}");
return true;
}
check_ingot_url(db, &ingot_url, dump_mir, report)
}
}
}
fn config_from_db(db: &DriverDataBase, ingot_url: &Url) -> Result<Option<Config>, String> {
let config_url = ingot_url
.join("fe.toml")
.map_err(|_| format!("Failed to locate fe.toml for {ingot_url}"))?;
let Some(file) = db.workspace().get(db, &config_url) else {
return Ok(None);
};
let config = Config::parse(file.text(db))
.map_err(|err| format!("Failed to parse {config_url}: {err}"))?;
Ok(Some(config))
}
fn dir_url(path: &Utf8PathBuf) -> Result<Url, String> {
let canonical_path = match path.canonicalize_utf8() {
Ok(path) => path,
Err(_) => {
let cwd = std::env::current_dir()
.map_err(|err| format!("Failed to read current directory: {err}"))?;
let cwd = Utf8PathBuf::from_path_buf(cwd)
.map_err(|_| "Current directory is not valid UTF-8".to_string())?;
cwd.join(path)
}
};
Url::from_directory_path(canonical_path.as_str())
.map_err(|_| format!("Error: invalid or non-existent directory path: {path}"))
}
#[allow(clippy::too_many_arguments)]
fn check_ingot_url(
db: &mut DriverDataBase,
ingot_url: &Url,
dump_mir: bool,
report: Option<&ReportContext>,
) -> bool {
if db
.workspace()
.containing_ingot(db, ingot_url.clone())
.is_none()
{
// Check if the issue is a missing fe.toml file
let config_url = match ingot_url.join("fe.toml") {
Ok(url) => url,
Err(_) => {
eprintln!("Error: Invalid ingot directory path");
return true;
}
};
if db.workspace().get(db, &config_url).is_none() {
eprintln!("Error: No fe.toml file found in the root directory");
eprintln!(" Expected fe.toml at: {config_url}");
eprintln!(
" Make sure you're in an fe project directory or create a fe.toml file"
);
} else {
eprintln!("Error: Could not resolve ingot from directory");
}
return true;
}
let mut seen = HashSet::new();
check_ingot_and_dependencies(db, ingot_url, dump_mir, report, &mut seen)
}
#[allow(clippy::too_many_arguments)]
fn check_workspace(
db: &mut DriverDataBase,
dir_path: &Utf8PathBuf,
workspace_config: WorkspaceConfig,
ingot: Option<&str>,
dump_mir: bool,
report: Option<&ReportContext>,
) -> bool {
let workspace_url = match dir_url(dir_path) {
Ok(url) => url,
Err(message) => {
eprintln!("{message}");
return true;
}
};
let members = match driver::workspace_members(&workspace_config.workspace, &workspace_url) {
Ok(members) => members,
Err(err) => {
eprintln!("Error: Failed to resolve workspace members: {err}");
return true;
}
};
if members.is_empty() {
let paths: Vec<&str> = workspace_config
.workspace
.members
.iter()
.map(|m| m.path.as_str())
.collect();
if paths.is_empty() {
eprintln!("Warning: No workspace members configured in fe.toml");
} else {
eprintln!(
"Warning: No workspace members found. The configured member paths do not exist:\n {}",
paths.join("\n ")
);
}
return false;
}
let selected_member_paths = match select_workspace_member_paths(
dir_path,
dir_path,
members
.iter()
.map(|member| WorkspaceMemberRef::new(member.path.as_path(), member.name.as_deref())),
ingot,
) {
Ok(paths) => paths,
Err(err) => {
eprintln!("Error: {err}");
return true;
}
};
let selected_member_paths: HashSet<Utf8PathBuf> = selected_member_paths.into_iter().collect();
let mut seen = HashSet::new();
let mut has_errors = false;
for member in members {
let member_path = dir_path.join(member.path.as_str());
if !selected_member_paths.contains(&member_path) {
continue;
}
let member_url = member.url;
let member_has_errors =
check_ingot_and_dependencies(db, &member_url, dump_mir, report, &mut seen);
has_errors |= member_has_errors;
}
has_errors
}
#[allow(clippy::too_many_arguments)]
fn check_ingot_and_dependencies(
db: &mut DriverDataBase,
ingot_url: &Url,
dump_mir: bool,
report: Option<&ReportContext>,
seen: &mut HashSet<Url>,
) -> bool {
if !seen.insert(ingot_url.clone()) {
return false;
}
let Some(ingot) = db.workspace().containing_ingot(db, ingot_url.clone()) else {
eprintln!("Error: Could not resolve ingot {ingot_url}");
return true;
};
if !ingot_has_source_files(db, ingot) {
eprintln!("Error: Could not find source files for ingot {ingot_url}");
return true;
}
let diagnostics = CompilationDiagnostics::for_ingot_with_seen(db, ingot, seen);
let mut has_errors = false;
if !diagnostics.hir.is_empty() {
diagnostics.hir.emit(db);
if let Some(report) = report {
let formatted = diagnostics.hir.format_diags(db);
write_report_file(report, "errors/diagnostics.txt", &formatted);
}
has_errors = true;
}
if !diagnostics.mir.is_empty() {
db.emit_complete_diagnostics(&diagnostics.mir);
has_errors = true;
}
if !diagnostics.dependencies.is_empty() {
has_errors = true;
let formatted = diagnostics.dependencies.format(db);
eprint!("{formatted}");
if let Some(report) = report {
write_report_file(report, "errors/dependency_diagnostics.txt", &formatted);
}
}
if !has_errors {
let root_mod = ingot.root_mod(db);
if dump_mir {
dump_module_mir(db, root_mod);
}
if let Some(report) = report {
write_check_artifacts(db, root_mod, report);
}
}
has_errors
}
#[allow(clippy::too_many_arguments)]
fn check_single_file(
db: &mut DriverDataBase,
file_path: &Utf8PathBuf,
dump_mir: bool,
report: Option<&ReportContext>,
) -> bool {
// Create a file URL for the single .fe file
let canonical = match file_path.canonicalize_utf8() {
Ok(p) => p,
Err(e) => {
eprintln!("Error: Cannot canonicalize {file_path}: {e}");
return true;
}
};
let file_url = match Url::from_file_path(&canonical) {
Ok(url) => url,
Err(_) => {
eprintln!("Error: Invalid file path: {file_path}");
return true;
}
};
// Read the file content
let content = match std::fs::read_to_string(file_path) {
Ok(content) => content,
Err(err) => {
eprintln!("Error: Failed to read file {file_path}: {err}");
return true;
}
};
// Add the file to the workspace
db.workspace().touch(db, file_url.clone(), Some(content));
// Try to get the file and check it for errors
if let Some(file) = db.workspace().get(db, &file_url) {
let top_mod = db.top_mod(file);
let diagnostics = CompilationDiagnostics::for_top_mod(db, top_mod, &file_url);
let mut has_errors = false;
if !diagnostics.hir.is_empty() {
eprintln!("errors in {file_url}");
eprintln!();
diagnostics.hir.emit(db);
if let Some(report) = report {
let formatted = diagnostics.hir.format_diags(db);
write_report_file(report, "errors/diagnostics.txt", &formatted);
}
has_errors = true;
}
if !diagnostics.mir.is_empty() {
if !has_errors {
eprintln!("errors in {file_url}");
eprintln!();
}
db.emit_complete_diagnostics(&diagnostics.mir);
has_errors = true;
}
if !diagnostics.dependencies.is_empty() {
let formatted = diagnostics.dependencies.format(db);
eprint!("{formatted}");
if let Some(report) = report {
write_report_file(report, "errors/dependency_diagnostics.txt", &formatted);
}
has_errors = true;
}
if has_errors {
return true;
}
if dump_mir {
dump_module_mir(db, top_mod);
}
if let Some(report) = report {
write_check_artifacts(db, top_mod, report);
}
} else {
eprintln!("Error: Could not process file {file_path}");
return true;
}
false
}
fn dump_module_mir(db: &DriverDataBase, top_mod: TopLevelMod<'_>) {
match build_runtime_package(db, top_mod) {
Ok(package) => {
println!("=== Runtime Package for module ===");
print!("{package:#?}");
}
Err(err) => eprintln!("failed to build runtime package: {err}"),
}
}
fn write_check_manifest(
staging: &Utf8PathBuf,
path: &Utf8PathBuf,
dump_mir: bool,
has_errors: bool,
) {
let mut out = String::new();
out.push_str("fe check report\n");
out.push_str(&format!("path: {path}\n"));
out.push_str(&format!("dump_mir: {dump_mir}\n"));
out.push_str(&format!(
"status: {}\n",
if has_errors { "failed" } else { "ok" }
));
out.push_str(&format!("fe_version: {}\n", env!("CARGO_PKG_VERSION")));
let _ = std::fs::write(staging.join("manifest.txt"), out);
}
fn write_check_artifacts(db: &DriverDataBase, top_mod: TopLevelMod<'_>, report: &ReportContext) {
match build_runtime_package(db, top_mod) {
Ok(package) => {
write_report_file(
report,
"artifacts/runtime_package.txt",
&format!("{package:#?}"),
);
}
Err(err) => {
write_report_file(
report,
"artifacts/runtime_package_error.txt",
&format!("{err}"),
);
}
}
}