Repository: casey/just
Pull request #1358 (2022-10-05 00:37:16)

Don't evaluate comments
This is a follow-up to #1333. In addition to not printing out comments, it doesn't evaluate the contents of interpolations inside comments. @neunenak, what do you think?

neunenak: Huh, I didn't realize interpolations inside comments would get evaluated. Yeah, this seems like a good change.
casey: Unfortunately they'll still get parsed, so you'll get parse errors. However, fixing that is a backwards-compatible change. However it'll be a little hairy. We'll have to either extract setting values in a first pass (not actually that hard since settings are simple) and use the settings to change how the parser works, or stipulate that the setting only affects parsing of recipes after the setting.

Patch:
diff --git a/src/line.rs b/src/line.rs
index f0bc6dc9..6b0de6c7 100644
--- a/src/line.rs
+++ b/src/line.rs
@@ -13,41 +13,39 @@ impl<'src> Line<'src> {
   }
 
   pub(crate) fn is_comment(&self) -> bool {
-    match self.fragments.first() {
-      Some(Fragment::Text { token }) => token.lexeme().starts_with('#'),
-      _ => false,
-    }
+    matches!(
+      self.fragments.first(),
+      Some(Fragment::Text { token }) if token.lexeme().starts_with('#'),
+    )
   }
 
   pub(crate) fn is_continuation(&self) -> bool {
-    match self.fragments.last() {
-      Some(Fragment::Text { token }) => token.lexeme().ends_with('\\'),
-      _ => false,
-    }
+    matches!(
+      self.fragments.last(),
+      Some(Fragment::Text { token }) if token.lexeme().ends_with('\\'),
+    )
   }
 
   pub(crate) fn is_shebang(&self) -> bool {
-    match self.fragments.first() {
-      Some(Fragment::Text { token }) => token.lexeme().starts_with("#!"),
-      _ => false,
-    }
+    matches!(
+      self.fragments.first(),
+      Some(Fragment::Text { token }) if token.lexeme().starts_with("#!"),
+    )
   }
 
   pub(crate) fn is_quiet(&self) -> bool {
-    match self.fragments.first() {
-      Some(Fragment::Text { token }) => {
-        token.lexeme().starts_with('@') || token.lexeme().starts_with("-@")
-      }
-      _ => false,
-    }
+    matches!(
+      self.fragments.first(),
+      Some(Fragment::Text { token })
+        if token.lexeme().starts_with('@') || token.lexeme().starts_with("-@"),
+    )
   }
 
   pub(crate) fn is_infallible(&self) -> bool {
-    match self.fragments.first() {
-      Some(Fragment::Text { token }) => {
-        token.lexeme().starts_with('-') || token.lexeme().starts_with("@-")
-      }
-      _ => false,
-    }
+    matches!(
+      self.fragments.first(),
+      Some(Fragment::Text { token })
+        if token.lexeme().starts_with('-') || token.lexeme().starts_with("@-"),
+    )
   }
 }
diff --git a/src/recipe.rs b/src/recipe.rs
index 80156f22..698a49db 100644
--- a/src/recipe.rs
+++ b/src/recipe.rs
@@ -124,7 +124,9 @@ impl<'src, D> Recipe<'src, D> {
         }
         let line = lines.next().unwrap();
         line_number += 1;
-        evaluated += &evaluator.evaluate_line(line, continued)?;
+        if !comment_line {
+          evaluated += &evaluator.evaluate_line(line, continued)?;
+        }
         if line.is_continuation() && !comment_line {
           continued = true;
           evaluated.pop();
@@ -132,6 +134,11 @@ impl<'src, D> Recipe<'src, D> {
           break;
         }
       }
+
+      if comment_line {
+        continue;
+      }
+
       let mut command = evaluated.as_str();
 
       if quiet_command {
@@ -142,10 +149,6 @@ impl<'src, D> Recipe<'src, D> {
         command = &command[1..];
       }
 
-      if comment_line {
-        continue;
-      }
-
       if command.is_empty() {
         continue;
       }


Test patch:
diff --git a/tests/ignore_comments.rs b/tests/ignore_comments.rs
index 1dc04b19..1badd4c9 100644
--- a/tests/ignore_comments.rs
+++ b/tests/ignore_comments.rs
@@ -83,3 +83,17 @@ fn continuations_with_echo_comments_true() {
     .stderr("# comment lines can be continued echo something-useful\n")
     .run();
 }
+
+#[test]
+fn dont_evalute_comments() {
+  Test::new()
+    .justfile(
+      "
+      set ignore-comments
+
+      some_recipe:
+        # {{ error('foo') }}
+    ",
+    )
+    .run();
+}