From 77e3c81f390b8cce016c8123150d0a16acac6747 Mon Sep 17 00:00:00 2001 From: Stephen Amar Date: Thu, 15 May 2025 11:30:00 -0700 Subject: [PATCH] Add --debug-importer flag to print out what the default importer is doing --- sjsonnet/src-jvm-native/sjsonnet/Config.scala | 5 ++ sjsonnet/src-jvm-native/sjsonnet/OsPath.scala | 2 - .../sjsonnet/SjsonnetMain.scala | 50 +++++++++++++++---- 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/sjsonnet/src-jvm-native/sjsonnet/Config.scala b/sjsonnet/src-jvm-native/sjsonnet/Config.scala index c102106f..dc6c22dc 100644 --- a/sjsonnet/src-jvm-native/sjsonnet/Config.scala +++ b/sjsonnet/src-jvm-native/sjsonnet/Config.scala @@ -10,6 +10,11 @@ final case class Config( doc = "Specify an additional library search dir (left-most wins)" ) jpaths: List[String] = Nil, + @arg( + name = "debug-importer", + doc = "Print some additional debugging information about the importer" + ) + debugImporter: Flag = Flag(), @arg( name = "output-file", short = 'o', diff --git a/sjsonnet/src-jvm-native/sjsonnet/OsPath.scala b/sjsonnet/src-jvm-native/sjsonnet/OsPath.scala index 17348ace..1ae98985 100644 --- a/sjsonnet/src-jvm-native/sjsonnet/OsPath.scala +++ b/sjsonnet/src-jvm-native/sjsonnet/OsPath.scala @@ -6,8 +6,6 @@ final case class OsPath(p: os.Path) extends Path { def relativeToString(other: Path): String = p.relativeTo(other.asInstanceOf[OsPath].p).toString def parent(): OsPath = OsPath(p / os.up) def segmentCount() = p.segmentCount - def debugRead(): Option[String] = try Some(os.read(p)) - catch { case e: Throwable => None } def last: String = p.last def /(s: String): Path = OsPath(p / s) diff --git a/sjsonnet/src-jvm-native/sjsonnet/SjsonnetMain.scala b/sjsonnet/src-jvm-native/sjsonnet/SjsonnetMain.scala index a5d355cd..0d27a9dc 100644 --- a/sjsonnet/src-jvm-native/sjsonnet/SjsonnetMain.scala +++ b/sjsonnet/src-jvm-native/sjsonnet/SjsonnetMain.scala @@ -12,10 +12,12 @@ import java.nio.charset.StandardCharsets import java.nio.file.NoSuchFileException import scala.annotation.unused import scala.util.Try -import scala.util.control.NonFatal object SjsonnetMain { - def resolveImport(searchRoots0: Seq[Path], allowedInputs: Option[Set[os.Path]] = None): Importer = + def resolveImport( + searchRoots0: Seq[Path], + allowedInputs: Option[Set[os.Path]] = None, + debugImporter: Boolean = false): Importer = new Importer { def resolve(docBase: Path, importName: String): Option[Path] = (docBase +: searchRoots0) @@ -28,15 +30,33 @@ object SjsonnetMain { case a: os.Path => Some(a) } ) - .filter(p => allowedInputs.fold(true)(_(p))) + .filter(p => { + val allowed = allowedInputs.fold(true)(_(p)) + if (debugImporter) { + if (allowed) System.err.println(s"[import $importName] candidate $p") + else + System.err.println( + s"[import $importName] excluded $p because it's not in $allowedInputs" + ) + } + allowed + }) .find(os.exists) - .flatMap(p => - try Some(OsPath(p)) - catch { case NonFatal(_) => None } - ) + .orElse({ + if (debugImporter) { + System.err.println(s"[import $importName] none of the candidates exist") + } + None + }) + .flatMap(p => { + if (debugImporter) { + System.err.println(s"[import $importName] $p is selected as it exists") + } + Some(OsPath(p)) + }) def read(path: Path, binaryData: Boolean): Option[ResolvedFile] = { - readPath(path, binaryData) + readPath(path, binaryData, debugImporter) } } @@ -242,7 +262,11 @@ object SjsonnetMain { } } case None => - resolveImport(config.jpaths.map(os.Path(_, wd)).map(OsPath.apply), allowedInputs) + resolveImport( + config.jpaths.map(os.Path(_, wd)).map(OsPath.apply), + allowedInputs, + config.debugImporter.value + ) }, parseCache, settings = new Settings( @@ -342,7 +366,10 @@ object SjsonnetMain { * of caching on top of the underlying file system. Small files are read into memory, while large * files are read from disk. */ - private def readPath(path: Path, binaryData: Boolean): Option[ResolvedFile] = { + private def readPath( + path: Path, + binaryData: Boolean, + debugImporter: Boolean = false): Option[ResolvedFile] = { val osPath = path.asInstanceOf[OsPath].p if (os.exists(osPath) && os.isFile(osPath)) { Some( @@ -353,6 +380,9 @@ object SjsonnetMain { ) ) } else { + if (debugImporter) { + System.err.println(s"[read $path] file does not exist or is not a file") + } None } }