drush webform-composer-update error

Created on 11 October 2024, 8 months ago

Problem/Motivation

I am trying to install the CDN libraries via composer via the drush command. But I am getting this error:

$ ddev drush webform-composer-update

 THIS IS AN EXPERIMENTAL DRUSH COMMAND.
PLEASE MAKE SURE TO BACKUP YOUR COMPOSER.JSON FILE.                                                                                                                                                                                                          
Are you sure you want update your Drupal installation's composer.json file? (yes/no) [yes]:
 > 

 [error]  Error: Attempt to assign property "choices" on array in Drupal\webform\Commands\WebformLibrariesCommands->setComposerLibraries() (line 506 of /var/www/html/docroot/modules/contrib/webform/src/Commands/WebformLibrariesCommands.php) #0 /var/www/html/docroot/modules/contrib/webform/src/Commands/WebformLibrariesCommands.php(431): Drupal\webform\Commands\WebformLibrariesCommands->setComposerLibraries(Array, Object(stdClass))
#1 [internal function]: Drupal\webform\Commands\WebformLibrariesCommands->composerUpdate(Array)
#2 /var/www/html/vendor/consolidation/annotated-command/src/CommandProcessor.php(276): call_user_func_array(Array, Array)
#3 /var/www/html/vendor/consolidation/annotated-command/src/CommandProcessor.php(212): Consolidation\AnnotatedCommand\CommandProcessor->runCommandCallback(Array, Object(Consolidation\AnnotatedCommand\CommandData))
#4 /var/www/html/vendor/consolidation/annotated-command/src/CommandProcessor.php(175): Consolidation\AnnotatedCommand\CommandProcessor->validateRunAndAlter(Array, Array, Object(Consolidation\AnnotatedCommand\CommandData))
#5 /var/www/html/vendor/consolidation/annotated-command/src/AnnotatedCommand.php(387): Consolidation\AnnotatedCommand\CommandProcessor->process(Object(Symfony\Component\Console\Output\ConsoleOutput), Array, Array, Object(Consolidation\AnnotatedCommand\CommandData))
#6 /var/www/html/vendor/symfony/console/Command/Command.php(326): Consolidation\AnnotatedCommand\AnnotatedCommand->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#7 /var/www/html/vendor/symfony/console/Application.php(1096): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#8 /var/www/html/vendor/symfony/console/Application.php(324): Symfony\Component\Console\Application->doRunCommand(Object(Consolidation\AnnotatedCommand\AnnotatedCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#9 /var/www/html/vendor/symfony/console/Application.php(175): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#10 /var/www/html/vendor/drush/drush/src/Runtime/Runtime.php(110): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#11 /var/www/html/vendor/drush/drush/src/Runtime/Runtime.php(40): Drush\Runtime\Runtime->doRun(Array, Object(Symfony\Component\Console\Output\ConsoleOutput))
#12 /var/www/html/vendor/drush/drush/drush.php(139): Drush\Runtime\Runtime->run(Array)
#13 /var/www/html/vendor/drush/drush/drush(4): require('/var/www/html/v...')
#14 /var/www/html/vendor/bin/drush(119): include('/var/www/html/v...')
#15 {main}. 

It seems that the "$repository" variable is an array here, but the method is treating it as an object. I wondered if my own composer file here was wrong (even though it works for the purpose of using repositories here), but sure enough, the composer documentation shows this repositories value as an array.

It is a bit confusing, because inspecting this via xdebug and phpstorm, the variable shows as "$repositories = {object[5]} ", which does not mean it is an object, but rather an array of 5 objects. This can be seen when exporting the variable:

array (
  0 => 
... // truncated

Steps to reproduce

Just using a standard composer.json file per the documentation with a defined "repositories" key that is an array of objects.

Proposed resolution

I tried just simply, manually replacing the $repositories->$library_name line with $repositories[$library_name]. That works as far as being able to intake the composer data without error, but when it gets to writing the updates to the composer data, it has converted my repositories section to an object. I'm not sure on the implications of this or if it matters, but I do observe that I can't successfully run composer update --lock to update my lock file. Which is how I would normally update my lock file after changes to composer.json. It doesn't error, but it doesn't seem to include the new requires for the libraries. I thought for a minute that we would need to also adjust how we are using json_decode, but it's behavior seems to be all or nothing when it comes to converting objects to arrays or vice versa.

The only way I was able to get my lock file to successfully update after was by manually converting the resulting repositories object in my composerjson file to an array, and then running "composer update --lock"

I know I am filing this against 6.2.7, which is what I am using, but I checked the 6.3.x branch, and see that at least in code, this repositories variable is being treated as an object there.

πŸ› Bug report
Status

Active

Version

6.2

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States bburg Washington D.C.

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

Comments & Activities

  • Issue created by @bburg
  • πŸ‡ΊπŸ‡ΈUnited States bburg Washington D.C.
  • Yes, it converts the repositories key to an object. Can you work up a minimal reproduction composer.json file and post that here please?

  • πŸ‡ΊπŸ‡ΈUnited States bburg Washington D.C.

    This one has more boilerplate than it probably needs, but I do get the error. Just run site install, enable webform. and run the drush webform-composer-update command.

    {
        "name": "forumone/drupal-project",
        "description": "Project template for Drupal projects with composer.",
        "type": "project",
        "license": "GPL-2.0-or-later",
        "repositories": [
            {
                "type": "composer",
                "url": "https://packages.drupal.org/8"
            },
            {
                "type": "composer",
                "url": "https://asset-packagist.org"
            }
        ],
        "require": {
            "composer/installers": "^1.10",
            "cweagans/composer-patches": "^1.7",
            "drupal/core-composer-scaffold": "^10",
            "drupal/core-recommended": "^10",
            "drupal/core-vendor-hardening": "^10",
            "drupal/libraries": "^4.0",
            "drupal/webform": "^6.2",
            "drush/drush": "^13.2",
            "joachim-n/composer-manifest": "^1",
            "npm-asset/select2": "^4.0",
            "oomphinc/composer-installers-extender": "^2.0",
            "rvtraveller/qs-composer-installer": ">=1.1",
            "vlucas/phpdotenv": "^4.0",
            "webmozart/path-util": ">=2.3"
        },
        "require-dev": {
            "dealerdirect/phpcodesniffer-composer-installer": "^1.0",
            "drupal/coder": "^8.3",
            "mglaman/phpstan-drupal": "^1.1",
            "phpspec/prophecy-phpunit": "^2",
            "phpstan/extension-installer": "^1.2",
            "phpstan/phpstan": "^1.9",
            "phpstan/phpstan-deprecation-rules": "^1.0",
            "squizlabs/php_codesniffer": "^3.7"
        },
        "conflict": {
            "drupal/drupal": "*"
        },
        "minimum-stability": "dev",
        "prefer-stable": true,
        "config": {
            "sort-packages": true,
            "platform": {
                "ext-gd": "1.0.0",
                "ext-opcache": "1.0.0",
                "ext-pdo": "1.0.0"
            },
            "allow-plugins": {
                "cweagans/composer-patches": true,
                "composer/installers": true,
                "drupal/core-composer-scaffold": true,
                "drupal/core-vendor-hardening": true,
                "drupal/console-extend-plugin": true,
                "dealerdirect/phpcodesniffer-composer-installer": true,
                "joachim-n/composer-manifest": true,
                "phpstan/extension-installer": true,
                "rvtraveller/qs-composer-installer": true,
                "oomphinc/composer-installers-extender": true
            }
        },
        "autoload": {
            "classmap": [
                "scripts/composer/ScriptHandler.php"
            ],
            "files": [
                "load.environment.php"
            ]
        },
        "scripts": {
            "install-codestandards": [
                "PHPCSStandards\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin::run"
            ],
            "php-stan": [
                "./vendor/bin/phpstan --memory-limit=1024M"
            ],
            "phpcs": [
                "./vendor/bin/phpcs"
            ],
            "phpcbf": [
                "./vendor/bin/phpcbf"
            ],
            "build-assets": [
                "composer install --optimize-autoloader --no-dev"
            ],
            "lint": "find web/modules/custom web/themes/custom -name '*.php' -exec php -l {} \\;",
            "code-sniff": [
                "./vendor/bin/phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md --ignore=node_modules,bower_components,vendor ./web/modules/custom",
                "./vendor/bin/phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md --ignore=node_modules,bower_components,vendor ./web/themes/custom",
                "./vendor/bin/phpcs --standard=DrupalPractice --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md --ignore=node_modules,bower_components,vendor ./web/modules/custom",
                "./vendor/bin/phpcs --standard=DrupalPractice --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md --ignore=node_modules,bower_components,vendor ./web/themes/custom"
            ],
            "unit-test": "echo 'No unit test step defined.'",
            "pre-install-cmd": [
                "DrupalProject\\composer\\ScriptHandler::checkComposerVersion"
            ],
            "pre-update-cmd": [
                "DrupalProject\\composer\\ScriptHandler::checkComposerVersion"
            ],
            "post-install-cmd": [
                "DrupalProject\\composer\\ScriptHandler::createRequiredFiles",
                "@install-codestandards"
            ],
            "post-update-cmd": [
                "DrupalProject\\composer\\ScriptHandler::createRequiredFiles"
            ],
            "post-create-project-cmd": [
                "DrupalProject\\composer\\ScriptHandler::createRequiredFiles"
            ]
        },
        "extra": {
            "composer-exit-on-patch-failure": true,
            "patches-file": "composer.patches.json",
            "patchLevel": {
                "drupal/core": "-p2"
            },
            "drupal-scaffold": {
                "file-mapping": {
                    "[web-root]/CHANGELOG.txt": false,
                    "[web-root]/COPYRIGHT.txt": false,
                    "[web-root]/INSTALL.mysql.txt": false,
                    "[web-root]/INSTALL.pgsql.txt": false,
                    "[web-root]/INSTALL.sqlite.txt": false,
                    "[web-root]/INSTALL.txt": false,
                    "[web-root]/LICENSE.txt": false,
                    "[web-root]/MAINTAINERS.txt": false,
                    "[web-root]/README.md": false,
                    "[web-root]/UPDATE.txt": false,
                    "[web-root]/USAGE.txt": false
                },
                "locations": {
                    "web-root": "web/"
                }
            },
            "installer-paths": {
                "web/core": [
                    "type:drupal-core"
                ],
                "web/libraries/{$name}": [
                    "type:drupal-library",
                    "type:bower-asset",
                    "type:npm-asset"
                ],
                "web/modules/contrib/{$name}": [
                    "type:drupal-module"
                ],
                "web/profiles/contrib/{$name}": [
                    "type:drupal-profile"
                ],
                "web/themes/contrib/{$name}": [
                    "type:drupal-theme"
                ],
                "drush/Commands/contrib/{$name}": [
                    "type:drupal-drush"
                ]
            },
            "installer-types": [
                "bower-asset",
                "npm-asset"
            ]
        }
    }
    
    
  • Please remove the excess for us.

  • πŸ‡ΊπŸ‡ΈUnited States bburg Washington D.C.

    I updated the example in my previous comment to use a version starting from composer create-project per the documentation in
    https://www.drupal.org/docs/develop/using-composer/manage-dependencies β†’ instead of my own starter version.

    I still get the error as before.

  • Yes, of course. It is reproducible. If you are looking for a workaround, change the repositories key to object-style, which is as follows:

        "repositories": {
            "drupal": {
                "type": "composer",
                "url": "https://packages.drupal.org/8"
            }
        }
    
  • πŸ‡ΊπŸ‡ΈUnited States bburg Washington D.C.

    I was just looking into this issue and found that this is a duplicate to πŸ› Error: Attempt to assign property on array in WebformLibrariesCommands->setComposerLibraries() Active .

Production build 0.71.5 2024