Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: undefined fun 'getuid' for LibC #291

Closed
jwoertink opened this issue Oct 13, 2024 · 6 comments
Closed

Error: undefined fun 'getuid' for LibC #291

jwoertink opened this issue Oct 13, 2024 · 6 comments

Comments

@jwoertink
Copy link
Contributor

jwoertink commented Oct 13, 2024

I'm working on getting Avram Windows Support and when running on Windows, I got this error

image

It looks like it's coming from here

System::User.find_by(id: LibC.getuid.to_s).username

This is running Crystal 1.14.0 on whatever windows-latest is.

Maybe related: crystal-lang/crystal#12605

@will
Copy link
Owner

will commented Oct 25, 2024

Thanks for reporting this! The username is there to mimic the default behavior of libpq where by default it'll try to connect to a database with your current user's name and using a role named after your user.

I figure there are two ways forward to fix this on Windows

  1. just remove the default detection on windows and force database names and roles to be explicitly set.
  2. Figure out how to get the current username on windows.

1 is a lot easier to do, especially without access to a windows computer and the tradeoff of being explicit doesn't feel too onerous, but for sure 2 is a nicer experience over all.

What do you think?

@straight-shoota
Copy link
Contributor

@will
Copy link
Owner

will commented Oct 25, 2024

Thanks for finding that @straight-shoota . Also relevant https://github.com/postgres/postgres/blob/7175ef870e24763a561821685160c5e35b2e8989/src/interfaces/libpq/fe-auth.c#L1255-L1262 where for non-windows they get the user id to call that function you linked, but for windows it's ignored and so instead hardcoded to 0.

I'm not sure what Windows header file GetUserName comes from. Searching comes up with a similar but not exactly the same named function https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getusernamea . I do think GetUserName with no suffix is a windows api function given this 22 year old commit postgres/postgres@469cb65 , but I'm afraid I really know next to nothing about Windows.

Also related, but I completely forgot but it looks like I tried to push this current user code into the crystal stdlib a while ago, but also in that PR didn't do Windows so it doesn't help much here. Still maybe this get-the-current-user does belong in stdlib?

@straight-shoota
Copy link
Contributor

GetUserName is a macro that expands to either GetUserNameA or GetUserNameW automatically, depending on whether the program is using ASCII or UTF-16 strings. This is a common pattern for all string-related functions in the WIN32 API.
In Crystal we need to call GetUserNameW directly.

@jwoertink
Copy link
Contributor Author

remove the default detection on windows and force database names and roles to be explicitly set.

I'm ok with this solution, at least for now. It could even be updated later to support solution 2.

@guillaume-rce
Copy link

guillaume-rce commented Nov 5, 2024

Hi! I have the same issue

Showing last frame. Use --error-trace for full trace.

In lib\pg\src\pq\conninfo.cr:174:37

 174 | System::User.find_by(id: LibC.getuid.to_s).username
                                     ^-----
Error: undefined fun 'getuid' for LibC

@will will closed this as completed in cafef48 Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants