2024 Clojure调查中分享您的想法!

欢迎!请查看关于页面以获取更多有关该工作方式的信息。

0
工具
编辑

背景

  1. 我的构建在一个容器中运行,我无法访问临时文件(它们在构建完成后就会被删除,无论成功与否)。因此,我将 --report stderr 选项添加到所有clojure命令的调用中。
  2. 我的构建使用Depstar创建uberjar,这需要使用 -X:... 选项。
  3. 我还使用 -Srepro 选项。

问题

当Cljs命令行中包含-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

所以问题是:如何确保任何未捕获的异常都被写入stdout或stderr,而不是我无法访问的临时文件?-X跳过了这个(令人烦恼的)默认行为,把异常详情写入到临时文件,也许吗?

2 个答案

+1

“--report”选项是clojure.main(而非clojure CLI)的功能。(顺便说一句,clojure.main中还有与之等价的Java系统属性方式来实现 - 见https://clojure.org/reference/repl_and_main#_as_launcher

使用 clj -X 时,您并没有使用 clojure.main,因此此选项不存在。但是 -X 应当根本不会进行写入临时文件的操作,因此我不明白为什么您还需要 -X 前的此选项?

这令人困惑,因为不了解 -X、-M、-A(以前)、-P 等.等.选项内部实现差异的人不知道(或,tbqh,在乎)这些选项的默认异常处理行为存在(不必要的)差异(一些依赖于 clojure.main,而另一些可能依托于JVM的默认异常处理机制)。

如果始终如一地应用“最小惊异”原则,即可 - 不论使用哪些其他的 clj/clojure 选项,异常处理始终以相同的方式处理,并且可以通过一些一致且通用的选项来覆盖默认处理机制。
应该考虑 -X 执行是否应承担 clojure.main 的一些错误报告功能。虽然它们(有意)不同,但可能会有更多的关联。
由于我无法在评论中格式化代码,我将编辑我的回答来解决这个问题。
我无法想出任何合理的理由,说明根据被调用的内容以及方式将未处理的异常进行区别对待。这似乎会引发混乱。
by
https://clojure.atlassian.net/browse/TDEPS-193的身份记录,以便更多思考。
by
我运行的是 `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` 将异常显示为内联。这是在 `clojure --version` 为 1.11.1.1189(且 Clojure 为 1.11.1)的情况下进行的。这似乎直接与您所说的相矛盾,Alex?!
by
正如我在回答中所说

使用 -X 会执行 clojure.main,但它只用于 CLI API 模拟,然后调用一个或多个“exec 函数”。您不能向其传递 --report,但可以使用 JVM 选项。
by
在此处的实现经历了几次变化。在当前版本中,-X/-T 将通过 clojure.main 执行函数。我目前认为这是实现详细信息(但可能是一个将持续下去的详细信息)。我们已经在 https://clojure.atlassian.net/browse/TDEPS-193 的 jira 中考虑了这个问题。
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

...