Composer not autoloading your package?

I needed to link a GitHub hosted library into my project with composer. It was my own fork of a library that was abandoned. I didn’t want to register it with packagist.com because it already is infested with weeds of this kind. Conveniently, the library already had composer.json so I just needed to edit my project’s composer.json and link to the library. Easy I thought.

  1. {
  2.   "repositories": [
  3.     {
  4.       "type": "package",
  5.       "package": {
  6.         "name": "xpavp03/nette-csv-response",
  7.         "version": "dev-patch-1",
  8.         "source": {
  9.           "type": "git",
  10.           "url": "https://github.com/xpavp03/nette-csv-response.git",
  11.           "reference": "origin/patch-1"
  12.         }
  13.       }
  14.     }
  15.   ],
  16.   "require": {
  17.  …
  18.   "xpavp03/nette-csv-response": "dev-patch-1"
  19.   }
  20. }
  21.  

Well, it did download the files but it also created a Git repository in my vendors structure. I could work around it by excluding it during deployment but I wanted to see if I can fetch the library without it.

  1.  "repositories": [
  2.     {
  3.       "type": "package",
  4.       "package": {
  5.         "name": "xpavp03/nette-csv-response",
  6.         "version": "dev-patch-1",
  7.         "dist": {
  8.           "type": "zip",
  9.           "url": "https://github.com/xpavp03/nette-csv-response/archive/patch-1.zip"
  10.         }
  11.       }
  12.     }
  13.   ],
  14.  

Worked great but, composer didn’t include this library in its autoloading files. Mhm…

It turned out that if you link a library as a package composer won’t look for its composer.json. It didn’t matter that it existed and had instructions for autoloading – composer didn’t expect it there and didn’t try looking. So the solution was to include autoloading instructions into my project’s composer.json:

  1.  "repositories": [
  2.     {
  3.       "type": "package",
  4.       "package": {
  5.         "name": "xpavp03/nette-csv-response",
  6.         "version": "dev-patch-1",
  7.         "dist": {
  8.           "type": "zip",
  9.           "url": "https://github.com/xpavp03/nette-csv-response/archive/patch-1.zip"
  10.         }
  11.         "autoload": {
  12.           "classmap": ["src/"]
  13.         }
  14.       }
  15.     }
  16.   ],
  17.  

I wish this was mentioned in the documentation.

Leave a Reply