Library wrong referenced if installed with composer

Created on 15 March 2024, 9 months ago
Updated 3 April 2024, 8 months ago

Problem/Motivation

If you configure composer to install the required klaro javascript library it will be installed into /libraries/klaro-js/dist/ whereas the library in klaro.libraries.yml points to /libraries/klaro/dist/

Steps to reproduce

Add follow snippet to the section repositories in your composer.json:

{
      "type": "package",
      "package": {
        "name": "klarohq/klaro-js",
        "version": "0.7.18",
        "type": "drupal-library",
        "dist": {
          "url": "https://github.com/KlaroHQ/klaro-js/archive/refs/tags/v0.7.18.zip",
          "type": "zip"
        }
      }
    }

then install the library with composer executing:

composer require "klarohq/klaro-js"

The library has been then installed in /libraries/klaro-js/ and if you visit admin/config/user-interface/klaro/apps you'll see the warning:

Warning: file_get_contents(libraries/klaro/dist/klaro-no-css.js): Failed to open stream: No such file or directory in _locale_parse_js_file() (line 1097 of core/modules/locale/locale.module).

_locale_parse_js_file('libraries/klaro/dist/klaro-no-css.js') (Line: 530)
locale_js_translate(Array, Object) (Line: 486)
locale_js_alter(Array, Object, Object) (Line: 545)
Drupal\Core\Extension\ModuleHandler->alter('js', Array, Object, Object) (Line: 285)
Drupal\Core\Asset\AssetResolver->getJsAssets(Object, , Object) (Line: 328)
Drupal\Core\Render\HtmlResponseAttachmentsProcessor->processAssetLibraries(Object, Array) (Line: 165)
Drupal\Core\Render\HtmlResponseAttachmentsProcessor->processAttachments(Object) (Line: 97)
Drupal\big_pipe\Render\BigPipeResponseAttachmentsProcessor->processAttachments(Object) (Line: 45)
Drupal\Core\EventSubscriber\HtmlResponseSubscriber->onRespond(Object, 'kernel.response', Object)
call_user_func(Array, Object, 'kernel.response', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.response') (Line: 214)
Symfony\Component\HttpKernel\HttpKernel->filterResponse(Object, Object, 1) (Line: 202)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 76)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 28)
Drupal\Core\StackMiddleware\ContentLength->handle(Object, 1, 1) (Line: 32)
Drupal\big_pipe\StackMiddleware\ContentLength->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 36)
Drupal\Core\StackMiddleware\AjaxPageState->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 704)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Proposed resolution

Change the reference in klaro.libraries.yml to point to /libraries/klaro-js/dist/

πŸ› Bug report
Status

Fixed

Version

3.0

Component

Code

Created by

πŸ‡¨πŸ‡­Switzerland juagarc4

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @juagarc4
  • πŸ‡¨πŸ‡­Switzerland juagarc4
  • πŸ‡¨πŸ‡­Switzerland juagarc4
  • πŸ‡¨πŸ‡­Switzerland juagarc4

    juagarc4 β†’ changed the visibility of the branch 3.x to hidden.

  • Status changed to Needs review 9 months ago
  • πŸ‡¨πŸ‡­Switzerland juagarc4
  • πŸ‡©πŸ‡ͺGermany sascha_meissner Planet earth

    Hey juagarc4,
    thank you so much for reporting and working on this issue!

    I looked into this and actually it looks like different repository than suggested in the readme of this module.
    This module is shipped with a composer.libraries.json which will contain the expected library

    e.g.:
    ```
    "kiprotect.klaro": {
    "type": "package",
    "package": {
    "name": "kiprotect/klaro",
    "type": "drupal-library",
    "version": "0.7.18",
    "dist": {
    "url": "https://github.com/kiprotect/klaro/archive/v0.7.18.zip",
    "type": "zip"
    }
    }
    }
    ```

    But it seems that klaro has changed itΒ΄s github structure and also the library name, still the shipped snippet above still is working.
    I will checkout how to continue on this, but if changing the library path would be the solution one would also have to think about all existent installations where the library will not be able to be loaded anymore because the path is wrong for them :/

  • πŸ‡¨πŸ‡­Switzerland juagarc4

    Hi sascha_meissner,

    Sorry, I didn't see the composer.libraries.json. But this explains now why the library was not downloaded as I installed the module the first time.

    I suppose the composer.libraries.json is being merged with composer.json by means of "wikimedia/composer-merge-plugin" but I can't see the corresponding "merge-plugin" configuration in composer.json.

    Since the Drupal core itself is not using this approach anymore since version 8.8 and Klaro supports Drupal 9+, does it make sense to continue using it here?
    https://www.drupal.org/node/3069730 β†’

    I would suggest to add the required library repository and its reference in the composer.json and remove the composer.libraries.json.
    Would it be possible?

    It will work for new installations and for updates. But I'm not sure how the old library can be removed from system. Will it be removed by the update of the new version of the module or would it require a manual action of the user?

  • Status changed to Needs work 9 months ago
  • πŸ‡¨πŸ‡­Switzerland juagarc4
  • πŸ‡©πŸ‡ͺGermany sascha_meissner Planet earth

    @juagarc4 Thank you for your input and your suggestions, iΒ΄d say it would make perfectly sense to adapt to the latest guide on "managing dependencies for a custom project" i wasnt up to date on this development, we just added the merge-plugin approach for convenience, if i remeber correctly we did so because webform plugin did it like this. And yes this requires to manually include the composer.libraries.json into your root composer.json TBH in our own projects we use a custom scaffold that will only put the two required dist/js and css files into libraries/klaro because we think its not the best practice to have the "whole library" inlcuding example pages and so on publicly within libraries folder when you just need the built files, this probably led to the decision not to force anyone in using a specific way on how to put the files into where they are expected. I will continue on this tomorrow and check with my collegues what they think!

  • πŸ‡¨πŸ‡­Switzerland juagarc4
  • πŸ‡¨πŸ‡­Switzerland juagarc4
  • πŸ‡¨πŸ‡­Switzerland juagarc4
  • πŸ‡¨πŸ‡­Switzerland juagarc4

    Hi sascha_meissner,

    Thanks for considering my proposal.

    I'm agree with you regarding "not to force anyone in using a specific way on how to put the files into where they are expected".
    For this reason you can use only a composer.json with the reference to the repository and then use a section "suggest" to inform the users about the required libraries to be installed. It's a less intrusive way to let them know that it's important and at the same time let them the freedom to install it however they want.

    I updated the MR with these changes. You can take a look and decide if it is an option for you.
    In this way the file composer.libraries.yml is only for BC reasons needed.

  • πŸ‡©πŸ‡ͺGermany sascha_meissner Planet earth

    Hey juagarc4,

    thanks for your update! i added hook_library_info_alter that grants compatibility for both installations, e.g it will work if installed in libraries/klaro or libraries/klaro-js, also added a watchdog message if none of them are found. + i updated the contents of composer.libraries.json with the new package so there is also backwards compatibility if anyone already included the composer.libraries.json in their root composer.json

    i accidently pushed (also your commits) to 3.x of origin, but iΒ΄d say its a happy accident because i wanted to keep your changes anyway ;)
    So i want to update the Readme a little bit when there is an opportunity and test a little bit and then it should be part of the next release :-)

  • πŸ‡¨πŸ‡­Switzerland juagarc4

    Hi sascha_meissner,

    don't worry. it's ok for me. I'm glad to have been helpful :-).

    I change then the status to "fixed" if you don't have anything against.

  • Status changed to Fixed 9 months ago
  • πŸ‡¨πŸ‡­Switzerland juagarc4
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024