_locale_parse_js_file cannot open SDC JavaScript files

Created on 1 May 2023, over 1 year ago
Updated 2 May 2023, over 1 year ago

Problem/Motivation

After clearing caches there is a warning about _locale_parse_js_file not being able to open the JS file for parsing. I am still unclear about the effects that this can have on localization of strings in JS files for SDC components.

Steps to reproduce

  1. Install a stock Umami demo site.
  2. Alter one of the templates to render a component that contains a JavaScript file.
  3. Clear caches.

An error message should appear. With the following stack trace.

Warning: file_get_contents(core/modules/sdc/../../../modules/contrib/sdc_examples/sdc_examples_replacements/components/my-button--primary/my-button--primary.js): Failed to open stream: No such file or directory in _locale_parse_js_file() (line 1096 of core/modules/locale/locale.module).

_locale_parse_js_file('core/modules/sdc/../../../modules/contrib/sdc_examples/sdc_examples_replacements/components/my-button--primary/my-button--primary.js') (Line: 529)
locale_js_translate(Array, Object) (Line: 485)
locale_js_alter(Array, Object, Object) (Line: 545)
Drupal\Core\Extension\ModuleHandler->alter('js', Array, Object, Object) (Line: 272)
Drupal\Core\Asset\AssetResolver->getJsAssets(Object, , Object) (Line: 58)
Drupal\cl_server\Asset\UnoptimizedAssetResolver->getJsAssets(Object, , Object) (Line: 325)
Drupal\Core\Render\HtmlResponseAttachmentsProcessor->processAssetLibraries(Object, Array) (Line: 164)
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: 196)
Symfony\Component\HttpKernel\HttpKernel->filterResponse(Object, Object, 1) (Line: 184)
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: 698)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Proposed resolution

We should ensure JavaScript files in Single Directory Components are findable by the locale module and properly fed into the locale module.

πŸ› Bug report
Status

Closed: works as designed

Version

10.1 ✨

Component
LocaleΒ  β†’

Last updated 5 days ago

Created by

e0ipso Can Picafort

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

Comments & Activities

  • Issue created by @e0ipso
  • πŸ‡ͺπŸ‡ΈSpain penyaskito Seville πŸ’ƒ, Spain πŸ‡ͺπŸ‡Έ, UTC+2 πŸ‡ͺπŸ‡Ί

    @e0ipso Can you attach a patch with the component?

  • e0ipso Can Picafort

    @penyaskito, you can get the component definitions:

    composer require drupal/sdc_examples
    drush pm:enable sdc_examples_replacements
    

    Then fetch the MR to use those components in your Umami homepage.

  • last update over 1 year ago
    29,359 pass, 6 fail
  • @e0ipso opened merge request.
  • Status changed to Needs work over 1 year ago
  • πŸ‡ͺπŸ‡ΈSpain penyaskito Seville πŸ’ƒ, Spain πŸ‡ͺπŸ‡Έ, UTC+2 πŸ‡ͺπŸ‡Ί

    What I did:

    git checkout -b '3357383-localeparsejsfile-cannot-open' --track drupal-3357383/'3357383-localeparsejsfile-cannot-open'
    ddev composer require drush/drush
    ddev drush si --yes demo_umami
    cd modules && git clone git@git.drupal.org:project/sdc_examples.git
    ddev drush en sdc sdc_examples --yes
    ddev drush uli
    

    Go to Spanish in the language switcher.
    Click compulsively the new button.

    What I see makes sense to me. No errors on console or watchdog.
    Wondering if this is because I used drush, or the problem might be in your env?

  • πŸ‡ͺπŸ‡ΈSpain penyaskito Seville πŸ’ƒ, Spain πŸ‡ͺπŸ‡Έ, UTC+2 πŸ‡ͺπŸ‡Ί

    Oh I see that was not the right module...

    ddev drush en sdc_examples_replacements

    Clear caches on "Performance" page.
    Go to homepage

    Now I see a different issue (404)

    https://drupal10.ddev.site/modules/sdc_examples/sdc_examples_replacements/components/my-button--primary/my-button--primary.js?ru008s net::ERR_ABORTED 404
    

    The file isn't in modules/sdc_examples/sdc_examples_replacements/components/my-button--primary/

  • πŸ‡ͺπŸ‡ΈSpain penyaskito Seville πŸ’ƒ, Spain πŸ‡ͺπŸ‡Έ, UTC+2 πŸ‡ͺπŸ‡Ί

    https://git.drupalcode.org/project/sdc_examples/-/blob/1.x/sdc_examples_...

    Defines the js. The js is included properly, but it's missing.

    edit modules/sdc_examples/sdc_examples_replacements/components/my-button--primary/my-button--primary.js
    See once() method call comment, but I'm not sure if that's expected or not. But the original my-button.js is there:

    (function (Drupal, once) {
      Drupal.behaviors.button = {
        attach: function attach(context) {
          let counter = 0;
          // The original my-button.js is still there! So we need a different once.
          const [button] = once('my-overridden-button', '.my-button', context);
          if (!button) {
            return;
          }
          button.addEventListener('click', function (event) {
            event.preventDefault();
            counter++;
            this.innerHTML = this.innerHTML.replace(/ \([0-9]*\)$/, '') + ' (' + counter + ') ' + Drupal.t('This is a new string in my component override');
          });
        },
      };
    })(Drupal, once);
    

    And I don't see any other errors

    Actually I can translate this properly, and works.

  • πŸ‡ͺπŸ‡ΈSpain penyaskito Seville πŸ’ƒ, Spain πŸ‡ͺπŸ‡Έ, UTC+2 πŸ‡ͺπŸ‡Ί

    So the expected behaviour is working when the js is there.

    If the js is missing as it is right now, I don't see any errors, but makes sense to have them when trying to load the js.

    If I delete an umami js defined in libraries, I see the same errors than I see with this missing file. So IMHO sdc is consistent with how core handles this, and this is a works as designed.

  • Status changed to Closed: works as designed over 1 year ago
  • e0ipso Can Picafort

    @penyaskito and I got together to investigate this. The crux of this is that the replacement component declares (in libraryOverrides) that it's providing a JS file that does not exist in the file system.

    Given that the behavior in this situation is the same as a missing JS file in a traditional library definition, I propose closing this as Works as Designed.

Production build 0.71.5 2024