Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions sjsonnet/src-jvm-native/sjsonnet/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 0 additions & 2 deletions sjsonnet/src-jvm-native/sjsonnet/OsPath.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
50 changes: 40 additions & 10 deletions sjsonnet/src-jvm-native/sjsonnet/SjsonnetMain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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
}
}
Expand Down