phpcomposer-php

Find out version of composer used to build composer.lock file


Most of my projects are built using Composer 1. Now that I am upgrading to Composer 2, I would like to run a command to get the version of Composer that was used to build the project. What is the best way to get the version? I believe the version is stored in plugin-api-version but not sure what is the best way to get that. Was hoping that there was a Composer command to read that.


Solution

  • Having been in the position where a project broke when switching to Composer 2, I have the same problem. Sure, in my case it was a bug that caused it which Composer 1 handled differently to Composer 2, but that doesn't matter. It means we can't switch Composer version across all projects blindly. It needs to be done and tested on a per-project basis. So I did some digging.

    plugin-api-version has been added in Composer v1.10.0. It doesn't appear that the plugin API version is identical to the Composer version, but it would appear that the major version at least matches, so it suffices to know whether Composer 1 or 2 has been used. However, you must keep in mind that plugin-api-version is missing from composer.lock files built with Composer version 1.9.0 or earlier. Based on that info, I wrote this shell script:

    for f in $(find projects/ -maxdepth 2 -name composer.lock); do echo -n $f:; grep plugin-api-version $f || echo "plugin-api-version missing"; done
    

    Some example output:

    projects/abc/composer.lock:    "plugin-api-version": "2.3.0"
    projects/def/composer.lock:plugin-api-version missing
    projects/ghi/composer.lock:plugin-api-version missing
    projects/jkl/composer.lock:plugin-api-version missing
    projects/mno/composer.lock:    "plugin-api-version": "2.3.0"
    projects/pqr/composer.lock:    "plugin-api-version": "2.6.0"
    projects/stu/composer.lock:plugin-api-version missing
    projects/vwxyz/composer.lock:    "plugin-api-version": "1.1.0"