Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using virtual type to configure plugin, interceptor method cannot be generated correctly in setup:di:compile command #33980

Open
1 of 5 tasks
GrassH opened this issue Sep 3, 2021 · 6 comments · May be fixed by #38141
Open
1 of 5 tasks
Assignees
Labels
Area: Other Developer Tools Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Priority: P3 May be fixed according to the position in the backlog. Progress: PR in progress Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Severity: S1 Affects critical data or functionality and forces users to employ a workaround. Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it

Comments

@GrassH
Copy link

GrassH commented Sep 3, 2021

Preconditions (*)

  1. Magento CE 2.4.2 and above (Only tested these versions)

Steps to reproduce (*)

  1. Create a Magento 2 Module - Vendor_Module (Sample code can be found here)
  2. Configure a plugin:
<?xml version="1.0"?>
<!--
/**
 * @package Vendor_Module
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <virtualType name="Vendor\Module\Plugin\Sub" type="Vendor\Module\Plugin\Overload">
        <arguments>
            <argument name="type" xsi:type="string">-</argument>
        </arguments>
    </virtualType>
    <type name="Vendor\Module\Model\Calculator">
        <plugin name="Vendor_Module::change_to_sub" type="Vendor\Module\Plugin\Sub" />
    </type>
    <type name="Magento\Framework\Console\CommandList">
        <arguments>
            <argument name="commands" xsi:type="array">
                <item name="test_plugin" xsi:type="object">Vendor\Module\Console\Command\TestCommand</item>
            </argument>
        </arguments>
    </type>
</config>

Vendor\Module\Plugin\Overload:

<?php
/**
 *
 * @package Vendor_Module
 */

namespace Vendor\Module\Plugin;

use Vendor\Module\Model\Calculator;

/* For testing */
class Overload
{
    /**
     * @var string
     */
    protected $type;

    /**
     * @param string $type
     */
    public function __construct($type = '+')
    {
        $this->type = $type;
    }

    /**
     * Modified to multiplication
     *
     * @param  Calculator $subject
     * @param  callable   $procced
     * @param  int|float  $left
     * @param  int|float  $right
     * @return int|float
     */
    public function aroundCalculate(
        Calculator $subject,
        callable $proceed,
        $left,
        $right
    ) {
        $result = null;
        switch ($this->type) {
            case '+':
                $result = $proceed($left, $right);
                break;

            case '-':
                $result = $left - $right;
                break;

            case '*':
                $result = $left * $right;
                break;

            case '/':
                $result = $left / $right;
                break;

            case '%':
                $result = $left % $right;
                break;
        }

        return $result;
    }
}

Vendor\Module\Model\Calculator:

<?php
/**
 *
 * @package Vendor_Module
 */

namespace Vendor\Module\Model;

/* For testing */
class Calculator
{
    /**
     * @param  int|float $left
     * @param  int|float $right
     * @return int|float
     */
    public function calculate($left, $right)
    {
        return $left + $right;
    }
}

Vendor\Module\Console\Command\TestCommand:

<?php
/**
 *
 * @package Vendor_Module
 */

namespace Vendor\Module\Console\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Vendor\Module\Model\Calculator;

class TestCommand extends Command
{
    /**
     * @var Calculator
     */
    protected $calculator;

    /**
     * {@inheritdoc}
     */
    public function __construct(Calculator $calculator, string $name = null)
    {
        $this->calculator = $calculator;
        parent::__construct($name);
    }

    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this->setName('test:plugin:calculate')
            ->setDescription('Test Plugin');
        parent::configure();
    }

    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        try {
            $output->writeln($this->calculator->calculate(1, 1));

            return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
        } catch (\Exception $e) {
            $output->writeln('<error>' . $e->getMessage() . '</error>');
            if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
                $output->writeln($e->getTraceAsString());
            }

            return \Magento\Framework\Console\Cli::RETURN_FAILURE;
        }
    }
}
  1. Remove files in the generated and execute command bin/magento setup:di:compile
  2. Execute test command bin/magento test:plugin:calculate
    Result is 2
  3. Remove files in the generated and execute command bin/magento test:plugin:calculate
    Result is 0

Expected result (*)

  1. Precompilation or runtime compilation, the result of execution should be 0.

Actual result (*)

  1. When precompiled, the result is 2
  2. When runtime compilation, the result is 0

Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.

  • 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”.
@m2-assistant
Copy link

m2-assistant bot commented Sep 3, 2021

Hi @GrassH. Thank you for your report.
To help us process this issue please make sure that you provided the following information:

  • Summary of the issue
  • Information on your environment
  • Steps to reproduce
  • Expected and actual results

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, please, review the Magento Contributor Assistant documentation.

Please, add a comment to assign the issue: @magento I am working on this


⚠️ According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.

🕙 You can find the schedule on the Magento Community Calendar page.

📞 The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, please join the Community Contributions Triage session to discuss the appropriate ticket.

🎥 You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel

✏️ Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

@engcom-November engcom-November added the Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it label Sep 3, 2021
@ihor-sviziev ihor-sviziev added the Severity: S1 Affects critical data or functionality and forces users to employ a workaround. label Sep 3, 2021
GrassH added a commit to GrassH/magento2 that referenced this issue Sep 6, 2021
@engcom-Lima engcom-Lima self-assigned this Sep 8, 2021
@m2-assistant
Copy link

m2-assistant bot commented Sep 8, 2021

Hi @engcom-Lima. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).

    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.

  • 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • 4. Verify that the issue is reproducible on 2.4-develop branch

    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • 5. Add label Issue: Confirmed once verification is complete.

  • 6. Make sure that automatic system confirms that report has been added to the backlog.

@engcom-Lima
Copy link
Contributor

Verified the issue and reproducible on 2.4-develop instance.

Below are the steps that I did to reproduce:

  1. Added module provided by the reporter.
  2. Did setup:upgrade
  3. Deleted files in the generated folder and executed command bin/magento setup:di:compile
  4. Executed test command bin/magento test:plugin:calculate as per code
    Result came as 2
  5. Removed files in the generated and execute command bin/magento test:plugin:calculate
    Result came as 0

There is a difference in the result of Pre-compilation and runtime compilation.

@engcom-Lima engcom-Lima added Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Area: Other Developer Tools Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed and removed Issue: ready for confirmation labels Sep 8, 2021
@github-jira-sync-bot
Copy link

✅ Jira issue https://jira.corp.magento.com/browse/AC-1181 is successfully created for this GitHub issue.

@m2-assistant
Copy link

m2-assistant bot commented Sep 8, 2021

✅ Confirmed by @engcom-Lima. Thank you for verifying the issue.
Issue Available: @engcom-Lima, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

@sidolov sidolov added the Priority: P3 May be fixed according to the position in the backlog. label Sep 9, 2021
LucianTuriacArnia added a commit to buckaroo-it/Magento2 that referenced this issue Nov 1, 2022
…yment methods, Remove Unit Tests, Change AbstractMethod with BuckarooAdapter

Trying to remove all afterCancel Order plugins and use virtualType instead but from 2.4.2 using virtualType as the class of the plugin doesn't work magento/magento2#33980
GrassH added a commit to GrassH/magento2 that referenced this issue Nov 2, 2023
@GrassH
Copy link
Author

GrassH commented Nov 2, 2023

@magento I am working on this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Other Developer Tools Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Priority: P3 May be fixed according to the position in the backlog. Progress: PR in progress Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Severity: S1 Affects critical data or functionality and forces users to employ a workaround. Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it
Projects
Status: Pull Request In Progress
6 participants