haskell / directory

Platform-independent library for basic file system operations
https://hackage.haskell.org/package/directory
Other
58 stars 47 forks source link

doesDirectoryExist prints stacktrace when `+RTS -xc` #125

Closed yaitskov closed 3 years ago

yaitskov commented 3 years ago

Hi,

I develop an application and had problem with locating place in the app where exception happens. I built my app with profiling and run with -xc. I realized that among the exception causing the problem to me, there are lots of stack traces generated for caught exceptions. These exceptions are part of program logic. They don't break anything obviously, but they make analysis application for problems much harder.

doesDirectoryExist is one of such functions used in the app.

doesDirectoryExist :: FilePath -> IO Bool
doesDirectoryExist path = do
  pathIsDirectory path
    `catchIOError` \ _ ->
      pure False

I don't know yet how GHC can help to avoid stack-traces of caught exceptions, but I would like to see alternative implementation without catching exception.

yaitskov commented 3 years ago

https://gitlab.haskell.org/ghc/ghc/-/issues/20536

Rufflewind commented 3 years ago

Sorry, I'm afraid this issue is not really feasible to fix.

The standard way to check whether a file/directory exists is in fact by calling a system API like stat and testing to see if an error has occurred. Errors from system APIs are converted to Haskell exceptions in the underlying Win32 and unix libraries. Therefore, the only way to avoid exceptions is if those libraries are completely rewritten to return error codes instead of raising exceptions, which I doubt is feasible nor elegant, and even if it was, directory is not the right place to ask for such a change. You would need to convince the upstream maintainer that this is worthwhile.

I think you would have better luck fixing the profiling tools to exclude caught exceptions.