Problem/Motivation

Once upon a time, in the ckeditor5 contrib module, commit-code-check.sh was used to do core-quality code checks on Drupal CI. This necessitated a bunch of awkward sed calls and other very hacky dance moves.

Automatic Updates does the same thing. I'm sure it would help Project Browser too. Or anything else that is developed first in contrib, for eventual inclusion in core.

Proposed resolution

Make it possible for commit-code-check.sh to run against a particular directory.

This will enable any Drupal contrib module to add this to their drupalci.yml:

      container_command.commit-checks:
        commands:
          - ../../../core/scripts/dev/commit-code-check.sh --drupalci
        halt-on-fail: true

… and get the same exact checks as core does:

https://www.drupal.org/project/automatic_updates, which has a phpstan.neon.dist and a .cspell.json
$ sh ../../../core/scripts/dev/commit-code-check.sh
1/3 /Users/wim.leers/core/modules/contrib/automatic_updates/drupalci.yml 175.53ms
2/3 /Users/wim.leers/core/modules/contrib/automatic_updates/next 4.37ms
3/3 /Users/wim.leers/core/modules/contrib/automatic_updates/package_manager/tests/src/Kernel/PackageManagerKernelTestBase.php 55.63ms
CSpell: Files checked: 3, Issues found: 0 in 0 files

CSpell: passed

----------------------------------------------------------------------------------------------------

Running PHPStan on *all* files.

                                                                                                                        
 [OK] No errors                                                                                                         
                                                                                                                        


PHPStan: passed

----------------------------------------------------------------------------------------------------
............................................................  60 / 332 (18%)
............................................................ 120 / 332 (36%)
.....S...................................................... 180 / 332 (54%)
............................................................ 240 / 332 (72%)
............................................................ 300 / 332 (90%)
................................                             332 / 332 (100%)


Time: 2.81 secs; Memory: 20MB


PHPCS: passed

----------------------------------------------------------------------------------------------------
Checking next

next passed

----------------------------------------------------------------------------------------------------
Checking drupalci.yml

ESLint: drupalci.yml passed
drupalci.yml passed

----------------------------------------------------------------------------------------------------
Checking package_manager/tests/src/Kernel/PackageManagerKernelTestBase.php

package_manager/tests/src/Kernel/PackageManagerKernelTestBase.php passed

----------------------------------------------------------------------------------------------------
https://www.drupal.org/project/cdn, which has a phpcs.xml that extends core's
$ sh ../../../core/scripts/dev/commit-code-check.sh
1/2 /Users/wim.leers/core/modules/contrib/cdn/interdiff.txt 169.54ms
2/2 /Users/wim.leers/core/modules/contrib/cdn/wip.patch 14.94ms X
/Users/wim.leers/core/modules/contrib/cdn/wip.patch:24:51 - Unknown word (Validatable)
/Users/wim.leers/core/modules/contrib/cdn/wip.patch:34:51 - Unknown word (Validatable)
/Users/wim.leers/core/modules/contrib/cdn/wip.patch:49:2 - Unknown word (farfuture)
/Users/wim.leers/core/modules/contrib/cdn/wip.patch:78:42 - Unknown word (farfuture)
CSpell: Files checked: 2, Issues found: 4 in 1 files

CSpell: failed

----------------------------------------------------------------------------------------------------

Running PHPStan on *all* files.
 ------ --------------------------------------------------------------------- 
  Line   cdn.module                                                           
 ------ --------------------------------------------------------------------- 
  53     Function file_create_url not found.                                  
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  53     Function file_url_transform_relative not found.                      
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
 ------ --------------------------------------------------------------------- 

 ------ --------------------------------------------------------------------------------- 
  Line   tests/src/Unit/File/FileUrlGeneratorTest.php                                     
 ------ --------------------------------------------------------------------------------- 
  523    Anonymous function has an unused use $base_path.                                 
  523    Anonymous function has an unused use $current_uri.                               
  523    Anonymous function has an unused use $root.                                      
  556    Class Drupal\cdn\CdnSettings constructor invoked with 2 parameters, 1 required.  
 ------ --------------------------------------------------------------------------------- 

 -- ---------------------------------------------------------------------------------------------------------------------------------- 
     Error                                                                                                                             
 -- ---------------------------------------------------------------------------------------------------------------------------------- 
     Ignored error pattern #^Plugin definitions cannot be altered.# was not matched in reported errors.                                
     Ignored error pattern #^Missing cache backend declaration for performance.# was not matched in reported errors.                   
     Ignored error pattern #cache tag might be unclear and does not contain the cache key in it.# was not matched in reported errors.  
     Ignored error pattern #^Class .* extends @internal class# was not matched in reported errors.                                     
 -- ---------------------------------------------------------------------------------------------------------------------------------- 

                                                                                                                        
 [ERROR] Found 10 errors                                                                                                
                                                                                                                        


PHPStan: failed

----------------------------------------------------------------------------------------------------
.......................... 26 / 26 (100%)


Time: 293ms; Memory: 12MB


PHPCS: passed

----------------------------------------------------------------------------------------------------
Checking composer.lock

composer.lock passed

----------------------------------------------------------------------------------------------------
Checking interdiff.txt

interdiff.txt passed

----------------------------------------------------------------------------------------------------
Checking wip.patch

wip.patch passed

----------------------------------------------------------------------------------------------------
https://www.drupal.org/project/big_pipe_sessionless, which has a phpcs.xml that extends core's:
$ sh ../../../core/scripts/dev/commit-code-check.sh
1/1 /Users/wim.leers/core/modules/contrib/big_pipe_sessionless/big_pipe_sessionless.info.yml 192.23ms X
/Users/wim.leers/core/modules/contrib/big_pipe_sessionless/big_pipe_sessionless.info.yml:1:7 - Unknown word (Sessionless)
CSpell: Files checked: 1, Issues found: 1 in 1 files

CSpell: failed

----------------------------------------------------------------------------------------------------

Running PHPStan on *all* files.
 ------ ---------------------------------------------------------------------------------------------------------------- 
  Line   tests/src/Unit/Render/Placeholder/BigPipeSessionlessStrategyTest.php                                            
 ------ ---------------------------------------------------------------------------------------------------------------- 
  59     You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).  
 ------ ---------------------------------------------------------------------------------------------------------------- 

 -- ---------------------------------------------------------------------------------------------------------------------------------- 
     Error                                                                                                                             
 -- ---------------------------------------------------------------------------------------------------------------------------------- 
     Ignored error pattern #^Unsafe usage of new static# was not matched in reported errors.                                           
     Ignored error pattern #^Plugin definitions cannot be altered.# was not matched in reported errors.                                
     Ignored error pattern #^Missing cache backend declaration for performance.# was not matched in reported errors.                   
     Ignored error pattern #cache tag might be unclear and does not contain the cache key in it.# was not matched in reported errors.  
     Ignored error pattern #^Class .* extends @internal class# was not matched in reported errors.                                     
 -- ---------------------------------------------------------------------------------------------------------------------------------- 

                                                                                                                        
 [ERROR] Found 6 errors                                                                                                 
                                                                                                                        


PHPStan: failed

----------------------------------------------------------------------------------------------------
.....E.... 10 / 10 (100%)



FILE: /Users/wim.leers/core/modules/contrib/big_pipe_sessionless/src/StackMiddleware/BigPipeSessionlessPageCache.php
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 2 LINES
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 28 | ERROR | Description for the @return value is missing (Drupal.Commenting.FunctionComment.MissingReturnComment)
 30 | ERROR | Public method name "BigPipeSessionlessPageCache::_storeResponse" is not in lowerCamel format
    |       | (Drupal.NamingConventions.ValidFunctionName.ScopeNotCamelCaps)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Time: 83ms; Memory: 10MB


PHPCS: failed

----------------------------------------------------------------------------------------------------
Checking big_pipe_sessionless.info.yml

ESLint: big_pipe_sessionless.info.yml passed
big_pipe_sessionless.info.yml passed

----------------------------------------------------------------------------------------------------

This would allow every contrib module that chooses to do so, to follow the lead of continuously matching core's evolving best practices.

Remaining tasks

Implement this in a merge request and manually test it. Then, RTBC and merge it, and rejoice! Standard procedure.

User interface changes

TBD; I'm not sure commit-code-check.sh counts as a user interface.

API changes

TBD; I'm not sure it counts as an API either.

Data model changes

None.

Release notes snippet

TBD, but this being an internal development script, I doubt it will need one.

Issue fork drupal-3314100

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

phenaproxima created an issue. See original summary.

phenaproxima’s picture

Issue summary: View changes
phenaproxima’s picture

Issue summary: View changes
wim leers’s picture

Priority: Normal » Major

YES PLEASE!! 🤩

TBD; I'm not sure commit-code-check.sh counts as a user interface.

I'm 99% certain the answer is no! 🤓

phenaproxima’s picture

Title: Make it possible to run commit-code-check.sh against an arbitrary path » [PP-1] Make it possible to run commit-code-check.sh against an arbitrary path
Status: Active » Postponed
Related issues: +#3314151: Fix cspell use: specify globRoot and always pass --root to cspell
wim leers’s picture

wim leers’s picture

I've fixed #3314151: Fix cspell use: specify globRoot and always pass --root to cspell. Please review, so we can move forward here :)

wim leers’s picture

Title: [PP-1] Make it possible to run commit-code-check.sh against an arbitrary path » Make it possible to run commit-code-check.sh against an arbitrary path
Status: Postponed » Needs work

VladimirAus made their first commit to this issue’s fork.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

wim leers’s picture

Status: Needs work » Needs review
StatusFileSize
new696 bytes
new1.27 KB

Rather than recreating this MR to target 11.x, I'm just uploading it as a patch. (Also, the conflict that #3314151: Fix cspell use: specify globRoot and always pass --root to cspell caused was not resolved correctly in the existing MR. 😅 But it's tricky!)

Also because creating a new branch endlessly tells me invalid reference name '11.x' 🤪😬😬

This patch correctly resolves the conflict: #3314151: Fix cspell use: specify globRoot and always pass --root to cspell already did everything we needed! 😊

wim leers’s picture

StatusFileSize
new837 bytes
new1.52 KB
+++ b/core/scripts/dev/commit-code-check.sh
@@ -109,6 +109,8 @@
+DRUPAL_ROOT=${DRUPAL_ROOT:="$TOP_LEVEL"}

We can do better than this I think.

This means that you would have to first cd into the contrib module and then specify the Drupal root manually:

cd modules/contrib/cdn
DRUPAL_ROOT=<SOMETHING> sh ../../../core/scripts/dev/commit-code-check.sh

… but the script is already in Drupal core. So why don't we use that to determine the Drupal root? 🤓

wim leers’s picture

Assigned: Unassigned » wim leers

#13 works fine:

$ sh ../../../core/scripts/dev/commit-code-check.sh 
/Users/wim.leers/core/modules/contrib/automatic_updates
 1/13 /Users/wim.leers/core/modules/contrib/automatic_updates/3347584-2.patch 170.51ms
 2/13 /Users/wim.leers/core/modules/contrib/automatic_updates/3350457-2.patch 4.88ms
 3/13 /Users/wim.leers/core/modules/contrib/automatic_updates/3350568-10.patch 5.31ms
 4/13 /Users/wim.leers/core/modules/contrib/automatic_updates/3350568-11.patch 5.46ms
 5/13 /Users/wim.leers/core/modules/contrib/automatic_updates/3350568-18.patch 14.84ms
 6/13 /Users/wim.leers/core/modules/contrib/automatic_updates/3350568.patch 3.15ms
 7/13 /Users/wim.leers/core/modules/contrib/automatic_updates/3351895-41-update-core-MR-converter-do-not-test.patch 2.38ms
 8/13 /Users/wim.leers/core/modules/contrib/automatic_updates/3351895-41-update-core-MR-converter.patch 1.85ms
 9/13 /Users/wim.leers/core/modules/contrib/automatic_updates/3354914-5.patch 5.33ms
10/13 /Users/wim.leers/core/modules/contrib/automatic_updates/drupalci.yml 4.82ms
11/13 /Users/wim.leers/core/modules/contrib/automatic_updates/f 2.96ms
12/13 /Users/wim.leers/core/modules/contrib/automatic_updates/mirror.patch 10.13ms
13/13 /Users/wim.leers/core/modules/contrib/automatic_updates/next 1.95ms
CSpell: Files checked: 13, Issues found: 0 in 0 files

CSpell: passed

Note that cSpell automatically picked up the .cspell.json in that module (i.e. the current working directory)!

Contrast that with:

$ sh ../../../core/scripts/dev/commit-code-check.sh 
 1/22 /Users/wim.leers/core/modules/contrib/cdn/.phpcs-cache 279.23ms X
/Users/wim.leers/core/modules/contrib/cdn/.phpcs-cache:1:9746 - Unknown word (Validatable)
/Users/wim.leers/core/modules/contrib/cdn/.phpcs-cache:1:415639 - Unknown word (Farfuture)
/Users/wim.leers/core/modules/contrib/cdn/.phpcs-cache:1:440137 - Unknown word (Farfuture)
…
/Users/wim.leers/core/modules/contrib/cdn/tests/src/Functional/CdnIntegrationTest.php:207:55 - Unknown word (Farfuture)
/Users/wim.leers/core/modules/contrib/cdn/tests/src/Functional/CdnIntegrationTest.php:220:27 - Unknown word (Farfuture)
CSpell: Files checked: 22, Issues found: 95 in 17 files

CSpell: failed

That module does not have a .cspell.json 👍

Next up: phpstan.

wim leers’s picture

StatusFileSize
new2.46 KB
new3.93 KB

First let's stop running all tests, we only want the commit checks. Let's save some DrupalCI cycles 😊

wim leers’s picture

StatusFileSize
new1.22 KB
new3.95 KB

Apparently we need some test results for it to succeed.

wim leers’s picture

StatusFileSize
new2.6 KB
new5.71 KB

Now also works for phpstan:

$ sh ../../../core/scripts/dev/commit-code-check.sh 
 1/14 /Users/wim.leers/core/modules/contrib/automatic_updates/3347584-2.patch 173.93ms
 2/14 /Users/wim.leers/core/modules/contrib/automatic_updates/3350457-2.patch 4.57ms
 3/14 /Users/wim.leers/core/modules/contrib/automatic_updates/3350568-10.patch 5.71ms
 4/14 /Users/wim.leers/core/modules/contrib/automatic_updates/3350568-11.patch 5.59ms
 5/14 /Users/wim.leers/core/modules/contrib/automatic_updates/3350568-18.patch 15.23ms
 6/14 /Users/wim.leers/core/modules/contrib/automatic_updates/3350568.patch 3.29ms
 7/14 /Users/wim.leers/core/modules/contrib/automatic_updates/3351895-41-update-core-MR-converter-do-not-test.patch 2.75ms
 8/14 /Users/wim.leers/core/modules/contrib/automatic_updates/3351895-41-update-core-MR-converter.patch 1.87ms
 9/14 /Users/wim.leers/core/modules/contrib/automatic_updates/3354914-5.patch 3.47ms
10/14 /Users/wim.leers/core/modules/contrib/automatic_updates/drupalci.yml 9.52ms
11/14 /Users/wim.leers/core/modules/contrib/automatic_updates/f 2.94ms
12/14 /Users/wim.leers/core/modules/contrib/automatic_updates/mirror.patch 6.17ms
13/14 /Users/wim.leers/core/modules/contrib/automatic_updates/next 2.00ms
14/14 /Users/wim.leers/core/modules/contrib/automatic_updates/package_manager/tests/src/Kernel/PackageManagerKernelTestBase.php 48.94ms
CSpell: Files checked: 14, Issues found: 0 in 0 files

CSpell: passed

----------------------------------------------------------------------------------------------------

Running PHPStan on *all* files.

                                                                                                                        
 [OK] No errors                                                                                                         
                                                                                                                        


PHPStan: passed

👆 This means it works, because if it were using core's phpstan.neon.dist, it would fail: there's additional ignoreErrors entries! 🥳

The last submitted patch, 12: 3314100-12.patch, failed testing. View results

wim leers’s picture

Assigned: wim leers » Unassigned
Issue summary: View changes
StatusFileSize
new6.13 KB
new11.2 KB

And now everything else:

  1. phpcs
  2. And under for FILE in $FILES; do, running:
    1. stat permissions checks
    2. phpcs on YAML
    3. eslint
    4. PCSS → CSS compiling

👍

Specifically skipped for non-core projects, because they're highly specific to Drupal core:

  1. yarn run -s lint:core-js-passing
  2. yarn run -s lint:css
  3. yarn run -s check:ckeditor5
  4. yarn run build:css --check

And under for FILE in $FILES; do, skipped these:

  1. # Don't commit changes to vendor.
  2. # Don't commit changes to core/node_modules.
  3. phpcs on PHP files → because we've already scanned everything above, so no more need to scan one-by-one!

Resulting output:

$ sh ../../../core/scripts/dev/commit-code-check.sh
1/3 /Users/wim.leers/core/modules/contrib/automatic_updates/drupalci.yml 175.53ms
2/3 /Users/wim.leers/core/modules/contrib/automatic_updates/next 4.37ms
3/3 /Users/wim.leers/core/modules/contrib/automatic_updates/package_manager/tests/src/Kernel/PackageManagerKernelTestBase.php 55.63ms
CSpell: Files checked: 3, Issues found: 0 in 0 files

CSpell: passed

----------------------------------------------------------------------------------------------------

Running PHPStan on *all* files.

                                                                                                                        
 [OK] No errors                                                                                                         
                                                                                                                        


PHPStan: passed

----------------------------------------------------------------------------------------------------
............................................................  60 / 332 (18%)
............................................................ 120 / 332 (36%)
.....S...................................................... 180 / 332 (54%)
............................................................ 240 / 332 (72%)
............................................................ 300 / 332 (90%)
................................                             332 / 332 (100%)


Time: 2.81 secs; Memory: 20MB


PHPCS: passed

----------------------------------------------------------------------------------------------------
Checking next

next passed

----------------------------------------------------------------------------------------------------
Checking drupalci.yml

ESLint: drupalci.yml passed
drupalci.yml passed

----------------------------------------------------------------------------------------------------
Checking package_manager/tests/src/Kernel/PackageManagerKernelTestBase.php

package_manager/tests/src/Kernel/PackageManagerKernelTestBase.php passed

----------------------------------------------------------------------------------------------------

👆 this means that modules/contrib/automatic_updates/drupalci.yml could use:

      container_command.commit-checks:
        commands:
          - ../../../core/scripts/dev/commit-code-check.sh --drupalci
        halt-on-fail: true

… and that's all it would take for a contrib module to adopt this!

wim leers’s picture

Title: Make it possible to run commit-code-check.sh against an arbitrary path » Make it possible for contrib modules to reuse core's commit-code-check.sh

While the original title was accurate, I think this title better reflects the ecosystem impact this is aiming at! 😊

wim leers’s picture

Issue summary: View changes

Adding a second and third example to the issue summary.

wim leers’s picture

StatusFileSize
new1.3 KB
new11.32 KB

I got something wrong in the PHPStan one.

phenaproxima’s picture

Status: Needs review » Needs work

Tests not passing...?

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.