Log an error message when library definitions have a license with no URL defined

Created on 10 May 2023, about 1 year ago
Updated 20 June 2024, 4 days ago

Problem/Motivation

If you have a library without a url key, you currently get the mentioned PHP warning.

That might not be a valid thing to do, but it happens, in our case it seems to be the colorbox module:

colorbox:
  remote: https://github.com/jackmoore/colorbox
  version: VERSION
  license:
    name: MIT
    gpl-compatible: false
  js:
    /libraries/colorbox/jquery.colorbox-min.js: { minified: true }
  dependencies:
    - core/drupal
    - core/drupalSettings
    - core/jquery
    - core/once

Steps to reproduce

Proposed resolution

Log a warning to encourage module developers to add a license URL.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component
Asset libraryΒ  β†’

Last updated 1 minute ago

No maintainer
Created by

πŸ‡¨πŸ‡­Switzerland Berdir Switzerland

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Issue created by @Berdir
  • πŸ‡¨πŸ‡­Switzerland Berdir Switzerland
  • Status changed to Needs review about 1 year ago
  • last update about 1 year ago
    29,383 pass
  • πŸ‡¬πŸ‡§United Kingdom catch

    Here it is with ?? 'no URL'. Can't really think of a better message or what else to do here, short of validating the library definitions and enforcing a URL, which would mean triggering a deprecation message for this case I guess.

  • Status changed to Needs work about 1 year ago
  • πŸ‡§πŸ‡ͺBelgium borisson_ Mechelen, πŸ‡§πŸ‡ͺ

    We should add a test that covers this case as well.

  • πŸ‡¨πŸ‡­Switzerland Berdir Switzerland
    +++ b/core/lib/Drupal/Core/Asset/JsCollectionOptimizerLazy.php
    @@ -180,7 +180,8 @@ public function optimizeGroup(array $group): string {
    -        $data .= "/* @license " . $js_asset['license']['name'] . " " . $js_asset['license']['url'] . " */\n";
    +        // It is possible to have a license name with no URL.
    +        $data .= "/* @license " . $js_asset['license']['name'] . " " . $js_asset['license']['url'] ?? 'no URL' . " */\n";
    

    that won't do what you expect, operator priority around concatenation and ternary is weird:

    https://3v4l.org/qaVSn

    I'm not sure it's worth adding a test for, I'm happy to define that license definitions have a url and that colorbox should be fixed and this is just a fallback/defensive programming. We'd need to add an invalid library definition in a module and then include that on a page.

  • πŸ‡¬πŸ‡§United Kingdom catch

    I'm happy to define that license definitions have a url and that colorbox should be fixed and this is just a fallback/defensive programming.

    I think if we say that, we shouldn't silently fix this, and instead log an error message with the library name in it.

  • πŸ‡¨πŸ‡­Switzerland Berdir Switzerland

    I would be OK with that.

  • πŸ‡¨πŸ‡­Switzerland Berdir Switzerland
  • πŸ‡¦πŸ‡ΉAustria agoradesign

    did the same for photoswipe: πŸ› Photoswipe library definition license is missing URL Fixed

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Just got bit by this on latest 10.1 deploy.

  • πŸ‡¬πŸ‡§United Kingdom catch

    Re-titling and updating the proposed resolution.

  • πŸ‡¨πŸ‡³China randy Tang

    I encountered this problem in 10.1.1, it was caused by the lack of url in the views_slideshow module. I found no license file in this module. In order to avoid the same problem with other modules, I added this patch to the core

  • 40:20
    36:25
    Running
  • πŸ‡¬πŸ‡§United Kingdom catch

    @Randy Tang that's similar to #3, but please see the discussion an issue summary - when a license is specified there should also be a URL, so instead we should log a more useful error. I think an assert() might be a better option here, then it would still be silent on production but caught a lot more quickly locally.

  • πŸ‡¦πŸ‡ΊAustralia pasan.gamage

    Hi,
    This same issue is there for core/lib/Drupal/Core/Asset/CssCollectionOptimizerLazy.php file L160

    Warning: Undefined array key "url" in Drupal\Core\Asset\CssCollectionOptimizerLazy->optimizeGroup() (line 160 of core/lib/Drupal/Core/Asset/CssCollectionOptimizerLazy.php).
    Drupal\Core\Asset\CssCollectionOptimizerLazy->optimizeGroup(Array) (Line: 183)
    Drupal\system\Controller\AssetControllerBase->deliver(Object, 'css_xZ1grJh6fKKy0PyENOxVLpswGrM9ycqavFyz8YvMrmw.css')
    call_user_func_array(Array, Array) (Line: 123)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 583)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 166)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 74)
    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: 106)
    Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
    Drupal\page_cache\StackMiddleware\PageCache->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: 51)
    Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 704)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
    
  • πŸ‡ΊπŸ‡ΈUnited States scotwith1t Birmingham, AL

    Same here. Getting the error from both CssCollectionOptimizerLazy and JsCollectionOptimizerLazy and the only module I can find that's missing a license URL is owlcarousel, which has both js and css in its library.

  • πŸ‡¬πŸ‡§United Kingdom catch

    @scotwith1t yes it's missing the license URL (https://git.drupalcode.org/project/owl-carousel/-/blob/8.x-2.x/owlcarous... for reference). Can you open an issue against owlcarousel? This issue won't get rid of the error, it will just make it more obvious which module is the culprit.

  • Warning: Undefined array key "url" in Drupal\Core\Asset\JsCollectionOptimizerLazy->optimizeGroup() (line 174 of core/lib/Drupal/Core/Asset/JsCollectionOptimizerLazy.php).

    Same issue here, it is intermittent throughout the site including in the admin panel whilst moving around that.

    Any Ideas on how to fix it ??

  • πŸ‡ΊπŸ‡ΈUnited States scotwith1t Birmingham, AL
  • πŸ‡ΊπŸ‡ΈUnited States Dave Reid Nebraska πŸ‡ΊπŸ‡Έ

    Question about this, we have some custom modules which add libraries that have a proprietary license only for the project and have no URL at all. Should I just provide an empty string? I want to avoid running into PHP notices.

  • πŸ‡¬πŸ‡§United Kingdom catch

    @Dave yes but it'll show up in aggregates, so make sure that's what you want.

    #2258313: Add license information to aggregated assets β†’

  • πŸ‡¦πŸ‡ΏAzerbaijan ActuallyAM

    I had the same problem but with CSS
    Warning: Undefined array key "url" in Drupal\Core\Asset\CssCollectionOptimizerLazy->optimizeGroup() (line 160 of /app/docroot/core/lib/Drupal/Core/Asset/CssCollectionOptimizerLazy.php)

    I made this css_optimizer_url.patch for css and now this warning is gone.
    I tested this with drupal 10.1.4

  • Update the patch above and fix the error for js optimization.

  • πŸ‡ΊπŸ‡ΈUnited States wylbur Minneapolis, Minnesota, USA

    We're getting this message when using the superfish module 8.x-1.7. In that module, the libraries info is provided by functions in the superfish.module file. The license info is provided within those functions, but that info does not seem to be passed to the functions in the JsCollectionOptimizerLazy.php file.

    I also agree that the errors do not provide helpful info to identify which library is missing the URL.

  • last update 9 months ago
    30,397 pass
  • πŸ‡¨πŸ‡¦Canada joelpittet Vancouver

    Regarding tests from #4 #3359497-4: Log an error message when library definitions have a license with no URL defined β†’ we have a library that has this in core:
    public/core/tests/Drupal/Tests/Core/Asset/library_test_files/licenses.libraries.yml:63
    https://git.drupalcode.org/project/drupal/-/blob/11.x/core/tests/Drupal/...

    And this tests it should be returning without a URL.
    \Drupal\Tests\Core\Asset\LibraryDiscoveryParserTest::testLibraryWithLicenses
    https://git.drupalcode.org/project/drupal/-/blob/11.x/core/tests/Drupal/...

    I guess we'd just need to try to render the result of that parser to get it to fail?

  • πŸ‡©πŸ‡ͺGermany diqidoq Berlin | Hamburg | New York | London | Paris

    To clarify for followers: patches in #3 and #13 just target js libraries, #22 css only and #23 targets both. All use the same workaround.

    But note: This is ia work-around. Follow the discussion to understand why we need to track missing urls for library licenses and that we just need another way to log it (without frontpage warnings). As long as you get warnings and notices on frontpages you need to surpress this patches are useful (also in production) but ONLY if you are sure that you do NOT need the license url in your case.

  • πŸ‡ͺπŸ‡ΈSpain jaramoshu

    In my case it throws:
    Warning: Undefined array key "url" in Drupal\Core\Asset\JsCollectionOptimizerLazy->optimizeGroup() (line 174 of /var/www/html/web/core/lib/Drupal/Core/Asset/JsCollectionOptimizerLazy.php).

    This is the undefined "url" key. To avoid this situation I check with isset().

  • πŸ‡¨πŸ‡¦Canada joseph.olstad

    There's far too many errors in contrib that result in annoying warnings.

    I chased down fontawesome, created a patch for it, but when I log into my site, I notice other warnings from other modules. Far too many to keep track of.

    So I'm going to use patch#23.

    Patch #23 uses the PHP 8.1 ?? syntax,
    this is appropriate and good for Drupal 10+

  • last update about 1 month ago
    29,689 pass
  • πŸ‡ͺπŸ‡¨Ecuador cacrody
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Environment: PHP 8.1 & MariaDB 10.3.22
    last update 4 days ago
    29,689 pass
  • last update 4 days ago
    29,689 pass
  • 5:56
    0:40
    Running
  • πŸ‡ΊπŸ‡ΈUnited States gtucker6

    I went ahead and applied the last patch that cacrody@gmail.com posted (#30) with success. I think we need to find an overall solution to this compatibility issue, as this is a very uncomfortable error that isn't site breaking. How about we add a warning status message and/or a warning log message instead of a php warning error? This will make the warning comfortably yellow while also notifying developers that a change needs to be made. It might also be best to limit this error to certain pages (i.e. status report and available updates). If I have time, I will post a patch including the above changes. I hope we get this resolved, as it is very distracting while developing.

Production build 0.69.0 2024