From 80dd35ccc4dcd431b61e5eaacf8e0caddfa40b34 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Wed, 16 Jul 2025 14:40:47 +0200 Subject: [PATCH 1/2] prevent trying to read non-existing file when extracting extra info for error messages --- compiler/ml/error_message_utils.ml | 10 +++++++--- .../expected/extract_from_none_file.res.expected | 6 ++++++ .../super_errors/fixtures/extract_from_none_file.res | 5 +++++ 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 tests/build_tests/super_errors/expected/extract_from_none_file.res.expected create mode 100644 tests/build_tests/super_errors/fixtures/extract_from_none_file.res diff --git a/compiler/ml/error_message_utils.ml b/compiler/ml/error_message_utils.ml index 1505c5699b..56b642e07c 100644 --- a/compiler/ml/error_message_utils.ml +++ b/compiler/ml/error_message_utils.ml @@ -41,9 +41,13 @@ end = struct String.sub src start_offset (end_offset - start_offset) let extract_text_at_loc loc = - (* TODO: Maybe cache later on *) - let src = Ext_io.load_file loc.Location.loc_start.pos_fname in - extract_location_string ~src loc + if loc.Location.loc_start.pos_fname = "_none_" then "" + else + try + (* TODO: Maybe cache later on *) + let src = Ext_io.load_file loc.Location.loc_start.pos_fname in + extract_location_string ~src loc + with _ -> "" let parse_expr_at_loc loc = let sub_src = extract_text_at_loc loc in diff --git a/tests/build_tests/super_errors/expected/extract_from_none_file.res.expected b/tests/build_tests/super_errors/expected/extract_from_none_file.res.expected new file mode 100644 index 0000000000..18f5c95880 --- /dev/null +++ b/tests/build_tests/super_errors/expected/extract_from_none_file.res.expected @@ -0,0 +1,6 @@ + + We've found a bug for you! + /.../fixtures/extract_from_none_file.res + + This has type: RegExp.t + But a while loop condition must always be of type: bool \ No newline at end of file diff --git a/tests/build_tests/super_errors/fixtures/extract_from_none_file.res b/tests/build_tests/super_errors/fixtures/extract_from_none_file.res new file mode 100644 index 0000000000..844d286f1f --- /dev/null +++ b/tests/build_tests/super_errors/fixtures/extract_from_none_file.res @@ -0,0 +1,5 @@ +// This will try and extract the text from a non-existing file. +// Test is intended to check that this does not crash. +while /foo/ { + () +} From dfeb4d5695d82f7c34aa7715d54485f53f22a58b Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Wed, 16 Jul 2025 14:42:17 +0200 Subject: [PATCH 2/2] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 37bbaf41b0..f649e15073 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,7 @@ - Rewatch: Add --dev flag to clean command. https://github.com/rescript-lang/rescript/pull/7622 - Rewatch: Use root package suffix in clean log messages. https://github.com/rescript-lang/rescript/pull/7648 - Fix inside comment printing for empty dict. https://github.com/rescript-lang/rescript/pull/7654 +- Fix I/O error message when trying to extract extra info from non-existing file. https://github.com/rescript-lang/rescript/pull/7656 # 12.0.0-beta.1