z-jxy / rpkl

Pkl bindings for Rust
MIT License
3 stars 2 forks source link

On Windows, canonicalized path has invalid characters #3

Closed FQAlmeida closed 2 weeks ago

FQAlmeida commented 3 weeks ago

Im trying to work with Pkl on Windows, and when I call from_config::<Simple>("./configs/simple.pkl"), the evaluator tries to parse file://\\?\C:\Users\otavi\projetos\Pkl-Teste\configs\simple.pkl, but backslashes and the question mark are invalid characters.

To be precise, I receive the following error message:


java.net.URISyntaxException: Illegal character in authority at index 7: file://\\?\C:\Users\otavi\projetos\Pkl-Teste\configs\simple.pkl
        at java.base@17.0.10/java.net.URI$Parser.fail(URI.java:2974)
        at java.base@17.0.10/java.net.URI$Parser.parseAuthority(URI.java:3308)
        at java.base@17.0.10/java.net.URI$Parser.parseHierarchical(URI.java:3219)
        at java.base@17.0.10/java.net.URI$Parser.parse(URI.java:3175)
        at java.base@17.0.10/java.net.URI.<init>(URI.java:623)
        at org.pkl.server.MessagePackDecoder.decode(MessagePackDecoder.kt:81)
        at org.pkl.server.MessageTransports$EncodingMessageTransport.doStart(MessageTransports.kt:49)
        at org.pkl.server.MessageTransports$AbstractMessageTransport.start(MessageTransports.kt:110)
        at org.pkl.server.Server.start(Server.kt:42)
        at org.pkl.cli.CliServer.doRun(CliServer.kt:29)
        at org.pkl.commons.cli.CliCommand.run(CliCommand.kt:44)
        at org.pkl.cli.commands.ServerCommand.run(ServerCommand.kt:30)
        at com.github.ajalt.clikt.parsers.Parser.parse(Parser.kt:198)
        at com.github.ajalt.clikt.parsers.Parser.parse(Parser.kt:211)
        at com.github.ajalt.clikt.parsers.Parser.parse(Parser.kt:18)
        at com.github.ajalt.clikt.core.CliktCommand.parse(CliktCommand.kt:400)
        at com.github.ajalt.clikt.core.CliktCommand.parse$default(CliktCommand.kt:397)
        at com.github.ajalt.clikt.core.CliktCommand.main(CliktCommand.kt:415)
        at com.github.ajalt.clikt.core.CliktCommand.main(CliktCommand.kt:440)
        at org.pkl.cli.Main$main$1.invoke(Main.kt:39)
        at org.pkl.cli.Main$main$1.invoke(Main.kt:27)
        at org.pkl.commons.cli.CliMainKt.cliMain(CliMain.kt:37)
        at org.pkl.cli.Main.main(Main.kt:27)

I got it to work be just replacing \ to / and removing ?, but I dont know if it is safe to do that.

z-jxy commented 3 weeks ago

This is due to how std::path::Path:: canonicalize works on windows, since the file is on your local machine it uses \\?\ as part of the UNC path. Stripping the prefix would be fine in this case, but it could result in invalid paths if someone tries to load a remote file (not sure if this would actually work).

I haven't tried pkl on Windows due to the CLI not being tested on there (pretty sure this is why it detects \ as an illegal character), but I think using dunce to handle this on Windows could work

z-jxy commented 3 weeks ago

Support for Windows seems to be on the main branch already and it seems like they've been tracking on the issues with paths.

I'll verify if the issue is fixed in the next release (scheduled for this month)

bioball commented 2 weeks ago

Absolute paths on Windows should turn into file URIs, where:

  1. backslashes are replaced with forward slashes
  2. a preceding forward slash is prefixed

So this path: C:\Users\otavi\projetos\Pkl-Teste\configs\simple.pkl
Should turn into: file:///C:/Users/otavi/projetos/Pkl-Teste/configs/simple.pkl

See note here: https://pkl-lang.org/main/current/language-reference/index.html#relative-uris