about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-11-11 16:31:44 +0000
committerPjotr <wrk@bosa.lan>2026-11-11 16:31:44 +0000
commit0751e56b5f56bca61b3599c0a96cfc5f0fd30f32 (patch)
tree4574f734c4d6f3a9b7af532d8495ae02e6228bd7 /src
parentfd7234a68c0a03925cdf5d678d7b76b97e815617 (diff)
downloadgenenetwork4-0751e56b5f56bca61b3599c0a96cfc5f0fd30f32.tar.gz
bulklmm: report Julia's error line, not its last line
A failing scan came back as

  BulkLMM failed: in expression starting at scripts/bulklmm_scan.jl:26

which names neither the package nor the reason. Julia had said it plainly,
two lines up:

  ERROR: LoadError: ArgumentError: Package CSV not found in current path.
  - Run `import Pkg; Pkg.add("CSV")` to install the CSV package.

The complaint finder looked for a line starting "Error" -- Julia writes
"ERROR:" -- and fell back to the last non-empty line, which is the line
number. That is the same defect as gemma-wrapper's Ruby backtrace, in our
own error handling: the wrapped tool said why, and we threw it away.

julia_complaint takes the first ERROR:/LoadError line plus Julia's hint
line when there is one, with the last line kept only as a fallback for a
driver that fails silently. Three unit tests cover the three shapes.

The integration tests also stop failing on a half-instantiated Julia
project: bulklmm_available used to accept any directory with a
Project.toml, and /tmp/gj-env has one naming BulkLMM and nothing else, so
the scan died on `using CSV` instead of skipping. It now requires the
project to name the three packages the driver loads, which is what a
usable env is.
Diffstat (limited to 'src')
-rw-r--r--src/bulklmm.rs89
1 files changed, 70 insertions, 19 deletions
diff --git a/src/bulklmm.rs b/src/bulklmm.rs
index 9811578..859d6f7 100644
--- a/src/bulklmm.rs
+++ b/src/bulklmm.rs
@@ -44,6 +44,40 @@ impl std::fmt::Display for BulklmmError {
     }
 }
 
+/// Why Julia stopped, in its own words.
+///
+/// Julia writes the sentence that matters on a line starting `ERROR:`, with
+/// `Stacktrace:` and frame lines under it, and — for a missing package — a
+/// `- Run \`import Pkg; ...\`` hint after that. Taking the *last* line
+/// instead reports `in expression starting at scripts/bulklmm_scan.jl:26`,
+/// which names neither the package nor the reason: the same defect as
+/// gemma-wrapper's Ruby backtrace, in our own error handling. So: the first
+/// `ERROR:` line, plus its hint line when there is one, and the last line
+/// only as a fallback for a driver that failed without one.
+fn julia_complaint(stderr: &str, stdout: &str) -> String {
+    let lines: Vec<&str> = stderr
+        .lines()
+        .chain(stdout.lines())
+        .map(str::trim)
+        .filter(|line| !line.is_empty())
+        .collect();
+    if let Some(index) = lines.iter().position(|line| {
+        let lowered = line.to_ascii_lowercase();
+        lowered.starts_with("error") || lowered.starts_with("loaderror")
+    }) {
+        let mut complaint = lines[index].to_string();
+        if let Some(hint) = lines.get(index + 1).filter(|line| line.starts_with("- ")) {
+            complaint.push(' ');
+            complaint.push_str(hint.trim_start_matches("- ").trim());
+        }
+        return complaint;
+    }
+    lines
+        .last()
+        .map(|line| (*line).to_string())
+        .unwrap_or_else(|| "BulkLMM failed without saying why".to_string())
+}
+
 fn io_err(path: &Path, error: std::io::Error) -> BulklmmError {
     BulklmmError::Io(format!("{}: {error}", path.display()))
 }
@@ -263,25 +297,10 @@ pub async fn compute(
     .await
     .map_err(|error| BulklmmError::Io(format!("{julia}: {error}")))?;
     if !output.status.success() {
-        let stderr = String::from_utf8_lossy(&output.stderr);
-        let stdout = String::from_utf8_lossy(&output.stdout);
-        let complaint = stderr
-            .lines()
-            .chain(stdout.lines())
-            .map(str::trim)
-            .filter(|line| !line.is_empty() && !line.starts_with("Stacktrace"))
-            .find(|line| line.starts_with("Error"))
-            .or_else(|| {
-                stderr
-                    .lines()
-                    .chain(stdout.lines())
-                    .map(str::trim)
-                    .rev()
-                    .find(|line| !line.is_empty())
-            })
-            .unwrap_or("BulkLMM failed without saying why")
-            .to_string();
-        return Err(BulklmmError::CommandFailed(complaint));
+        return Err(BulklmmError::CommandFailed(julia_complaint(
+            &String::from_utf8_lossy(&output.stderr),
+            &String::from_utf8_lossy(&output.stdout),
+        )));
     }
     let contents = std::fs::read_to_string(&result_path).map_err(|e| io_err(&result_path, e))?;
     let perm_contents = if nperm > 0 {
@@ -380,4 +399,36 @@ mod tests {
         assert_eq!(map["0.05"], 3.959);
         assert_eq!(h2, Some(0.0));
     }
+
+    /// The failure that prompted this: a Julia project without CSV. The last
+    /// line names a line number and nothing else; the `ERROR:` line names the
+    /// package, and the hint says what to run.
+    #[test]
+    fn the_complaint_is_julias_error_line_and_its_hint() {
+        let stderr = "ERROR: LoadError: ArgumentError: Package CSV not found in current path.\n\
+                      - Run `import Pkg; Pkg.add(\"CSV\")` to install the CSV package.\n\
+                      Stacktrace:\n\
+                       [1] macro expansion\n\
+                       @ ./loading.jl:1166 [inlined]\n\
+                      in expression starting at scripts/bulklmm_scan.jl:26\n";
+        let complaint = julia_complaint(stderr, "");
+        assert!(complaint.starts_with("ERROR: LoadError: ArgumentError: Package CSV not found"), "{complaint}");
+        assert!(complaint.contains("Pkg.add"), "{complaint}");
+    }
+
+    #[test]
+    fn a_driver_that_fails_silently_says_so() {
+        assert_eq!(
+            julia_complaint("", ""),
+            "BulkLMM failed without saying why"
+        );
+    }
+
+    #[test]
+    fn without_an_error_line_the_last_line_is_better_than_nothing() {
+        assert_eq!(
+            julia_complaint("something went sideways\n", ""),
+            "something went sideways"
+        );
+    }
 }