pganalyze / pg_query_go

Go library to parse and normalize SQL queries using the PostgreSQL query parser
BSD 3-Clause "New" or "Revised" License
663 stars 79 forks source link

Not able to build on windows #25

Closed dharmjit closed 11 months ago

dharmjit commented 4 years ago

Hi, I am tryoing to build this project on windows and getting below error

$ go get github.com/lfittl/pg_query_go                                                                                                 # github.com/lfittl/pg_query_go/parser                                                                                                 In file included from .\include/c.h:1129:0,                                                                                                             from .\include/postgres.h:47,                                                                                                          from contrib_pgcrypto_sha1.c:50:                                                                                      .\include/port.h:17:19: fatal error: netdb.h: No such file or directory                                                                compilation terminated.

I have tried the modules step as well but no success. I have searched for netdb.h and this file is not there in the include folder. How I can resolve this. Thanks

dharmjit commented 4 years ago

Hi, Any update on this.

maxiride commented 4 years ago

I guess I have the same error as @dharmjit but the inline formatting really doesn't help in reading it 😅 (please fix it)

Microsoft Windows [Version 10.0.18363.778]
(c) 2019 Microsoft Corporation. All rights reserved.

D:\Federico\Projects>go get github.com/lfittl/pg_query_go
# github.com/lfittl/pg_query_go/parser
In file included from C:\Users\maxir\go\pkg\mod\github.com\lfittl\pg_query_go@v1.0.0\parser\include/c.h:1129,
                 from C:\Users\maxir\go\pkg\mod\github.com\lfittl\pg_query_go@v1.0.0\parser\include/postgres.h:47,
                 from contrib_pgcrypto_sha1.c:50:
C:\Users\maxir\go\pkg\mod\github.com\lfittl\pg_query_go@v1.0.0\parser\include/port.h:17:10: fatal error: netdb.h: No such file or directory
   17 | #include <netdb.h>
      |          ^~~~~~~~~
compilation terminated.

I really don't know what this is and what I'm talking about, so I tried a bare bone "googling" and a quick browsing led to similar issues in other codebases like here and here but you can find tons of them across multiple programming languages. From my understanding it all boils down to different dependencies under Windows and Unix.

Here is some info on the library.

The general agreement I found on the subject is to use a conditional including, under windows host the library to be used should be #include <winsock2.h> but further analysis is needed to pinpoint the fix.

Anyway the bug clearly isn't related specifically to github.com/lfittl/pg_query_go but rather to its external dependencies needs.


A temporary workaround I am using is to build my application in a linux docker container, not the best workflow but works.

PatrLind commented 1 year ago

I was just trying this out in a test project, but right now it seems like the include file that is missing is expected to be in this project. Is this because this is work in progress? The include file that is missing should be parser/include/port/win32_port.h and is included from here: https://github.com/pganalyze/pg_query_go/blob/main/parser/include/port.h#L24C29-L24C29

Compiler error on Widows:

# github.com/pganalyze/pg_query_go/v4/parser
In file included from ..\..\go\pkg\mod\github.com\pganalyze\pg_query_go\v4@v4.2.3\parser\include/c.h:1387,
                 from ..\..\go\pkg\mod\github.com\pganalyze\pg_query_go\v4@v4.2.3\parser\include/postgres.h:46,
                 from pg_query_internal.h:4,
                 from pg_query.c:2:
..\..\go\pkg\mod\github.com\pganalyze\pg_query_go\v4@v4.2.3\parser\include/port.h:24:10: fatal error: port/win32_port.h: No such file or directory
   24 | #include "port/win32_port.h"
      |          ^~~~~~~~~~~~~~~~~~~
compilation terminated.
lfittl commented 1 year ago

I was just trying this out in a test project, but right now it seems like the include file that is missing is expected to be in this project. Is this because this is work in progress? The include file that is missing should be parser/include/port/win32_port.h and is included from here: https://github.com/pganalyze/pg_query_go/blob/main/parser/include/port.h#L24C29-L24C29

Compiler error on Widows:

# github.com/pganalyze/pg_query_go/v4/parser
In file included from ..\..\go\pkg\mod\github.com\pganalyze\pg_query_go\v4@v4.2.3\parser\include/c.h:1387,
                 from ..\..\go\pkg\mod\github.com\pganalyze\pg_query_go\v4@v4.2.3\parser\include/postgres.h:46,
                 from pg_query_internal.h:4,
                 from pg_query.c:2:
..\..\go\pkg\mod\github.com\pganalyze\pg_query_go\v4@v4.2.3\parser\include/port.h:24:10: fatal error: port/win32_port.h: No such file or directory
   24 | #include "port/win32_port.h"
      |          ^~~~~~~~~~~~~~~~~~~
compilation terminated.

This is because Postgres itself supports Windows, but libpg_query (and pg_query_go by extent) does not today. Its definitely possible, but requires pulling in the right files (and a Windows machine to test with). The relevant file, in case that helps, is in https://github.com/postgres/postgres/blob/master/src/include/port/win32_port.h - but its likely making this work is more complicated than just fixing the import by adding the missing file.

See https://github.com/pganalyze/libpg_query/issues/44 - contributions welcome, but over the years nobody from the community has made the effort to try to fix this, and unfortunately neither I nor the other maintainers of the library run Windows ourselves, thus making this a bit hard to get fixed.

lfittl commented 11 months ago

Windows support has been added in #107 and released as v5.1.0.