From 4c5096ba53d559fcfe235a5a0df1082e7f427e49 Mon Sep 17 00:00:00 2001 From: Petr Pucil Date: Thu, 17 Aug 2023 23:41:33 +0200 Subject: [PATCH] Avoid normalizing input .ksy paths in stderr and JSON output See https://github.com/kaitai-io/kaitai_struct/issues/507 Before this commit, the input .ksy paths were normalized as soon as they were parsed from command line arguments, because they were read to `java.io.File` objects. [`java.io.File`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/File.html) objects are subjected to path normalization as soon as they're created, for example on Windows the forward slashes `/` are converted to backslashes `\`, a series of consecutive slashes are collapsed into one on both Unix and Windows systems etc. (See https://github.com/kaitai-io/kaitai_struct/issues/507#issuecomment-1679686055 for more details.) Consequently, the .ksy file path for which the compile results are reported may not have matched the original input path. This was particularly noticeable on Windows, where you could only use backslashes (not forward slashes) in the .ksy path passed to `ksv` due to this. As I explain in https://github.com/kaitai-io/kaitai_struct/issues/507#issuecomment-1679530418, I believe that treating input paths as opaque strings makes the compiler easier to operate. Now you can pass a bunch of .ksy paths to the compiler and expect to find each path string used exactly as a JSON key in `--ksc-json-output`, without any effects of path normalization mentioned earlier. --- jvm/src/main/scala/io/kaitai/struct/JavaMain.scala | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/jvm/src/main/scala/io/kaitai/struct/JavaMain.scala b/jvm/src/main/scala/io/kaitai/struct/JavaMain.scala index 210b556ac..2e0c6b57a 100644 --- a/jvm/src/main/scala/io/kaitai/struct/JavaMain.scala +++ b/jvm/src/main/scala/io/kaitai/struct/JavaMain.scala @@ -16,7 +16,7 @@ object JavaMain { case class CLIConfig( verbose: Seq[String] = Seq(), - srcFiles: Seq[File] = Seq(), + srcFiles: Seq[String] = Seq(), outDir: File = new File("."), targets: Seq[String] = Seq(), throwExceptions: Boolean = false, @@ -35,7 +35,7 @@ object JavaMain { head(Version.name, Version.version) - arg[File]("...") unbounded() action { (x, c) => + arg[String]("...") unbounded() action { (x, c) => c.copy(srcFiles = c.srcFiles :+ x) } text("source files (.ksy)") // opt[File]('o', "outfile") valueName("") action { (x, c) => @@ -259,13 +259,13 @@ class JavaMain(config: CLIConfig) { def run(): Unit = { val logs: Map[String, InputEntry] = config.srcFiles.map { srcFile => val log = if (config.throwExceptions) { - compileOneInput(srcFile.toString) + compileOneInput(srcFile) } else { try { - compileOneInput(srcFile.toString) + compileOneInput(srcFile) } catch { case ex: Throwable => - InputFailure(List(exceptionToCompileError(ex, srcFile.toString))) + InputFailure(List(exceptionToCompileError(ex, srcFile))) } } if (!config.jsonOutput) { @@ -276,7 +276,7 @@ class JavaMain(config: CLIConfig) { problems.foreach { (p) => Console.err.println(p.message) } } - srcFile.toString -> log + srcFile -> log }.toMap if (config.jsonOutput) {