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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

sadashiv’s picture

Title: Re-Attempt to detect library in library_load() function. » Re-Attempt to detect library in libraries_load() function.
Status: Active » Needs review
FileSize
409 bytes

I 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.

tstoeckler’s picture

Title: Re-Attempt to detect library in libraries_load() function. » Library definitions of uninstalled libraries are cached
Category: Feature request » Bug report

Yes, 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.

sadashiv’s picture

Hi,

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.

tstoeckler’s picture

Status: Needs review » Needs work

Looking 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.

sadashiv’s picture

Status: Needs work » Needs review

Hi,

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.

tstoeckler’s picture

Priority: Normal » Major
FileSize
794 bytes

So 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 that libraries_load() would end up being called on every request. That's why I would like to avoid calling libraries_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.

  • tstoeckler committed d78ad5b on 7.x-2.x
    Issue #2341955 by sadashiv, tstoeckler: Clear library cache on library...
tstoeckler’s picture

Status: Needs review » Fixed

OK, 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.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

abratko’s picture

Hi. why patсh #1 was not committed?