pkl icon indicating copy to clipboard operation
pkl copied to clipboard

opaque uris and resources interaction

Open BenFradet opened this issue 2 months ago • 4 comments

with 0.29, opaque uris are invalid: https://pkl-lang.org/main/current/release-notes/0.29.html#opaque-file-uris-are-invalid

the error message says:

–– Pkl Error ––
Resource URI `file:../path/.manifest.yml` has invalid syntax.

30 | const manifest = yamlParser.parse(read("file:../\(manifestPath)"))
                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at Common#manifest (file:///pkl/Common.pkl, line 30)

File URIs must have a path that starts with `/` (e.g. file:/path/to/my_module.pkl).
To resolve relative paths, remove the scheme prefix (remove "file:").

However, read requires a Resource which mandates the file: prefix.

It is a bit awkward to do

const workingDirectory = read("env:PWD")
const manifest = yamlParser.parse(read("file:\(workingDirectory)/../\(manifestPath)"))

Are there any alternatives?

BenFradet avatar Oct 24 '25 13:10 BenFradet

Is that resource at a relative path to your Pkl file? If so, you'd just pass in the relative path:

const manifest = yamlParser.parse(read("../\(manifestPath)"))

bioball avatar Oct 24 '25 16:10 bioball

If you do truly want to read relative to the working directory, here's how to do it in a robust, cross-platform way:

https://github.com/apple/pkl-pantry/blob/7a8db5d291bd91b0a1cd0fcc3e6284a167968abf/packages/k8s.contrib.crd/generate.pkl#L98-L107

HT154 avatar Oct 24 '25 16:10 HT154

Is that resource at a relative path to your Pkl file? If so, you'd just pass in the relative path:

const manifest = yamlParser.parse(read("../(manifestPath)"))

this doesn't work, it fails with:

–– Pkl Error ––
Cannot find resource `../path/manifest.yml`.

The prefix in Resource might me mandatory?

BenFradet avatar Oct 27 '25 07:10 BenFradet

The prefix in Resource might me mandatory?

It is not. Reading with relative path reads relative to the module the read is in. If you are intending to read relative to the working directory, you need to use the more verbose invocation I linked above.

HT154 avatar Oct 27 '25 15:10 HT154