magento2: Can't compile style for custom theme which inherits from another custom theme

Preconditions and environment

  • 2.4.4
  • OS: Ubuntu 20.04.2 LTS
  • Magento is in developer mode
  • Grunt installed

Code:

issue_35684.zip (extract to root dir of the magento project)

Steps to reproduce

Overview

  1. Create Theme Foo (inheriting from Magento/luma)
  2. Create Theme Bar (inheriting from Foo)
  3. Create Module Company_Test
  4. Setup Upgrade
  5. Grunt configuration
  6. Grunt Refresh

1. Create Theme Foo (inheriting from Magento/luma)

1.1 app/design/frontend/company/foo/registration.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

use \Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::THEME, 'frontend/company/foo', __DIR__);

1.2 app/design/frontend/company/foo/theme.xml

<theme xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/theme.xsd">
    <title>Foo</title>
    <parent>Magento/luma</parent>
</theme>

1.3 app/design/frontend/company/foo/web/css/source/_extend.less

@import '_mixin.less';

1.4 app/design/frontend/company/foo/web/css/source/_mixin.less

.h1 {
    color: red;
    font-size: 24px;
}

2. Create Theme Bar (inheriting from Foo)

2.1 app/design/frontend/company/bar/registration.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

use \Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::THEME, 'frontend/company/bar', __DIR__);

2.2 app/design/frontend/company/bar/theme.xml

<theme xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/theme.xsd">
    <title>Bar</title>
    <parent>company/foo</parent>
</theme>

2.3 app/design/frontend/company/bar/web/css/source/_extend.less

@import '_text.less';

2.4 app/design/frontend/company/bar/web/css/source/_text.less

.black { color: black; }

3. Create Module Company_Test

3.1 app/code/Company/Test/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Company_Test',
    __DIR__
);

3.2 app/code/Company/Test/etc/module.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
	<module name="Company_Test" setup_version="1.0.0"/>
</config>

3.3 app/code/Company/Test/view/frontend/web/css/source/_module.less

body.contact-index-index {
    .page-title { .h1(); }
}

3.4 Enable Module “Company_Test”

php bin/magento module:enable Company_Test

4. Setup Upgrade

php bin/magento setup:upgrade

5. Grunt Configuration

dev/tools/grunt/configs/local-themes.js

/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

 'use strict';

 /**
  * Define Themes
  *
  * area: area, one of (frontend|adminhtml|doc),
  * name: theme name in format Vendor/theme-name,
  * locale: locale,
  * files: [
  * 'css/styles-m',
  * 'css/styles-l'
  * ],
  * dsl: dynamic stylesheet language (less|sass)
  *
  */
 module.exports = {
     company_foo: {
         area: 'frontend',
         name: 'company/foo',
         locale: 'de_DE',
         files: [
             'css/styles-m',
             'css/styles-l'
         ],
         dsl: 'less'
     },
     company_bar: {
         area: 'frontend',
         name: 'company/bar',
         locale: 'de_DE',
         files: [
             'css/styles-m',
             'css/styles-l'
         ],
         dsl: 'less'
     },
 };

6. Grunt Refresh

Run grunt refresh

Expected result

It should compile without an error.

Actual result

-> .h1 is undefined

user@xx-VirtualBox:/var/www/company/fresh_m244$ grunt refresh
Running "refresh" task

Running "clean:var" (clean) task
>> 1433 paths cleaned.

Running "clean:pub" (clean) task
>> 1194 paths cleaned.

Running "clean:styles" (clean) task
>> 0 paths cleaned.

Running "clean:markup" (clean) task
>> 0 paths cleaned.

Running "clean:js" (clean) task
>> 0 paths cleaned.

Running "clean:generation" (clean) task
>> 1732 paths cleaned.

Running "clean:company_foo" (clean) task
>> 0 paths cleaned.

Running "clean:company_bar" (clean) task
>> 0 paths cleaned.

Running "exec:all" (exec) task
grunt --force clean:company_foo
Processed Area: frontend, Locale: de_DE, Theme: company/foo, File type: less.
-> css/styles-m.less
-> css/styles-l.less
Successfully processed.
Running "clean:company_bar" (clean) task
>> 49 paths cleaned.

Done.


Execution Time (2022-07-22 11:58:46 UTC+2)
loading tasks      92ms  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 68%
clean:company_bar  42ms  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 31%
Total 135ms

Processed Area: frontend, Locale: de_DE, Theme: company/bar, File type: less.
-> css/styles-m.less
-> css/styles-l.less
Successfully processed.

Running "less:company_foo" (less) task
>> 2 stylesheets created.
>> 2 sourcemaps created.

Running "less:company_bar" (less) task
>> pub/static/frontend/company/bar/de_DE/Company_Test/css/source/_module.less: [L2:C18] .h1 is undefined
Warning: Error compiling pub/static/frontend/company/bar/de_DE/css/styles-m.less Use --force to continue.

Aborted due to warnings.


Execution Time (2022-07-22 11:58:33 UTC+2)
clean:var         305ms  ▇▇ 1%
clean:pub         382ms  ▇▇ 1%
clean:generation  324ms  ▇▇ 1%
exec:all          21.8s  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 80%
less:company_foo   3.8s  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 14%
less:company_bar  478ms  ▇▇▇ 2%
Total 27.4s

Additional information

If I skip 2.3 and 2.4 then compilation works.

Release note

No response

Triage and priority

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 20 (10 by maintainers)

Most upvoted comments

@Eddcapone,

Thanks for sharing more information. we have tried to reproduce this issue on 2.4-develop branch and now it is reproducible and below are the steps which i have follow #35684

my code zip file. Issue35684.zip(extract to root dir of the magento project)

  1. Create Theme Foo (inheriting from Magento/luma)
  2. Create Theme Bar (inheriting from Foo)
  3. Create Module Company_Test
  4. Enable Module
  5. Setup Upgrade
  6. Grunt configuration
  7. Grunt Refresh

Please find the attached screen for your reference.

image

Hence, the issue reported is reproducible and confirmed.

Thanks

Can you share sample of your ? @Eddcapone

In your specific case you can import mixins in your module like this

_module.less

@import (reference) '_mixins';
// Other styles

_mixins you can use syntax like https://github.com/magento/magento2/blob/2.4-develop/app/design/frontend/Magento/blank/web/css/source/_extends.less

@abs-yourmixins-name: {
    //Any code less/css here
};

.h1() {
 // Any code less/css here
}

Just curious why not to use in theme scope/area in app/design

Because it is only needed for the extension. If the extension is not active e.g. because it is only enabled for a certain store view, then the style is not needed. If I would put it in the theme, then it would load all the time.