`canonicalizePath "\\\\localhost\\C$\.."` incorrectly produces `"\\\\localhost\\"`
type: a-bug
Lifted from https://github.com/agda/agda/issues/8116#issuecomment-3391680164:
```haskell
import System.Directory
import System.FilePath
list :: FilePath -> IO ()
list dir = do
putStrLn $ replicate 66 '-'
putStrLn dir
putStrLn $ replicate 66 '-'
mapM_ (putStrLn . ("- " ++)) . take 2 =<< listDirectory dir
up :: FilePath -> IO FilePath
up dir = canonicalizePath (dir </> "..")
upIO :: IO FilePath -> IO FilePath
upIO = (up =<<)
-- https://stackoverflow.com/questions/2787203/unc-path-to-a-folder-on-my-local-computer
main :: IO ()
main = do
-- let dir = "\\\\?\\C:\\Users\\admin"
let dir = "\\\\localhost\\C$\\Users\\admin"
let ups = take 4 $ iterate upIO (pure dir)
mapM_ (list =<<) ups
```
This produces:
```
------------------------------------------------------------------
\\localhost\C$\Users\admin
------------------------------------------------------------------
- VirtualBox VMs
- Videos
------------------------------------------------------------------
\\localhost\C$\Users
------------------------------------------------------------------
- Public
- desktop.ini
------------------------------------------------------------------
\\localhost\C$
------------------------------------------------------------------
- Windows
- Users
------------------------------------------------------------------
\\localhost\
------------------------------------------------------------------
test-directory: Uncaught exception ghc-internal:GHC.Internal.IO.Exception.IOExce
ption:
\\localhost\: getDirectoryContents:findFirstFile: does not exist (The specified
path is invalid.)
While handling findFirstFile: does not exist (The specified path is invalid.)
```
In contrast, `"\\\\?\\C:\\Users\\admin"` is treated correctly. Canonicalization turns this into `C:\Users\admin`.
There is a suspicion that `canonicalizePath` does not work correctly on UNC pathes.
The unitests for `canonicalizePath` seem to steer clear of UNC paths, except for this one: https://github.com/haskell/directory/blob/6442a3cf04f74d82cdf8c9213324313d52b23d28/tests/CanonicalizePath.hs#L164-L166
关闭于 2025-11-30 5 条评论