[PRISM] Raise LoadError when file cannot be read

This commit is contained in:
Kevin Newton 2024-04-25 13:32:08 -04:00
parent 6f4f360fc4
commit af24ba4034
4 changed files with 19 additions and 9 deletions

View File

@ -8785,7 +8785,7 @@ pm_parse_file_script_lines(const pm_scope_node_t *scope_node, const pm_parser_t
* be read. * be read.
*/ */
VALUE VALUE
pm_load_file(pm_parse_result_t *result, VALUE filepath) pm_load_file(pm_parse_result_t *result, VALUE filepath, bool load_error)
{ {
if (!pm_string_mapped_init(&result->input, RSTRING_PTR(filepath))) { if (!pm_string_mapped_init(&result->input, RSTRING_PTR(filepath))) {
#ifdef _WIN32 #ifdef _WIN32
@ -8794,9 +8794,21 @@ pm_load_file(pm_parse_result_t *result, VALUE filepath)
int e = errno; int e = errno;
#endif #endif
VALUE err = rb_syserr_new(e, RSTRING_PTR(filepath)); VALUE error;
if (load_error) {
VALUE message = rb_str_buf_new_cstr(strerror(e));
rb_str_cat2(message, " -- ");
rb_str_append(message, filepath);
error = rb_exc_new3(rb_eLoadError, message);
rb_ivar_set(error, rb_intern_const("@path"), filepath);
} else {
error = rb_syserr_new(e, RSTRING_PTR(filepath));
RB_GC_GUARD(filepath); RB_GC_GUARD(filepath);
return err; }
return error;
} }
pm_options_frozen_string_literal_init(&result->options); pm_options_frozen_string_literal_init(&result->options);
@ -8843,7 +8855,7 @@ pm_parse_file(pm_parse_result_t *result, VALUE filepath)
VALUE VALUE
pm_load_parse_file(pm_parse_result_t *result, VALUE filepath) pm_load_parse_file(pm_parse_result_t *result, VALUE filepath)
{ {
VALUE error = pm_load_file(result, filepath); VALUE error = pm_load_file(result, filepath, false);
if (NIL_P(error)) { if (NIL_P(error)) {
error = pm_parse_file(result, filepath); error = pm_parse_file(result, filepath);
} }

View File

@ -72,7 +72,7 @@ typedef struct {
bool parsed; bool parsed;
} pm_parse_result_t; } pm_parse_result_t;
VALUE pm_load_file(pm_parse_result_t *result, VALUE filepath); VALUE pm_load_file(pm_parse_result_t *result, VALUE filepath, bool load_error);
VALUE pm_parse_file(pm_parse_result_t *result, VALUE filepath); VALUE pm_parse_file(pm_parse_result_t *result, VALUE filepath);
VALUE pm_load_parse_file(pm_parse_result_t *result, VALUE filepath); VALUE pm_load_parse_file(pm_parse_result_t *result, VALUE filepath);
VALUE pm_parse_string(pm_parse_result_t *result, VALUE source, VALUE filepath); VALUE pm_parse_string(pm_parse_result_t *result, VALUE source, VALUE filepath);

2
ruby.c
View File

@ -2157,7 +2157,7 @@ prism_script(ruby_cmdline_options_t *opt, pm_parse_result_t *result)
} }
else { else {
pm_options_command_line_set(options, command_line); pm_options_command_line_set(options, command_line);
error = pm_load_file(result, opt->script_name); error = pm_load_file(result, opt->script_name, true);
// If reading the file did not error, at that point we load the command // If reading the file did not error, at that point we load the command
// line options. We do it in this order so that if the main script fails // line options. We do it in this order so that if the main script fails

View File

@ -19,8 +19,6 @@ MSpec.register(:exclude, "Regexps with encoding modifiers preserves UTF-8 as /u
MSpec.register(:exclude, "A Symbol literal raises an SyntaxError at parse time when Symbol with invalid bytes") MSpec.register(:exclude, "A Symbol literal raises an SyntaxError at parse time when Symbol with invalid bytes")
## Core ## Core
MSpec.register(:exclude, "IO.popen with a leading Array argument accepts a trailing Hash of Process.exec options")
MSpec.register(:exclude, "IO.popen with a leading Array argument accepts an IO mode argument following the Array")
MSpec.register(:exclude, "TracePoint#inspect returns a String showing the event, method, path and line for a :return event") MSpec.register(:exclude, "TracePoint#inspect returns a String showing the event, method, path and line for a :return event")
MSpec.register(:exclude, "TracePoint.new includes multiple events when multiple event names are passed as params") MSpec.register(:exclude, "TracePoint.new includes multiple events when multiple event names are passed as params")
MSpec.register(:exclude, "TracePoint#path equals \"(eval at __FILE__:__LINE__)\" inside an eval for :end event") MSpec.register(:exclude, "TracePoint#path equals \"(eval at __FILE__:__LINE__)\" inside an eval for :end event")