Skip to content

Commit fdf00a7

Browse files
authored
Prevent trying to read non-existing file when extracting extra info for error messages (#7656)
* prevent trying to read non-existing file when extracting extra info for error messages * changelog
1 parent 2c96b79 commit fdf00a7

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
- Rewatch: Add --dev flag to clean command. https://github.com/rescript-lang/rescript/pull/7622
3838
- Rewatch: Use root package suffix in clean log messages. https://github.com/rescript-lang/rescript/pull/7648
3939
- Fix inside comment printing for empty dict. https://github.com/rescript-lang/rescript/pull/7654
40+
- Fix I/O error message when trying to extract extra info from non-existing file. https://github.com/rescript-lang/rescript/pull/7656
4041

4142
# 12.0.0-beta.1
4243

compiler/ml/error_message_utils.ml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,13 @@ end = struct
4141
String.sub src start_offset (end_offset - start_offset)
4242

4343
let extract_text_at_loc loc =
44-
(* TODO: Maybe cache later on *)
45-
let src = Ext_io.load_file loc.Location.loc_start.pos_fname in
46-
extract_location_string ~src loc
44+
if loc.Location.loc_start.pos_fname = "_none_" then ""
45+
else
46+
try
47+
(* TODO: Maybe cache later on *)
48+
let src = Ext_io.load_file loc.Location.loc_start.pos_fname in
49+
extract_location_string ~src loc
50+
with _ -> ""
4751

4852
let parse_expr_at_loc loc =
4953
let sub_src = extract_text_at_loc loc in
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/extract_from_none_file.res
4+
5+
This has type: RegExp.t
6+
But a while loop condition must always be of type: bool
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// This will try and extract the text from a non-existing file.
2+
// Test is intended to check that this does not crash.
3+
while /foo/ {
4+
()
5+
}

0 commit comments

Comments
 (0)