Problem/Motivation

An entry in *.libraries.yml must contain either css, js, or drupalSettings to be considered valid.

However, a library that only contains other libraries as dependencies is still useful.

Proposed resolution

Do not throw an exception when a library contains only other dependencies.

Remaining tasks

N/A

User interface changes

N/A

API changes

Not really? Adjusts the definition of IncompleteLibraryDefinitionException

Data model changes

N/A

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tim.plunkett created an issue. See original summary.

tim.plunkett’s picture

The last submitted patch, 2: 2905429-libraries-2-FAIL.patch, failed testing. View results

Wim Leers’s picture

+1! I think this makes sense. We should also get @nod_'s thoughts though. I think we've considered this before at some point.

+++ b/core/tests/Drupal/Tests/Core/Asset/library_test_files/example_module_only_dependencies.libraries.yml
@@ -0,0 +1,2 @@
+example:
+  dependencies: {  }

Well but this asset library does seem rather nonsensical, doesn't it? Shouldn't it at least have some dependency?

tstoeckler’s picture

Can you provide an example of why this would be useful?

tim.plunkett’s picture

#4
Yes, well a definition of:

example:
  js: {  }

is also valid. I followed that.

To fix all of them would be a BC break, but it'd be a switch from !isset to empty.

#5
In this case, there are multiple places I needed to add both outside_in and off_canvas libraries as #attached.
And then they switched s/outside_in/settings_tray, and I realized how many places that was.
And in the future, off_canvas will move from settings tray to core/misc, and I'd need to update again.
The idea was to make a single custom "library" that wrapped those two, making only one place that would need to be updated in the future.

Jacine’s picture

+1. I ran into this today. Use case is:

- I have a library definition for each layout, e.g. one for a 50% / 50%, another for 75% 25%, etc.
- I also have a library definition that loads ALL of them, instead of having to do attach_library() 20 times if you need them all (you might).

layouts.all:
  dependencies:
    - layout_1
    - layout_2
    - layout_3
    - etc...

Results in one {{ attach_library('lala/layouts.all') }}, being possible vs...

{{ attach_library('lala/layouts.layout_1') }}
{{ attach_library('lala/layouts.layout_2') }}
{{ attach_library('lala/layouts.layout_3') }}, etc.

Thanks!

Wim Leers’s picture

Status: Needs review » Reviewed & tested by the community
catch’s picture

Does this need a change record?

xjm’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record

Yeah, a small change record would be good. Otherwise this looks fine to me.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

chrisolof’s picture

This would be useful. For now adding the js: { } does prevent the exception.

My use case: Packaging up a collection of libraries into a single, meta library is a code and complexity reduction in my codebase. It doesn't necessarily need to add any files or settings of its own to be useful.

zrpnr’s picture

Status: Needs work » Needs review
FileSize
2.61 KB

I came across this issue when we were working on #2926155: Follow-up to #2809427: update from jQuery UI 1.11.4 to 1.12.1 forgot to list some new JS files in *.libraries.yml
One idea in that issue for de-tangling the dependencies of jQuery UI was to pull each dependent file out to its own asset library which could be selectively required by the other jQuery UI asset libraries. A single "collection" of these other smaller libraries would have been a convenient solution for maintaining BC while the actual files were shuffled around.

It looks like this issue just needed a change record and a re-roll for the patch (although it was still green). I ran the LibraryDiscoveryParserTest and tested this manually by creating a jQuery UI asset library that only depended on another new asset library.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

zrpnr’s picture

Another use case for this:
in 3067251 # 49
@xjm discussed an approach for a contrib module that would be just a dependency.

we could retain the contrib module, but just have it declare a dependency on the core library

A contrib module could act as a placeholder for a soon-to-be-deprecated core library.
Modules could depend on a contrib asset library which in turn depends on a core asset library.
Once that core asset library is removed, the contrib library could provide the files instead, and the modules' dependency would be unchanged.

Wim Leers’s picture

Issue tags: -Needs change record
+++ b/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryParserTest.php
@@ -171,6 +171,25 @@ public function testInvalidLibrariesFile() {
+   * Tests that an exception is thrown when no CSS/JS/setting is specified.

Shouldn't this be Tests that no exception is thrown 🤔

zrpnr’s picture

Shouldn't this be "Tests that no exception is thrown"

That's correct, the other tests in this suite all expectException while this new test checks that no exception is thrown.
I also changed the wording of the testBuildByExtensionWithMissingInformation comment since it didn't mention dependencies in the missing info it's checking.

Wim Leers’s picture

Status: Needs review » Reviewed & tested by the community

Perfect :)

Wim Leers’s picture

Note this is perfectly cherry-pickable to 8.8.x — no disruption.

  • catch committed a80a479 on 9.0.x
    Issue #2905429 by zrpnr, tim.plunkett, Wim Leers, Jacine: Library...

  • catch committed 6022129 on 8.9.x
    Issue #2905429 by zrpnr, tim.plunkett, Wim Leers, Jacine: Library...

  • catch committed e8f1dc8 on 8.8.x
    Issue #2905429 by zrpnr, tim.plunkett, Wim Leers, Jacine: Library...
catch’s picture

Version: 8.9.x-dev » 8.8.x-dev
Status: Reviewed & tested by the community » Fixed

Committed a80a479 and pushed to 9.0.x then cherry-picked back to 8.9.x and 8.8.x. Thanks!

Status: Fixed » Closed (fixed)

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

kim.pepper’s picture

Libraries may contain only other libraries as dependencies

I was a bit confused by the title of this change record. At first glance it read that libraries may contain only other libraries, which didn't make sense. Can I suggest:

Libraries may contain other libraries as dependencies