Repository navigation
Description
Describe the bug
The file+noindex scheme is underspecified for Windows paths. It seems to follow the file scheme (RFC 8089), but Windows paths are not properly parsed by network-uri. A path like file:///C:/some/path should produce the path C:/some/path, however it produces
/C:/some/path.
$ cabal repl network-uri
...
ghci> import Network.URI
ghci> uri = fromJust $ parseURI "file+noindex:///C:/some/path"
ghci> putStr $ unlines ["Scheme: " <> uriScheme uri, "Auth: " <> show (uriAuthority uri), "Path: " <> uriPath uri, "Frag: " <> uriFragment uri]
Scheme: local+noindex:
Auth: Just (URIAuth {uriUserInfo = "", uriRegName = "", uriPort = ""})
Path: /C:/some/path
Frag:
ghci> readFile $ uriPath uri
*** Exception: /C:/some/path: openFile: invalid argument (Invalid argument)
And if the last slash is removed, then the C: part is parsed as part of the auth:
$ cabal repl -b network-uri
...
ghci> import Network.URI
ghci> uri = fromJust $ parseURI "file+noindex://C:/some/path"
ghci> putStr $ unlines ["Scheme: " <> uriScheme uri, "Auth: " <> show (uriAuthority uri), "Path: " <> uriPath uri, "Frag: " <> uriFragment uri]
Scheme: local+noindex:
Auth: Just (URIAuth {uriUserInfo = "", uriRegName = "C", uriPort = ":"})
Path: /some/path
Frag:
ghci> readFile $ uriPath uri
"The contents of the file in C:/some/path\n"
ghci>
$ cd D:/
$ cabal repl -b network-uri
...
ghci> readFile "/some/path"
*** Exception: /some/path: openFile: does not exist (No such file or directory)
Just by luck, paths starting with / are interpreted as paths in the current drive so if we just happen to want that it might work.
If no volume or drive letter is specified and the directory name begins with the directory separator character, the path is relative from the root of the current drive.
The right parsing would be what file-uri does:
ghci> import System.URI.File
ghci> uri = fromRight undefined $ parseFileURI ExtendedWindows "file:///C:/some/path"
ghci> uri
FileURI {fileAuth = Nothing, filePath = "C:/some/path"}
ghci> readFile $ Data.ByteString.Char8.unpack $ filePath uri
"The contents of the file in C:/some/path\n"
Which results in a path accessible from everywhere in the system. However this is not the end of the story because:
file-urionly allowsfile:schemes, it will not allowfile+noindex:uris.- The file uri RFC does not mention fragments, so
file-uridoes not parse those.
We can either define a parser for file+noindex from scratch, or do some tricks to parse the uri as a network-uri uri, then fabricate an intermediate uri with the file: scheme and use file-uri or something else. I just didn't want this finding to go missing again.
Activity
jasagredo commentedon Jan 3, 2025
It probably doesn't make much sense fixing this before #8889 goes in.
jasagredo commentedon Jan 3, 2025
Also this PR fixes the issue but in a slightly hacky way MercuryTechnologies#3. It was dropped in the end from what was merged into
masterphadej commentedon Jan 3, 2025
I don't think it make sense to support DOS filepaths. UNC paths could already work (I don't have windows machine to try).
phadej commentedon Jan 3, 2025
Also duplicate of #7065: looks like UNC paths do work!
jasagredo commentedon Jan 3, 2025
Thanks @phadej. I can try with UNC paths next week. Not sure if they need to be normalised or one can open a path
//?/C:/some/pathjasagredo commentedon Jan 7, 2025
@phadej It does not work with UNC paths:
No matter which combination of
/I use, it will consider the path part of the query. Also notice we would have to postprocess the filepath:phadej commentedon Jan 7, 2025
jasagredo commentedon Jan 7, 2025
I see.
\\.\could indeed work. I find it quite convoluted and inconvenient for Windows users of cabal (as opposed to parsing a file uri with ExtendedWindows extension), but I agree that it would work.Btw, where is your quote from?
phadej commentedon Jan 8, 2025
From the already linked https://learn.microsoft.com/en-us/dotnet/standard/io/file-path-formats#unc-paths
phadej commentedon Jan 8, 2025
To comment on original post
It's not.
file-urisaysAnd more importantly, we need to parse the schema first to even know we are parsing file-path like URIs. The repository location is a URL, it can be
file:schema, but more often it'shttporhttps.I guess e.g. Chrome does some extra processing so
file:///C:/foo.txtdoes work, but it's not just "always strip leading slash on Windows", as UNC paths must work too. If UNC paths work without extra processing, that's IMO good enough.jasagredo commentedon Jan 8, 2025
Ok, I am convinced. BTW,
//./..paths also need to be normalised before being used.ulysses4ever commentedon Jan 8, 2025
Did #9540 run into this kind of issue? Could the discussion here be used to unblock #9540?
jasagredo commentedon Jan 8, 2025
Maybe. I have not heard from Andrea in quite some time. Don't know what is the plan with those PRs.
ulysses4ever commentedon Jan 8, 2025
Yes, my understanding is that Andrea is currently not around, and if we want some of his drafts merged, we have to take care of them on our own. I've been hoping that we can bring #9540 over the line ourselves because (my impression) the feature is fully implemented; it just doesn't work on Windows because of the paths confusion.
jasagredo commentedon Jan 8, 2025
@phadej to add to the problem: haskell/filepath#247 😢
phadej commentedon Jan 8, 2025
I don't see a problem.
EDIT:
Looks like a bug though.
jasagredo commentedon Jan 8, 2025
I first saw your message on the mail (without the edited part) and I was questioning my own sanity once again 😁