Treat opaque file URIs as errors (#1087)

Opaque file URIs are URIs whose scheme-specific part does not start with `/`.
For example, `file:foo/bar.txt` is an opaque URI.

Currently, this has the unintentional behavior of: look for file `foo/bar.txt` from the process working directory.
These are effectively dynamics imports; from a single import, we can't statically analyze what it resolves as.

According to RFC-8089, File URIs must have paths that start with `/`. So, these are actually _not valid URIs_.
See the grammar defined in https://datatracker.ietf.org/doc/html/rfc8089#section-2

This changes Pkl's behavior so that these URIs are treated as errors.
This commit is contained in:
Daniel Chao
2025-06-03 14:57:22 -07:00
committed by GitHub
parent 4eeb61dc74
commit 2bc9c2f424
16 changed files with 109 additions and 9 deletions

View File

@@ -0,0 +1 @@
res = read("file:path/to/foo.txt")

View File

@@ -0,0 +1 @@
res = read*("file:path/to/foo.txt")

View File

@@ -0,0 +1 @@
res = import("file:path/to/foo.pkl")

View File

@@ -0,0 +1,9 @@
// Ideally, this should have the same error message as `invalidFileUri2`; the error is that the glob
// pattern is invalid.
//
// But, this is somewhat challenging to fix, because the `URISyntaxException` gets thrown before
// `ImportGlobNode` is initialized.
//
// Regardless, this error is good enough (given this error message, users know what to do), and we
// can afford to be pragmatic here.
res = import*("file:path/to/foo.pkl")

View File

@@ -0,0 +1,13 @@
Pkl Error
Resource URI `file:path/to/foo.txt` has invalid syntax.
x | res = read("file:path/to/foo.txt")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at invalidFileUri1#res (file:///$snippetsDir/input/errors/invalidFileUri1.pkl)
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:").
xxx | text = renderer.renderDocument(value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at pkl.base#Module.output.text (pkl:base)

View File

@@ -0,0 +1,12 @@
Pkl Error
Invalid glob pattern `file:path/to/foo.txt`.
x | res = read*("file:path/to/foo.txt")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at invalidFileUri2#res (file:///$snippetsDir/input/errors/invalidFileUri2.pkl)
Scheme `file` requires a hierarchical path (there must be a `/` after the first colon).
xxx | text = renderer.renderDocument(value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at pkl.base#Module.output.text (pkl:base)

View File

@@ -0,0 +1,13 @@
Pkl Error
Module URI `file:path/to/foo.pkl` has invalid syntax.
x | res = import("file:path/to/foo.pkl")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at invalidFileUri3#res (file:///$snippetsDir/input/errors/invalidFileUri3.pkl)
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:").
xxx | text = renderer.renderDocument(value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at pkl.base#Module.output.text (pkl:base)

View File

@@ -0,0 +1,13 @@
Pkl Error
Module URI `file:path/to/foo.pkl` has invalid syntax.
x | res = import*("file:path/to/foo.pkl")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at invalidFileUri4#res (file:///$snippetsDir/input/errors/invalidFileUri4.pkl)
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:").
xxx | text = renderer.renderDocument(value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at pkl.base#Module.output.text (pkl:base)