Composer package registry re-orders package data

Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.

Summary

Gitlab's Composer package registry has a behaviour where package data from composer.json is sorted unexpectedly and inconsistently with the source data.

The behaviour appears to be that key:value pairs are ordered by key length.

For composer.json values which require an expected ordering, this behaviour may cause breakage.

Steps to reproduce

  • Publish a package using Gitlab's Composer package registry
  • Compare the metadata of the source composer.json and the version available from the package registry
  • Values will be ordered according to the length of the key?!

Example Project

https://gitlab.com/gl364606/gl364606/

What is the current bug behavior?

The JSON output is modified in ways which can cause unexpected behaviour in consuming applications.

Input:

{
    "require": {
          "php": "^8.0",
          "ext-json": "*",
          "chriskonnertz/open-graph": "^2.0",
          "laravel/framework": "^8.0",
          "spatie/schema-org": "^3.5.0"
    },
    "require-dev": {
          "brainmaestro/composer-git-hooks": "^2.8.2",
          "friendsofphp/php-cs-fixer": "^3.0.0",
          "phpunit/phpunit": "^9.5"
    }
}

Output:

{
        "require": {
          "php": "^8.0",
          "ext-json": "*",
          "laravel/framework": "^8.0",
          "spatie/schema-org": "^3.5.0",
          "chriskonnertz/open-graph": "^2.0"
        },
        "require-dev": {
          "phpunit/phpunit": "^9.5",
          "friendsofphp/php-cs-fixer": "^3.0.0",
          "brainmaestro/composer-git-hooks": "^2.8.2"
        }
}

A popular tool impacted by this behaviour is cweagans/composer-patches. Patches may need application in a specific order, and re-ordering of JSON data can cause patches to fail when applied in an unexpected order.

What is the expected correct behavior?

The JSON input at publication time (ie that in the published tag) should be accurately represented in the data output by the package registry.

Relevant logs and/or screenshots

Input:

image

Output:

image

Behaviour also visible at this (non gitlab.com) URL:

Output of checks

This bug can be observed on GitLab.com when using the Composer Package Registry feature.

See linked example repo + jobs, or test in gitlab-org/gitlab#364606: Test for Composer pac... (!256532 - closed)

Possible fixes

Some approaches to address this might be:

  • reading and serving composer.json from Git storage, bypassing Postgres round-trip
  • changing storage type (eg from JSONB to JSON) by converting existing composer_json column,
  • storing raw data separately in a new column / storage (⁉️ composer_json column is a copy of composer.json content, a third copy would be too many!)

Internal components could keep using the composer_json column if it's helpful; we just need to not serve that to Composer.

Edited by Chris Burgess