haskell / directory

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

FR: findExecutable "" should return Nothing, like on linux #180

Closed TerrorJack closed 2 months ago

TerrorJack commented 5 months ago

On Windows, findExecutable "" would throw *** Exception: searchPath: invalid argument (The parameter is incorrect.). However, on Linux it simply returns Nothing. I believe this is a bug and we should align Windows behavior with Linux to also return Nothing.

hasufell commented 5 months ago

What are the affected versions?

TerrorJack commented 5 months ago

directory-1.3.8.4 as shipped in ghc head.

hasufell commented 5 months ago

I can also reproduce this with directory-1.3.7.1 from GHC-9.4.8. So it is not related to the OsPath migration.

Rufflewind commented 3 months ago

This appears to be canonical behavior for the Win32 SearchPathW API:

#include <stdio.h>
#include <windows.h>

int main(void)
{
    wchar_t buffer[512] = {0};
    if (SearchPathW(NULL, L"", L".exe", 512, buffer, NULL) == 0) {
        return GetLastError();
    }
    printf("%ls\n", buffer);
    return 0;
}

This fails with ERROR_INVALID_PARAMETER (87).

Given that the directory implementation of findExecutable has always relied on SearchPathW, this is unlikely a regression and has probably worked this way since the beginning.

I think it is a reasonable feature request though. The SearchPathW seems to handle other kinds of nonsensical strings just fine, except for the empty string.