Backend: Checking `$CI_PIPELINE_SOURCE` for `merge_request_event` doesn't work with `include:project:rules` / `include:project:`

Summary

Conditionally including pipeline configuration from another project in a merge request pipeline does not work as expected when the rule is if: '$CI_PIPELINE_SOURCE == "merge_request_event"'. This happens despite Use variables with include explicitly stating that CI_PIPELINE_SOURCE can be used in include, and there is no mention anywhere that this doesn't cover include:project:rules as well. include:local:rules works as expected, but include:local: without a rule on the include also seems affected.

Steps to reproduce

Have a project (your-namespace/your/project) with these two files:

.gitlab-ci.yml

dummy-job:
  script:
    - echo "test"
  rules:
     - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
       when: always

include:
  - local: 'include.yml'
    rules:
      - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
        when: always

include.yml

job-from-include:
  script:
    - echo "test"
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      when: always

Open a merge request with this configuration in place, and observe that both jobs are in the resulting pipeline. (example pipeline, config)

Now change the include:local to include:project: in .gitlab-ci.yml:

dummy-job:
  script:
    - echo "test"
  rules:
     - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
       when: always

include:
  - project: your-namespace/your/project
    ref: main
    file: 'include.yml'
    rules:
      - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
        when: always

Open a merge request with this configuration in place, and observe that only the dummy-job gets added to the pipeline. (example pipeline, config)

What is the current bug behavior?

Project includes do not happen with a if: '$CI_PIPELINE_SOURCE == "merge_request_event"' rule in a merge request pipeline

What is the expected correct behavior?

Project includes do happen with a if: '$CI_PIPELINE_SOURCE == "merge_request_event"' rule in a merge request pipeline

Output of checks

This bug happens on GitLab.com

Possible fixes

Edited by Manuel Grabowski