Skip to content

Commit

Permalink
Avoid normalizing input .ksy paths in stderr and JSON output
Browse files Browse the repository at this point in the history
See kaitai-io/kaitai_struct#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
kaitai-io/kaitai_struct#507 (comment)
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
kaitai-io/kaitai_struct#507 (comment),
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.
  • Loading branch information
generalmimon committed Aug 17, 2023
1 parent 7071a9d commit 4c5096b
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions jvm/src/main/scala/io/kaitai/struct/JavaMain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -35,7 +35,7 @@ object JavaMain {

head(Version.name, Version.version)

arg[File]("<file>...") unbounded() action { (x, c) =>
arg[String]("<file>...") unbounded() action { (x, c) =>
c.copy(srcFiles = c.srcFiles :+ x) } text("source files (.ksy)")

// opt[File]('o', "outfile") valueName("<file>") action { (x, c) =>
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 4c5096b

Please sign in to comment.