2024 年 Clojure 状态调查!中分享您的想法。

欢迎来到这里!有关如何使用更多信息,请参阅关于页面。

0
工具
编辑

背景

  1. 我在容器中运行构建,我没有对临时文件的访问权限(无论构建是否成功,都会立即被丢弃)。因此,我将 --report stderr 选项添加到每个 clojure 命令的调用中。
  2. 我使用 Depstar 构建uberjar,它需要使用 -X:... 选项。
  3. 我还使用 -Srepro 选项。

问题

当 Clojure 命令行包含 -X 选项时,似乎没有方法同时包含 --report stderr 选项。以下是示例输出

$ clojure --report stderr -Srepro -X:uberjar
WARNING: Use of -A with clojure.main is deprecated, use -M instead
{:clojure.main/message
 "Execution error (FileNotFoundException) at java.io.FileInputStream/open0 (FileInputStream.java:-2).\n-Srepro (No such file or directory)\n",
 :clojure.main/triage
 {:clojure.error/class java.io.FileNotFoundException,
  :clojure.error/line -2,
  :clojure.error/cause "-Srepro (No such file or directory)",
  :clojure.error/symbol java.io.FileInputStream/open0,
  :clojure.error/source "FileInputStream.java",
  :clojure.error/phase :execution},
 :clojure.main/trace
 {:via
  [{:type java.io.FileNotFoundException,
    :message "-Srepro (No such file or directory)",
    :at [java.io.FileInputStream open0 "FileInputStream.java" -2]}],
  :trace
  [[java.io.FileInputStream open0 "FileInputStream.java" -2]
   [java.io.FileInputStream open "FileInputStream.java" 219]
   [java.io.FileInputStream <init> "FileInputStream.java" 157]
   [java.io.FileInputStream <init> "FileInputStream.java" 112]
   [clojure.lang.Compiler loadFile "Compiler.java" 7575]
   [clojure.main$load_script invokeStatic "main.clj" 475]
   [clojure.main$script_opt invokeStatic "main.clj" 535]
   [clojure.main$script_opt invoke "main.clj" 530]
   [clojure.main$main invokeStatic "main.clj" 664]
   [clojure.main$main doInvoke "main.clj" 616]
   [clojure.lang.RestFn applyTo "RestFn.java" 137]
   [clojure.lang.Var applyTo "Var.java" 705]
   [clojure.main main "main.java" 40]],
  :cause "-Srepro (No such file or directory)"}}

Execution error (FileNotFoundException) at java.io.FileInputStream/open0 (FileInputStream.java:-2).
-Srepro (No such file or directory)

在前两个选项交换后

$ clojure -Srepro --report stderr -X:uberjar
WARNING: Use of -A with clojure.main is deprecated, use -M instead
{:clojure.main/message
 "Execution error (FileNotFoundException) at java.io.FileInputStream/open0 (FileInputStream.java:-2).\n-X:uberjar (No such file or directory)\n",
 :clojure.main/triage
 {:clojure.error/class java.io.FileNotFoundException,
  :clojure.error/line -2,
  :clojure.error/cause "-X:uberjar (No such file or directory)",
  :clojure.error/symbol java.io.FileInputStream/open0,
  :clojure.error/source "FileInputStream.java",
  :clojure.error/phase :execution},
 :clojure.main/trace
 {:via
  [{:type java.io.FileNotFoundException,
    :message "-X:uberjar (No such file or directory)",
    :at [java.io.FileInputStream open0 "FileInputStream.java" -2]}],
  :trace
  [[java.io.FileInputStream open0 "FileInputStream.java" -2]
   [java.io.FileInputStream open "FileInputStream.java" 219]
   [java.io.FileInputStream <init> "FileInputStream.java" 157]
   [java.io.FileInputStream <init> "FileInputStream.java" 112]
   [clojure.lang.Compiler loadFile "Compiler.java" 7575]
   [clojure.main$load_script invokeStatic "main.clj" 475]
   [clojure.main$script_opt invokeStatic "main.clj" 535]
   [clojure.main$script_opt invoke "main.clj" 530]
   [clojure.main$main invokeStatic "main.clj" 664]
   [clojure.main$main doInvoke "main.clj" 616]
   [clojure.lang.RestFn applyTo "RestFn.java" 137]
   [clojure.lang.Var applyTo "Var.java" 705]
   [clojure.main main "main.java" 40]],
  :cause "-X:uberjar (No such file or directory)"}}

Execution error (FileNotFoundException) at java.io.FileInputStream/open0 (FileInputStream.java:-2).
-X:uberjar (No such file or directory)

最后,我使用的 CLI 工具版本

$ clojure --version
Clojure CLI version 1.10.3.855

因此问题是:我如何确保任何未捕获的异常(在这种情况下是 Depstar,但可能是任何内容)被写入 stdout 或 stderr,而不是我无法访问的临时文件?也许是 -X 绕过了(令人烦恼)将异常详情写入临时文件(的)默认行为?

2 个答案

+1

“-report”选项是 clojure.main 的特性(而非 clojure CLI)。(顺便说一下,使用 clojure.main 有一个等效的 Java 系统属性方法来实现这个功能 - see https://clojure.org/reference/repl_and_main#_as_launcher

使用 clj -X,您没有使用 clojure.main,因此这个选项不存在,但-X也永远不应该做写入临时文件的事情,所以我纳闷为什么您需要用-X呢?

由于不熟悉 -X, -M, -A(过去有),-P 等等的内部实现差异,人们不知道(或,坦白讲,也不关心)这些选项的默认异常处理行为存在(不必要)的差异(一些依赖于 clojure.main,其他可能依赖于 JVM 的默认异常处理机制)。

如果这里应用了“最小惊喜”原则,那就太理想了 - 不论使用哪些其他 clj/clojure 选项,异常处理方式始终相同,默认的处理机制可以通过某些一致且通用的选项来覆盖。
考虑一下 -X 执行是否应该承担 clojure.main 的一些错误报告功能。虽然它们是(有意)不同的,但可能会有更多的重叠。
由于我不能在评论中格式化代码,我将编辑我的答案来解决这个问题。
 
是啊,我想不出任何合理的理由来对不同调用的未处理异常进行不同的处理。这似乎是一个容易引起混淆的方案。
 
已记录在 https://clojure.atlassian.net/browse/TDEPS-193,以进一步考虑这个问题。
 
我在运行`clojure -X:sql:dev development/cli-start`,然而当它失败(由于端口未可用)时,我会得到`完整报告在: /var/folders/06/46ycckp94db7k29k022nxjp00000gn/T/clojure-8233560807290300750.edn`,将命令改为`env JAVA_OPTS='-Dclojure.main.report=stderr' clojure -X:sql:dev development/cli-start`会使异常 inline显示。这是在`clojure --version`为1.11.1.1189(且Clojure 1.11.1)的情况下。这似乎直接与你说的话相矛盾,Alex!?
 
正如我在回答中所说

使用 `-X` 可以执行 clojure.main,但它仅适用于 CLI API 的存根,然后调用一个或多个 "exec 函数"。你不能向它传递 --report,但可以使用 JVM 选项。
0

:jvm-opts '["-Dclojure.main.report=stderr"]'

(! 650)-> clojure -X:jar :jar test.jar :main-class foo.bar :aot true
[main] WARN hf.depstar.uberjar - :aot is not recommended for a 'thin' JAR!
[main] INFO hf.depstar.uberjar - Compiling foo.bar ...
Execution error (FileNotFoundException) at user/eval136 (REPL:1).
Could not locate foo/bar__init.class, foo/bar.clj or foo/bar.cljc on classpath.

Full report at:
/var/folders/p1/30gnjddx6p193frh670pl8nh0000gn/T/clojure-13601013524882075105.edn

[main] ERROR hf.depstar.uberjar - Compilation of foo.bar failed!
[main] ERROR hf.depstar.uberjar - AOT FAILED

(! 652)-> clojure -X:jar :jar test.jar :main-class foo.bar :aot true :jvm-opts '["-Dclojure.main.report=stderr"]'
[main] WARN hf.depstar.uberjar - :aot is not recommended for a 'thin' JAR!
[main] INFO hf.depstar.uberjar - Compiling foo.bar ...
{:clojure.main/message
 "Execution error (FileNotFoundException) at user/eval136 (REPL:1).\nCould not locate foo/bar__init.class, foo/bar.clj or foo/bar.cljc on classpath.\n",
 :clojure.main/triage
 {:clojure.error/class java.io.FileNotFoundException,
  :clojure.error/line 1,
  :clojure.error/cause
  "Could not locate foo/bar__init.class, foo/bar.clj or foo/bar.cljc on classpath.",
  :clojure.error/symbol user/eval136,
  :clojure.error/phase :execution},
 :clojure.main/trace
 {:via
  [{:type java.io.FileNotFoundException,
    :message
    "Could not locate foo/bar__init.class, foo/bar.clj or foo/bar.cljc on classpath.",
    :at [clojure.lang.RT load "RT.java" 462]}],
  :trace
  [[clojure.lang.RT load "RT.java" 462]
   [clojure.lang.RT load "RT.java" 424]
   [clojure.core$load$fn__6856 invoke "core.clj" 6115]
   [clojure.core$load invokeStatic "core.clj" 6114]
   [clojure.core$load doInvoke "core.clj" 6098]
   [clojure.lang.RestFn invoke "RestFn.java" 408]
   [clojure.core$load_one invokeStatic "core.clj" 5897]
   [clojure.core$compile$fn__6861 invoke "core.clj" 6125]
   [clojure.core$compile invokeStatic "core.clj" 6125]
   [clojure.core$compile invoke "core.clj" 6117]
   [user$eval136 invokeStatic "NO_SOURCE_FILE" 1]
   [user$eval136 invoke "NO_SOURCE_FILE" 1]
   [clojure.lang.Compiler eval "Compiler.java" 7181]
   [clojure.lang.Compiler eval "Compiler.java" 7136]
   [clojure.core$eval invokeStatic "core.clj" 3202]
   [clojure.main$eval_opt invokeStatic "main.clj" 488]
   [clojure.main$eval_opt invoke "main.clj" 482]
   [clojure.main$initialize invokeStatic "main.clj" 508]
   [clojure.main$null_opt invokeStatic "main.clj" 542]
   [clojure.main$null_opt invoke "main.clj" 539]
   [clojure.main$main invokeStatic "main.clj" 664]
   [clojure.main$main doInvoke "main.clj" 616]
   [clojure.lang.RestFn applyTo "RestFn.java" 137]
   [clojure.lang.Var applyTo "Var.java" 705]
   [clojure.main main "main.java" 40]],
  :cause
  "Could not locate foo/bar__init.class, foo/bar.clj or foo/bar.cljc on classpath."}}

Execution error (FileNotFoundException) at user/eval136 (REPL:1).
Could not locate foo/bar__init.class, foo/bar.clj or foo/bar.cljc on classpath.


[main] ERROR hf.depstar.uberjar - Compilation of foo.bar failed!
[main] ERROR hf.depstar.uberjar - AOT FAILED

(! 667)-> cat src/thrower.clj 
(ns thrower)

(defn bang [_] (throw (ex-info "foo" {})))

(! 668)-> clojure -X thrower/bang
Execution error (ExceptionInfo) at thrower/bang (thrower.clj:3).
foo

Full report at:
/var/folders/p1/30gnjddx6p193frh670pl8nh0000gn/T/clojure-248093423269602852.edn
(! 669)-> clojure -J-Dclojure.main.report=stderr -X thrower/bang
{:clojure.main/message
 "Execution error (ExceptionInfo) at thrower/bang (thrower.clj:3).\nfoo\n",
 :clojure.main/triage
 {:clojure.error/class clojure.lang.ExceptionInfo,
  :clojure.error/line 3,
  :clojure.error/cause "foo",
  :clojure.error/symbol thrower/bang,
  :clojure.error/source "thrower.clj",
  :clojure.error/phase :execution},
 :clojure.main/trace
 {:via
  [{:type clojure.lang.ExceptionInfo,
    :message "foo",
    :data {},
    :at [thrower$bang invokeStatic "thrower.clj" 3]}],
  :trace
  [[thrower$bang invokeStatic "thrower.clj" 3]
   [thrower$bang invoke "thrower.clj" 3]
   [clojure.lang.AFn applyToHelper "AFn.java" 154]
   [clojure.lang.AFn applyTo "AFn.java" 144]
   [clojure.lang.Var applyTo "Var.java" 705]
   [clojure.core$apply invokeStatic "core.clj" 667]
   [clojure.core$apply invoke "core.clj" 662]
   [clojure.run.exec$exec invokeStatic "exec.clj" 40]
   [clojure.run.exec$exec doInvoke "exec.clj" 35]
   [clojure.lang.RestFn invoke "RestFn.java" 423]
   [clojure.run.exec$_main invokeStatic "exec.clj" 169]
   [clojure.run.exec$_main doInvoke "exec.clj" 157]
   [clojure.lang.RestFn applyTo "RestFn.java" 137]
   [clojure.lang.Var applyTo "Var.java" 705]
   [clojure.core$apply invokeStatic "core.clj" 667]
   [clojure.main$main_opt invokeStatic "main.clj" 514]
   [clojure.main$main_opt invoke "main.clj" 510]
   [clojure.main$main invokeStatic "main.clj" 664]
   [clojure.main$main doInvoke "main.clj" 616]
   [clojure.lang.RestFn applyTo "RestFn.java" 137]
   [clojure.lang.Var applyTo "Var.java" 705]
   [clojure.main main "main.java" 40]],
  :cause "foo",
  :data {}}}

Execution error (ExceptionInfo) at thrower/bang (thrower.clj:3).
foo
解决方案:在所有这些中,使用 -J-Dclojure.main.report=stderr 代替!:)
...