yglukhov / nim-jwt

JWT implementation for nim-lang
MIT License
51 stars 11 forks source link

conflicting types for 'br_hmac_size' #20

Closed krydos closed 1 year ago

krydos commented 1 year ago

hi @yglukhov, huge thanks for the library.

I just took an example from the readme file and tried to compile the project. It errored out with this message:

error > /Users/krydos/.cache/nim/chat_test_d/@m..@s..@s..@s.nimble@spkgs@sjwt-0.2@sjwt@sprivate@scrypto.nim.c:244:13: error: conflicting types for 'br_hmac_size' N_CDECL(NU, br_hmac_size)(br_hmac_context* ctx); ^ /Users/krydos/.nimble/pkgs/bearssl-0.2.0/bearssl/abi/../csources/inc/bearssl_hmac.h:155:1: note: previous definition is here br_hmac_size(br_hmac_context *ctx) ^ 1 error generated. Error: execution of an external compiler program 'clang -c -w -ferror-limit=3 -I/Users/krydos/.nimble/pkgs/bearssl-0.2.0/bearssl/abi/../csources/src/ -I/Users/krydos/.nimble/pkgs/bearssl-0.2.0/bearssl/abi/../csources/inc/ -I/Users/krydos/.nimble/pkgs/bearssl-0.2.0/bearssl/abi/../csources/tools/ -DBR_USE_UNIX_TIME=1 -DBR_USE_URANDOM=1 -DBR_LE_UNALIGNED=1 -DBR_64=1 -DBR_INT128=1 -I/Users/krydos/.nimble/pkgs/bearssl_pkey_decoder-0.1.0/bearssl_pkey_decoder/csources -I/Users/krydos/.choosenim/toolchains/nim-1.6.10/lib -I/Users/krydos/Projects/chat_test/src -o /Users/krydos/.cache/nim/chat_test_d/@m..@s..@s..@s.nimble@spkgs@sjwt-0.2@sjwt@sprivate@scrypto.nim.c.o /Users/krydos/.cache/nim/chat_test_d/@m..@s..@s..@s.nimble@spkgs@sjwt-0.2@sjwt@sprivate@scrypto.nim.c' failed with exit code: 1

I'm not quite sure what's going on because this JWT lib uses bearssl so whatever br_hmac_size definition is there it should be the only one, right? Do you have any ideas how it is possible that I have a conflict there? Is it something with my configuration maybe?

My nim version is 1.6.10 OS: MacOS

here is list of requires from my project's nimble file in case it can help:

requires "nim >= 1.6.10" requires "jester" requires "ws" requires "jwt"

yglukhov commented 1 year ago

Please try reinstalling jwt along with its dependencies and tell me if it helps.

rm -rf ~/.nimble/pkgs/jwt* ~/.nimble/pkgs/bearssl*
nimble install jwt
krydos commented 1 year ago

thanks for quick answer. Nope. Unfortunately the error is exactly the same.

I expect you don't have it, right? It is something my machine specific then. I'll try to see if I can find some minimal test case to reproduce it. Let you know

krydos commented 1 year ago

hm, actually the minimal test I can make is this (pretty much readme example):

nim_jwt_test.nim ```nim import jwt, times, json proc main(): void = var token = toJWT(%*{ "header": { "alg": "HS256", "typ": "JWT" }, "claims": { "userId": "testuserid", "exp": (getTime() + 1.days).toUnix() } }) token.sign("secretcode") echo $token when isMainModule: main() ```
nim_jwt_test.nimble (dependecies section) ``` # Dependencies requires "nim >= 1.6.10" requires "jwt" ```

What I also did is I removed everything from my ~/.nimble directory to make sure no other packages somehow redefine that br_hmac_size function. Still, got the same error.

EDIT: So I cloned the repo and tried to run nimble test and I got the same error. Removed ~/.nimble directory and the error is still there. Removed ~/.nimble and ~/.cache/nim dirs - still the same error.

Then I installed completely fresh ubuntu on my VM, compiled 1.6.10 there, cloned the jwt lib and ran nimble test. NO ERRORS. Have no clue what does it mean but as additional info I hope it's something.

krydos commented 1 year ago

Alright, sorry for the spam. One more finding.

If I replace definition of hmacSize* in ~/.nimble/pkgs/bearssl-0.2.0/bearssl/abi/bearssl_hmac.nim:40 with

proc hmacSize*(ctx: var HmacContext): uint {.inline, importcFunc, importc: "br_hmac_size", nodecl.} =
  return ctx.outLen

then everything compiles with no issues.

Note that nodecl pragma added by me. I don't really understand all this nim compilation magic at this moment but it seems like it instructs the compiler to not generate a function definition.

I still don't understand why this issue exists on my Mac laptop but not inside the linux vm on the same Mac laptop. Do you have any ideas about it?

Do you think it make sense to pass this issue to bearssl-nim team?

yglukhov commented 1 year ago

Ah indeed, the hmacSize definition looks wrong, it just doesn't need importc at all, only inline:

proc hmacSize*(ctx: var HmacContext): uint {.inline.} =
  return ctx.outLen

Please see if that works for you. I'm still not sure why it works on linux :/

krydos commented 1 year ago

Works perfectly with just inline. I suppose I can close the issue then since there is nothing broken in jwt lib.

Thanks a lot!

EDIT: still would be nice to know why it does work in some cases. I thought it's because gcc (ubuntu) vs clang (macos) thing but I can't confirm really. I installed clang on ubuntu and nimble --cc=clang test in the jwt lib root works with no issues.