JuliaInterop / Clang.jl

C binding generator and Julia interface to libclang
https://juliainterop.github.io/Clang.jl/
MIT License
217 stars 68 forks source link

In get_system_shard_key() BoundsError: attempt to access 2-element Vector{String} at index [] #450

Closed emmt closed 3 months ago

emmt commented 9 months ago

With Julia-1.6.7 and Clang-0.14.1 on Linux Ubuntu 23.04, calling get_default_args() gives the following error :

ERROR: BoundsError: attempt to access 2-element Vector{String} at index []
Stacktrace:
  [1] throw_boundserror(A::Vector{String}, I::Tuple{})
    @ Base ./abstractarray.jl:651
  [2] checkbounds
    @ ./abstractarray.jl:616 [inlined]
  [3] _getindex
    @ ./abstractarray.jl:1196 [inlined]
  [4] getindex
    @ ./abstractarray.jl:1170 [inlined]
  [5] get_system_shard_key(triple::String)
    @ Clang.JLLEnvs ~/.julia/packages/Clang/x6vao/src/shards/JLLEnvs.jl:96
  [6] get_environment_info(triple::String, version::VersionNumber)
    @ Clang.JLLEnvs ~/.julia/packages/Clang/x6vao/src/shards/JLLEnvs.jl:102
  [7] get_system_dirs(triple::String, version::VersionNumber)
    @ Clang.JLLEnvs ~/.julia/packages/Clang/x6vao/src/shards/JLLEnvs.jl:114
  [8] get_system_dirs
    @ ~/.julia/packages/Clang/x6vao/src/shards/JLLEnvs.jl:113 [inlined]
  [9] get_default_args(triple::String) (repeats 2 times)
    @ Clang.Generators ~/.julia/packages/Clang/x6vao/src/generator/context.jl:233
 [10] top-level scope
    @ REPL[14]:1

This is due to variable platform_keys in get_system_shard_key() having more than one element and thus platform_keys[] fails with the above error.

The code of get_system_shard_key() has not changed since Clang-0.14.0 (if I am correctly using git blame). I would suggest to fix this by using expression:

return first(platform_keys)

instead of:

return platform_keys[]

in function get_system_shard_key in file src/shards/JLLEnvs.jl. But I am not sure that this would be correct...

It would be nice to retroproagate the fix to older versions of Clang, at least to the 0.14 branch, to be able to use Clang with Julia-1.6.

Gnimuc commented 9 months ago

does Clang.jl work properly after fixing the issue?

Gnimuc commented 9 months ago

I'm a bit worried about the compatibility margin between Julia-1.6 and Ygg-JLL-BB2.

emmt commented 9 months ago

I did not tried the proposed fix because I was not sure of the purpose of get_system_shard_key. In particular, if it is supposed to fail when platform_keys is not an array with exactly one element, then my fix breaks this assumption. The other concern is: I choose the first element of platform_keys because I assumed that it is as good as any other. But is this true?

Gnimuc commented 9 months ago

It's the system shard key in the toml file: https://github.com/JuliaInterop/Clang.jl/blob/master/Artifacts.toml

I think this fix is ok: first(platform_keys)

but I'm not sure whether there are other compatibility issues. (your system Linux Ubuntu 23.04 is new but the gcc shipped with the system is probably missing in Ygg)

emmt commented 9 months ago

I have just tried the proposed fix. This works for Julia 1.6 with Clang 0.14.1

I can make a PR but I do not know how to propagate to other versions.

emmt commented 9 months ago

PR https://github.com/JuliaInterop/Clang.jl/pull/451 attempts to fix this.

Gnimuc commented 3 months ago

I just noticed the root cause of the problem is that the Artifacts.toml file is somehow broken, but the logic used in get_system_shard_key assumes there should be only one matched entry...