Closed petr-k closed 4 years ago
You're right. Thanks for pointing this out.
This was done for compatibility with C++ jsonnet.
IMO the right way to do it would be to have canonicalizePath(codePath, importedPath string)
function in Importer
interface. This way interpreter would assume nothing about the structure of the path. That would also help with caching (caching once per code directory is not ideal). Having a way to canonicalize paths would let us cache every file exactly once.
@sparkprime What do you think?
Yes I was thinking the same. I also think that was proposed in https://github.com/google/jsonnet/issues/239
In other words, if the importer is responsible for deriving the new path from the current file and the possibly relative import string, then there is no need for it to expose a splitting function.
Just to provide some context, I stumbled upon this when implementing an importer that would support URLs in import
statements and search paths for the [kubecfg] tool: (https://github.com/ksonnet/kubecfg) https://github.com/ksonnet/kubecfg/issues/182
C++ implementation did not canonicalize paths: https://github.com/google/jsonnet/blob/master/core/vm.cpp#L37
This has been solved a while back.
When importing jsonnet documents using the
import
statement, the current implementation assumes that the resource returned from importer'sFoundHere
is a filesystem path. On subsequent imports, the base dir of that path is derived via a call topath.Dir()
(see e.g. https://github.com/google/go-jsonnet/blob/master/interpreter.go#L415). This function always performs a path cleanup.This is a problem when implementing custom importers that do not use filesystem paths, but for example URLs. E.g. when an importer returns
https://raw.githubusercontent.com/ksonnet/ksonnet-lib/master/ksonnet.beta.2/k.libsonnet
inFoundHere
, upon resolving relative imports based on that URL, theimportDir
comes back to the importer cleaned up ashttps:/raw.githubusercontent.com/ksonnet/ksonnet-lib/master/ksonnet.beta.2
(notice the single forward slash inhttps:/
).go-jsonnet
should not assume filesystem paths, in fact the resource "path" should be opaque.