Stream: contributing

Topic: Roc should error for non-roc files #6418


view this post on Zulip Trevor Settles (Jan 27 2024 at 03:33):

This is my first contribution, I started looking at this issue today.
https://github.com/roc-lang/roc/issues/6418
I've done a bit of print debugging, and it seems like I've got the start of a fix going on. Here's my diff so far, am I on the right track?

diff --git a/crates/compiler/build/src/program.rs b/crates/compiler/build/src/program.rs
index 47bf2d4c3..dd53349fe 100644
--- a/crates/compiler/build/src/program.rs
+++ b/crates/compiler/build/src/program.rs
@@ -714,6 +714,24 @@ pub fn standard_load_config(
     }
 }

+pub fn is_valied_roc_file<'a>(path: PathBuf) -> Result<PathBuf, BuildFileError<'a>> {
+    let roc_ext = OsStr::new("roc");
+    match path.extension() {
+        Some(ext) => {
+            if roc_ext != ext {
+                Err(BuildFileError::LoadingProblem(
+                    LoadingProblem::FormattedReport("It failed, wrong file ext".to_string()),
+                ))
+            } else {
+                Ok(path)
+            }
+        }
+        None => Err(BuildFileError::LoadingProblem(
+            LoadingProblem::FormattedReport("It failed, no file ext".to_string()),
+        )),
+    }
+}
+
 #[allow(clippy::too_many_arguments)]
 pub fn build_file<'a>(
     arena: &'a Bump,
@@ -731,6 +749,8 @@ pub fn build_file<'a>(
 ) -> Result<BuiltFile<'a>, BuildFileError<'a>> {
     let compilation_start = Instant::now();

+    let app_module_path = is_valied_roc_file(app_module_path)?;
+
     // Step 1: compile the app and generate the .o file
     let loaded =
         roc_load::load_and_monomorphize(arena, app_module_path.clone(), roc_cache_dir, load_config)

It looks like I'll have to create the error message string in LoadingProblem::FormattedReport, but that will be the part I look into next

view this post on Zulip Luke Boswell (Jan 27 2024 at 03:43):

Looks like a good start to me. I think we also want to run if there is no extension.

view this post on Zulip Trevor Settles (Feb 06 2024 at 06:06):

I've got a draft PR up at https://github.com/roc-lang/roc/pull/6506. I think this is the implementation that works, I still need to write some tests for it. I'm not super happy with the part where I'm turning the slice of u8s into a string to check the first line. Is there a better place to do that?


Last updated: Jul 06 2025 at 12:14 UTC