Replace UNKNOWN.roc in reports with real filename

This commit is contained in:
Richard Feldman 2023-12-11 21:22:15 -05:00
parent 95a4bb988c
commit 4b743c8878
No known key found for this signature in database
GPG key ID: F1F21AA5B1D9E43B
3 changed files with 68 additions and 47 deletions

View file

@ -684,6 +684,7 @@ impl MakeSpecializationsPass {
struct State<'a> { struct State<'a> {
pub root_id: ModuleId, pub root_id: ModuleId,
pub root_subs: Option<Subs>, pub root_subs: Option<Subs>,
pub root_path: PathBuf,
pub cache_dir: PathBuf, pub cache_dir: PathBuf,
/// If the root is an app module, the shorthand specified in its header's `to` field /// If the root is an app module, the shorthand specified in its header's `to` field
pub opt_platform_shorthand: Option<&'a str>, pub opt_platform_shorthand: Option<&'a str>,
@ -752,6 +753,7 @@ impl<'a> State<'a> {
fn new( fn new(
root_id: ModuleId, root_id: ModuleId,
root_path: PathBuf,
opt_platform_shorthand: Option<&'a str>, opt_platform_shorthand: Option<&'a str>,
target_info: TargetInfo, target_info: TargetInfo,
function_kind: FunctionKind, function_kind: FunctionKind,
@ -770,6 +772,7 @@ impl<'a> State<'a> {
Self { Self {
root_id, root_id,
root_path,
root_subs: None, root_subs: None,
opt_platform_shorthand, opt_platform_shorthand,
cache_dir, cache_dir,
@ -1077,8 +1080,9 @@ pub struct LoadStart<'a> {
arc_modules: Arc<Mutex<PackageModuleIds<'a>>>, arc_modules: Arc<Mutex<PackageModuleIds<'a>>>,
ident_ids_by_module: SharedIdentIdsByModule, ident_ids_by_module: SharedIdentIdsByModule,
root_id: ModuleId, root_id: ModuleId,
opt_platform_shorthand: Option<&'a str>, root_path: PathBuf,
root_msg: Msg<'a>, root_msg: Msg<'a>,
opt_platform_shorthand: Option<&'a str>,
src_dir: PathBuf, src_dir: PathBuf,
} }
@ -1101,7 +1105,7 @@ impl<'a> LoadStart<'a> {
let res_loaded = load_filename( let res_loaded = load_filename(
arena, arena,
filename, filename.clone(),
true, true,
None, None,
None, None,
@ -1136,6 +1140,7 @@ impl<'a> LoadStart<'a> {
ident_ids_by_module, ident_ids_by_module,
src_dir, src_dir,
root_id: header_output.module_id, root_id: header_output.module_id,
root_path: filename,
root_msg: header_output.msg, root_msg: header_output.msg,
opt_platform_shorthand: header_output.opt_platform_shorthand, opt_platform_shorthand: header_output.opt_platform_shorthand,
}) })
@ -1162,7 +1167,7 @@ impl<'a> LoadStart<'a> {
let header_output = load_from_str( let header_output = load_from_str(
arena, arena,
filename, filename.clone(),
src, src,
Arc::clone(&arc_modules), Arc::clone(&arc_modules),
Arc::clone(&ident_ids_by_module), Arc::clone(&ident_ids_by_module),
@ -1178,6 +1183,7 @@ impl<'a> LoadStart<'a> {
src_dir, src_dir,
ident_ids_by_module, ident_ids_by_module,
root_id, root_id,
root_path: filename,
root_msg, root_msg,
opt_platform_shorthand: opt_platform_id, opt_platform_shorthand: opt_platform_id,
}) })
@ -1352,6 +1358,7 @@ pub fn load_single_threaded<'a>(
arc_modules, arc_modules,
ident_ids_by_module, ident_ids_by_module,
root_id, root_id,
root_path,
root_msg, root_msg,
src_dir, src_dir,
opt_platform_shorthand, opt_platform_shorthand,
@ -1367,6 +1374,7 @@ pub fn load_single_threaded<'a>(
let number_of_workers = 1; let number_of_workers = 1;
let mut state = State::new( let mut state = State::new(
root_id, root_id,
root_path,
opt_platform_shorthand, opt_platform_shorthand,
target_info, target_info,
function_kind, function_kind,
@ -1503,7 +1511,7 @@ fn state_thread_step<'a>(
Ok(ControlFlow::Break(LoadResult::Monomorphized(monomorphized))) Ok(ControlFlow::Break(LoadResult::Monomorphized(monomorphized)))
} }
Msg::FailedToReadFile { filename, error } => { Msg::FailedToReadFile { filename, error } => {
let buf = to_file_problem_report_string(&filename, error); let buf = to_file_problem_report_string(filename, error);
Err(LoadingProblem::FormattedReport(buf)) Err(LoadingProblem::FormattedReport(buf))
} }
@ -1654,7 +1662,7 @@ pub fn report_loading_problem(
} }
LoadingProblem::FormattedReport(report) => report, LoadingProblem::FormattedReport(report) => report,
LoadingProblem::FileProblem { filename, error } => { LoadingProblem::FileProblem { filename, error } => {
to_file_problem_report_string(&filename, error) to_file_problem_report_string(filename, error)
} }
err => todo!("Loading error: {:?}", err), err => todo!("Loading error: {:?}", err),
} }
@ -1677,6 +1685,7 @@ fn load_multi_threaded<'a>(
arc_modules, arc_modules,
ident_ids_by_module, ident_ids_by_module,
root_id, root_id,
root_path,
root_msg, root_msg,
src_dir, src_dir,
opt_platform_shorthand, opt_platform_shorthand,
@ -1707,6 +1716,7 @@ fn load_multi_threaded<'a>(
let mut state = State::new( let mut state = State::new(
root_id, root_id,
root_path,
opt_platform_shorthand, opt_platform_shorthand,
target_info, target_info,
function_kind, function_kind,
@ -2238,6 +2248,7 @@ fn update<'a>(
let buf = to_https_problem_report_string( let buf = to_https_problem_report_string(
url, url,
Problem::InvalidUrl(url_err), Problem::InvalidUrl(url_err),
header.module_path,
); );
return Err(LoadingProblem::FormattedReport(buf)); return Err(LoadingProblem::FormattedReport(buf));
} }
@ -3159,7 +3170,7 @@ fn finish_specialization<'a>(
} }
Valid(To::NewPackage(p_or_p)) => PathBuf::from(p_or_p.as_str()), Valid(To::NewPackage(p_or_p)) => PathBuf::from(p_or_p.as_str()),
other => { other => {
let buf = to_missing_platform_report(state.root_id, other); let buf = report_cannot_run(state.root_id, state.root_path, other);
return Err(LoadingProblem::FormattedReport(buf)); return Err(LoadingProblem::FormattedReport(buf));
} }
}; };
@ -3513,9 +3524,7 @@ fn load_builtin_module_help<'a>(
) -> (HeaderInfo<'a>, roc_parse::state::State<'a>) { ) -> (HeaderInfo<'a>, roc_parse::state::State<'a>) {
let is_root_module = false; let is_root_module = false;
let opt_shorthand = None; let opt_shorthand = None;
let filename = PathBuf::from(filename); let filename = PathBuf::from(filename);
let parse_state = roc_parse::state::State::new(src_bytes.as_bytes()); let parse_state = roc_parse::state::State::new(src_bytes.as_bytes());
let parsed = roc_parse::module::parse_header(arena, parse_state.clone()); let parsed = roc_parse::module::parse_header(arena, parse_state.clone());
@ -3968,6 +3977,7 @@ fn parse_header<'a>(
module_timing, module_timing,
)?; )?;
let filename = resolved_header.module_path.clone();
let mut messages = Vec::with_capacity(packages.len() + 1); let mut messages = Vec::with_capacity(packages.len() + 1);
// It's important that the app header is first in the list! // It's important that the app header is first in the list!
@ -3982,6 +3992,7 @@ fn parse_header<'a>(
module_id, module_id,
module_ids, module_ids,
ident_ids_by_module, ident_ids_by_module,
filename,
); );
// Look at the app module's `to` keyword to determine which package was the platform. // Look at the app module's `to` keyword to determine which package was the platform.
@ -4084,6 +4095,7 @@ fn load_packages<'a>(
module_id: ModuleId, module_id: ModuleId,
module_ids: Arc<Mutex<PackageModuleIds<'a>>>, module_ids: Arc<Mutex<PackageModuleIds<'a>>>,
ident_ids_by_module: SharedIdentIdsByModule, ident_ids_by_module: SharedIdentIdsByModule,
filename: PathBuf,
) { ) {
// Load all the packages // Load all the packages
for Loc { value: entry, .. } in packages.iter() { for Loc { value: entry, .. } in packages.iter() {
@ -4121,7 +4133,7 @@ fn load_packages<'a>(
} }
} }
Err(problem) => { Err(problem) => {
let buf = to_https_problem_report_string(src, problem); let buf = to_https_problem_report_string(src, problem, filename);
load_messages.push(Msg::FailedToLoad(LoadingProblem::FormattedReport(buf))); load_messages.push(Msg::FailedToLoad(LoadingProblem::FormattedReport(buf)));
return; return;
@ -6581,7 +6593,11 @@ fn to_parse_problem_report<'a>(
buf buf
} }
fn to_missing_platform_report(module_id: ModuleId, other: &PlatformPath) -> String { fn report_cannot_run(
module_id: ModuleId,
filename: PathBuf,
platform_path: &PlatformPath,
) -> String {
use roc_reporting::report::{Report, RocDocAllocator, DEFAULT_PALETTE}; use roc_reporting::report::{Report, RocDocAllocator, DEFAULT_PALETTE};
use ven_pretty::DocAllocator; use ven_pretty::DocAllocator;
use PlatformPath::*; use PlatformPath::*;
@ -6591,20 +6607,20 @@ fn to_missing_platform_report(module_id: ModuleId, other: &PlatformPath) -> Stri
let alloc = RocDocAllocator::new(&[], module_id, &interns); let alloc = RocDocAllocator::new(&[], module_id, &interns);
let report = { let report = {
match other { match platform_path {
Valid(_) => unreachable!(), Valid(_) => unreachable!(),
NotSpecified => { NotSpecified => {
let doc = alloc.stack([ let doc = alloc.stack([
alloc.reflow("I could not find a platform based on your input file."), alloc.reflow("I could not find a platform based on your input file."),
alloc.reflow(r"Does the module header contain an entry that looks like this:"), alloc.reflow(r"Does the module header have an entry that looks like this?"),
alloc alloc
.parser_suggestion(" packages { pf: \"platform\" }") .parser_suggestion("packages { blah: \"…path or URL to platform…\" }")
.indent(4), .indent(4),
alloc.reflow("See also TODO."), alloc.reflow("Tip: The following part of the tutorial has an example of specifying a platform:\n\n<https://www.roc-lang.org/tutorial#building-an-application>"),
]); ]);
Report { Report {
filename: "UNKNOWN.roc".into(), filename,
doc, doc,
title: "NO PLATFORM".to_string(), title: "NO PLATFORM".to_string(),
severity: Severity::RuntimeError, severity: Severity::RuntimeError,
@ -6619,7 +6635,7 @@ fn to_missing_platform_report(module_id: ModuleId, other: &PlatformPath) -> Stri
]); ]);
Report { Report {
filename: "UNKNOWN.roc".into(), filename,
doc, doc,
title: "NO PLATFORM".to_string(), title: "NO PLATFORM".to_string(),
severity: Severity::RuntimeError, severity: Severity::RuntimeError,
@ -6634,7 +6650,7 @@ fn to_missing_platform_report(module_id: ModuleId, other: &PlatformPath) -> Stri
]); ]);
Report { Report {
filename: "UNKNOWN.roc".into(), filename,
doc, doc,
title: "NO PLATFORM".to_string(), title: "NO PLATFORM".to_string(),
severity: Severity::RuntimeError, severity: Severity::RuntimeError,
@ -6649,7 +6665,7 @@ fn to_missing_platform_report(module_id: ModuleId, other: &PlatformPath) -> Stri
]); ]);
Report { Report {
filename: "UNKNOWN.roc".into(), filename,
doc, doc,
title: "NO PLATFORM".to_string(), title: "NO PLATFORM".to_string(),
severity: Severity::RuntimeError, severity: Severity::RuntimeError,

View file

@ -1090,7 +1090,7 @@ pub fn can_problem<'b>(
title = "OVERAPPLIED CRASH".to_string(); title = "OVERAPPLIED CRASH".to_string();
} }
Problem::FileProblem { filename, error } => { Problem::FileProblem { filename, error } => {
let report = to_file_problem_report(alloc, &filename, error); let report = to_file_problem_report(alloc, filename, error);
doc = report.doc; doc = report.doc;
title = report.title; title = report.title;
} }

View file

@ -1101,7 +1101,11 @@ where
} }
#[cfg(not(target_family = "wasm"))] #[cfg(not(target_family = "wasm"))]
pub fn to_https_problem_report_string(url: &str, https_problem: Problem) -> String { pub fn to_https_problem_report_string(
url: &str,
https_problem: Problem,
filename: PathBuf,
) -> String {
let src_lines: Vec<&str> = Vec::new(); let src_lines: Vec<&str> = Vec::new();
let mut module_ids = ModuleIds::default(); let mut module_ids = ModuleIds::default();
@ -1115,7 +1119,7 @@ pub fn to_https_problem_report_string(url: &str, https_problem: Problem) -> Stri
let mut buf = String::new(); let mut buf = String::new();
let palette = DEFAULT_PALETTE; let palette = DEFAULT_PALETTE;
let report = to_https_problem_report(&alloc, url, https_problem); let report = to_https_problem_report(&alloc, url, https_problem, filename);
report.render_color_terminal(&mut buf, &alloc, &palette); report.render_color_terminal(&mut buf, &alloc, &palette);
buf buf
@ -1126,6 +1130,7 @@ pub fn to_https_problem_report<'b>(
alloc: &'b RocDocAllocator<'b>, alloc: &'b RocDocAllocator<'b>,
url: &'b str, url: &'b str,
https_problem: Problem, https_problem: Problem,
filename: PathBuf,
) -> Report<'b> { ) -> Report<'b> {
match https_problem { match https_problem {
Problem::UnsupportedEncoding(not_supported_encoding) => { Problem::UnsupportedEncoding(not_supported_encoding) => {
@ -1154,7 +1159,7 @@ pub fn to_https_problem_report<'b>(
]); ]);
Report { Report {
filename: "UNKNOWN.roc".into(), filename,
doc, doc,
title: "UNSUPPORTED ENCODING".to_string(), title: "UNSUPPORTED ENCODING".to_string(),
severity: Severity::Fatal, severity: Severity::Fatal,
@ -1189,7 +1194,7 @@ pub fn to_https_problem_report<'b>(
]); ]);
Report { Report {
filename: "UNKNOWN.roc".into(), filename,
doc, doc,
title: "MULTIPLE ENCODINGS".to_string(), title: "MULTIPLE ENCODINGS".to_string(),
severity: Severity::Fatal, severity: Severity::Fatal,
@ -1224,7 +1229,7 @@ pub fn to_https_problem_report<'b>(
]); ]);
Report { Report {
filename: "UNKNOWN.roc".into(), filename,
doc, doc,
title: "INVALID CONTENT HASH".to_string(), title: "INVALID CONTENT HASH".to_string(),
severity: Severity::Fatal, severity: Severity::Fatal,
@ -1241,7 +1246,7 @@ pub fn to_https_problem_report<'b>(
alloc.concat([alloc.tip(), alloc.reflow(r"Is the URL correct?")]), alloc.concat([alloc.tip(), alloc.reflow(r"Is the URL correct?")]),
]); ]);
Report { Report {
filename: "UNKNOWN.roc".into(), filename,
doc, doc,
title: "NOTFOUND".to_string(), title: "NOTFOUND".to_string(),
severity: Severity::Fatal, severity: Severity::Fatal,
@ -1268,7 +1273,7 @@ pub fn to_https_problem_report<'b>(
]); ]);
Report { Report {
filename: "UNKNOWN.roc".into(), filename,
doc, doc,
title: "IO ERROR".to_string(), title: "IO ERROR".to_string(),
severity: Severity::Fatal, severity: Severity::Fatal,
@ -1295,7 +1300,7 @@ pub fn to_https_problem_report<'b>(
]); ]);
Report { Report {
filename: "UNKNOWN.roc".into(), filename,
doc, doc,
title: "IO ERROR".to_string(), title: "IO ERROR".to_string(),
severity: Severity::Fatal, severity: Severity::Fatal,
@ -1324,7 +1329,7 @@ pub fn to_https_problem_report<'b>(
]); ]);
Report { Report {
filename: "UNKNOWN.roc".into(), filename,
doc, doc,
title: "HTTP ERROR".to_string(), title: "HTTP ERROR".to_string(),
severity: Severity::Fatal, severity: Severity::Fatal,
@ -1365,7 +1370,7 @@ pub fn to_https_problem_report<'b>(
]); ]);
Report { Report {
filename: "UNKNOWN.roc".into(), filename,
doc, doc,
title: "INVALID EXTENSION SUFFIX".to_string(), title: "INVALID EXTENSION SUFFIX".to_string(),
severity: Severity::Fatal, severity: Severity::Fatal,
@ -1398,7 +1403,7 @@ pub fn to_https_problem_report<'b>(
]); ]);
Report { Report {
filename: "UNKNOWN.roc".into(), filename,
doc, doc,
title: "INVALID EXTENSION".to_string(), title: "INVALID EXTENSION".to_string(),
severity: Severity::Fatal, severity: Severity::Fatal,
@ -1436,7 +1441,7 @@ pub fn to_https_problem_report<'b>(
]); ]);
Report { Report {
filename: "UNKNOWN.roc".into(), filename,
doc, doc,
title: "INVALID FRAGMENT".to_string(), title: "INVALID FRAGMENT".to_string(),
severity: Severity::Fatal, severity: Severity::Fatal,
@ -1474,7 +1479,7 @@ pub fn to_https_problem_report<'b>(
]); ]);
Report { Report {
filename: "UNKNOWN.roc".into(), filename,
doc, doc,
title: "MISSING PACKAGE HASH".to_string(), title: "MISSING PACKAGE HASH".to_string(),
severity: Severity::Fatal, severity: Severity::Fatal,
@ -1500,7 +1505,7 @@ pub fn to_https_problem_report<'b>(
]); ]);
Report { Report {
filename: "UNKNOWN.roc".into(), filename,
doc, doc,
title: "HTTPS MANDATORY".to_string(), title: "HTTPS MANDATORY".to_string(),
severity: Severity::Fatal, severity: Severity::Fatal,
@ -1543,7 +1548,7 @@ pub fn to_https_problem_report<'b>(
]); ]);
Report { Report {
filename: "UNKNOWN.roc".into(), filename,
doc, doc,
title: "MISLEADING CHARACTERS".to_string(), title: "MISLEADING CHARACTERS".to_string(),
severity: Severity::Fatal, severity: Severity::Fatal,
@ -1573,7 +1578,7 @@ pub fn to_https_problem_report<'b>(
]); ]);
Report { Report {
filename: "UNKNOWN.roc".into(), filename,
doc, doc,
title: "FILE TOO LARGE".to_string(), title: "FILE TOO LARGE".to_string(),
severity: Severity::Fatal, severity: Severity::Fatal,
@ -1582,13 +1587,10 @@ pub fn to_https_problem_report<'b>(
} }
} }
pub fn to_file_problem_report_string(filename: &Path, error: io::ErrorKind) -> String { pub fn to_file_problem_report_string(filename: PathBuf, error: io::ErrorKind) -> String {
let src_lines: Vec<&str> = Vec::new(); let src_lines: Vec<&str> = Vec::new();
let mut module_ids = ModuleIds::default(); let mut module_ids = ModuleIds::default();
let module_id = module_ids.get_or_insert(&"find module name somehow?".into()); let module_id = module_ids.get_or_insert(&"find module name somehow?".into());
let interns = Interns::default(); let interns = Interns::default();
// Report parsing and canonicalization problems // Report parsing and canonicalization problems
@ -1604,16 +1606,16 @@ pub fn to_file_problem_report_string(filename: &Path, error: io::ErrorKind) -> S
pub fn to_file_problem_report<'b>( pub fn to_file_problem_report<'b>(
alloc: &'b RocDocAllocator<'b>, alloc: &'b RocDocAllocator<'b>,
filename: &Path, filename: PathBuf,
error: io::ErrorKind, error: io::ErrorKind,
) -> Report<'b> { ) -> Report<'b> {
let filename: String = filename.to_str().unwrap().to_string(); let filename_str: String = filename.to_str().unwrap().to_string();
match error { match error {
io::ErrorKind::NotFound => { io::ErrorKind::NotFound => {
let doc = alloc.stack([ let doc = alloc.stack([
alloc.reflow(r"I am looking for this file, but it's not there:"), alloc.reflow(r"I am looking for this file, but it's not there:"),
alloc alloc
.string(filename) .string(filename_str)
.annotate(Annotation::ParserSuggestion) .annotate(Annotation::ParserSuggestion)
.indent(4), .indent(4),
alloc.concat([ alloc.concat([
@ -1623,7 +1625,7 @@ pub fn to_file_problem_report<'b>(
]); ]);
Report { Report {
filename: "UNKNOWN.roc".into(), filename,
doc, doc,
title: "FILE NOT FOUND".to_string(), title: "FILE NOT FOUND".to_string(),
severity: Severity::Fatal, severity: Severity::Fatal,
@ -1633,7 +1635,7 @@ pub fn to_file_problem_report<'b>(
let doc = alloc.stack([ let doc = alloc.stack([
alloc.reflow(r"I don't have the required permissions to read this file:"), alloc.reflow(r"I don't have the required permissions to read this file:"),
alloc alloc
.string(filename) .string(filename_str)
.annotate(Annotation::ParserSuggestion) .annotate(Annotation::ParserSuggestion)
.indent(4), .indent(4),
alloc alloc
@ -1641,7 +1643,7 @@ pub fn to_file_problem_report<'b>(
]); ]);
Report { Report {
filename: "UNKNOWN.roc".into(), filename,
doc, doc,
title: "FILE PERMISSION DENIED".to_string(), title: "FILE PERMISSION DENIED".to_string(),
severity: Severity::Fatal, severity: Severity::Fatal,
@ -1652,13 +1654,16 @@ pub fn to_file_problem_report<'b>(
let formatted = format!("{error}"); let formatted = format!("{error}");
let doc = alloc.stack([ let doc = alloc.stack([
alloc.reflow(r"I tried to read this file:"), alloc.reflow(r"I tried to read this file:"),
alloc.string(filename).annotate(Annotation::Error).indent(4), alloc
.string(filename_str)
.annotate(Annotation::Error)
.indent(4),
alloc.reflow(r"But ran into:"), alloc.reflow(r"But ran into:"),
alloc.text(formatted).annotate(Annotation::Error).indent(4), alloc.text(formatted).annotate(Annotation::Error).indent(4),
]); ]);
Report { Report {
filename: "UNKNOWN.roc".into(), filename,
doc, doc,
title: "FILE PROBLEM".to_string(), title: "FILE PROBLEM".to_string(),
severity: Severity::Fatal, severity: Severity::Fatal,