Skip to content

Conversation

victorgveloso
Copy link

@victorgveloso victorgveloso commented Oct 14, 2025

FIX #81
The problem in ChainedLibraryLookup.get comes from the fact findLibrary is invoked eagerly, thus leading to UnsatisfiedLinkError even when SymbolLookup never requires using the SymbolLookup returned by findLibrary. Actually, the solution is quite simple and it's inspired by the initialization of the lookup local variable.

It leverages the fact SymbolLookup is a FunctionalInterface to implement it in Lazily manner. All you need to do is replace the following line:

return lookup.or(findLibrary(arena)).or(Linker.nativeLinker().defaultLookup());

With the following line

return lookup.or((name) -> findLibrary(arena).find(name)).or(Linker.nativeLinker().defaultLookup());

Notice that now findLibrary will only execute if all SymbolLookup loaded by ServiceLoader failed to lookup the library.

…loaded SymbolLookup successfuly found library
@victorgveloso
Copy link
Author

victorgveloso commented Oct 14, 2025

This PR only applies a simple trick to fix custom library loading when the library is not in java.library.path but a service loaded SymbolLookup successfully found it.

@victorgveloso
Copy link
Author

Please, @ObserverOfTime , consider accepting this bugfix otherwise jtreesitter service loader in LibraryLoader is useless (as ChainedLibraryLoader throws exceptions when the native library is not found in java.library.path even before trying to run any custom LibraryLoader)

@ObserverOfTime
Copy link
Member

I'll incorporate it in #128.

@victorgveloso victorgveloso changed the base branch from master to improve-exceptions October 17, 2025 13:25
@victorgveloso
Copy link
Author

I changed back the base branch to improve-exceptions so that you can easily integrate it while giving the proper credits for my contribution. You can accept this PR now and it won't affect the master branch

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

Successfully merging this pull request may close these issues.

Should usage of SymbolLookup.loaderLookup() be opt-in?

2 participants