Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hi,
I saw that libraries_load() function doesn't load the library sometimes even if libraries_detect() function shows it installed.
I faced this problem while using different modules using libraries api. Status report shows library installed and shows the version but the code throws error that library can't be loaded. Flushing all cache fixed this problem.
Comment | File | Size | Author |
---|---|---|---|
#6 | 2341955-6-libraries-cache-clear-ui.patch | 794 bytes | tstoeckler |
#1 | detect_library_in_libraries_load-2341955.patch | 409 bytes | sadashiv |
Comments
Comment #1
sadashiv CreditAttribution: sadashiv commentedI checked libraries_load function and found that it loads library from cache and returns it. Problem was that when I was enabling the module it was clearing cache and loading the cache with installed as False. After extracting the library libraries_detect shows it installed but libraries_load returns the same old instance retrieved from cache which has installed = False so it doesn't load the library.
I am attaching a patch which can solve this problem. The patch also checks whether the cached copy had the library installed and if the library is not installed it tries to detect it again.
Hth,
Sadashiv.
Comment #2
tstoecklerYes, this is a valid concern. The fix looks correct. Could use a code comment, though. I'm not sure how trivial this is, but it would be awesome to provide some tests for this as well.
Comment #3
sadashiv CreditAttribution: sadashiv commentedHi,
It is a case where we install a module which needs the library during runtime so it does allow us to enable the module. After enabling the module if we paste the library in the libraries folder then it doesn't load the library until we clear the cache as library is always served from the cached copy. What I did in the patch is I checked whether the library is installed or not. If it is not installed then I am trying to detect it again and so it would load the library.
Writing test class is not simple as, first the library should not be installed and should get installed after enabling the module, so would request to move this fix so that other modules using libraries don't need to clear cache after copying the library.
Thanks,
Sadashiv.
Comment #4
tstoecklerLooking at this now, it seems we should instead be performing a check when *setting* the cache, not when retrieving it. Otherwise we have bogus cache entries. With the patch we ignore those, but they are still around, which is fairly weird.
Comment #5
sadashiv CreditAttribution: sadashiv commentedHi,
I was checking this while retrieving from cache because when we follow a sequence as
1) Download the module which requires library during runtime and enable it.
2) Status report shows library missing so the developer extracts the library in appropriate location and check status report again
3) The library shows up as installed if the developer followed the documentation and called libraries_detect() to detect the library, but when we run the code which needs library files and we call libraries_load() it doesn't load the library
Reason why this happens:
When we call libraries_detect to detect the library it checks whether the library is physically in place with nothing loaded from cache so it shows it's installed and ready to be used.
When we call libraries load it loads the cached entry of the library which has installed set to false is returned.
Solution is to refresh the cache when libraries_detect is called or to check if the library is installed in libraries_load
Thanks,
Sadashiv.
Comment #6
tstoecklerSo I still think that checking when setting the cache would support your use-case as well and it does not leave around stale cache entries.
I thought about this some more and I still think this is problematic for optional libraries. I.e. consider the Administration menu module, which at some point supported the jQuery hoverintent library (not sure if it still does). Using the
#attached
system it could simply attach the library so thatlibraries_load()
would end up being called on every request. That's why I would like to avoid callinglibraries_detect()
on every request.We recently introduced an overview of the installed libraries with specific instructions on how to install each library in #819610: Provide a UI for Libraries. So here's a patch with adds a cache clearing to the library installation form. That way, once it's shown as installed, it can also be loaded.
Can you try whether the attached patch works for you? Just go to /admin/reports/libraries and see if the cache is cleared at the right time, when you are on the library-specific page.
Comment #8
tstoecklerOK, rerolled and committed this patch. In fact I changed it to not clear the cache for each library one-by-one, but just clear the entire cache once.
Comment #10
abratko CreditAttribution: abratko commentedHi. why patсh #1 was not committed?