Composer Not Generating Autoloads For Library
Asked Answered
R

3

13

I've set up two projects, an 'init' and a library, which is required by the init. They both have PSR-0 autoloads set, but the autoload values from the library are not added to the vendor/composer/autoload_namespaces.php in the init project.

Sample composer.json from the Library:

{
    "name": "lxp/library",
    "description": "A test library",
    "autoload": {
        "psr-0": {
            "LXP\\Library": "src/"
        }
    }        
}

Sample composer.json from the project that requires that library:

{
    "name": "lxp/init",
    "name": "A test init",
    "autoload": {
        "psr-0": {
            "LXP\\Init": "src/"
        }
    },
    "repositories": [
        {
            "type": "composer",
            "url": "http://satis.repo.redacted/"
        }
    ],
    "require": {
        "lxp/library": "dev-master"
    }
}

The library contains the class LXP\Library\Something in the file src/LXP/Library/Something.php.

The project that requires the library contains the class LXP\Init\Now in the file src/LXP/Init/Now.php.

When running composer install in the 'init' project, it downloads the library project and puts it in vendor correctly, but vendor/composer/autoload_namespaces.php doesn't contain the library's autoload information, only that of the current project.

What am I doing wrong? If I run dump-autoload in the library project then the autoload_namespaces.php file is correct, and a quick bootstrap script confirms that it does indeed pick up the class.

EDIT - This is a problem with the satis-generated packages.json. To fix it, I had to add the autoload information from the library's composer.json into the json file I supply to satis, which seems like an unnecessary duplication and so I'm probably doing it wrong. Is there a single place that autoload information can be stored for satis libraries? For example, can satis read the composer.json files that exist in the libraries it scans?

EDIT #2 - Satis does not read the composer.jsons from repositories specified as 'package' type. This is obvious in hindsight, because 'package' is used for libraries that do not have a composer.json, and is a way to wrap composer-like dependency management around them.

Changing the satis.json's repository to 'vcs' type meant that the composer.json was read, and the information (including the autoload specification) was parsed and stored in the packages.json.

@Seldaek - thank you for suggesting that my satis config was the problem, and I hope that this clarifies satis / composer behaviour for anyone else in my position.

Ratib answered 6/5, 2013 at 22:6 Comment(0)
F
8

I see two possible mistakes you may have done that would cause this:

  • You forgot to update your satis repo so the autoload config for lxp/init is not up to date in there
  • You are running composer install from a lock file, and that means composer just reads the info from the composer.lock file and does not update package metadata to the latest version available in satis. To solve this you should run composer update instead.
Flop answered 7/5, 2013 at 7:12 Comment(5)
@Seladek - my satis-generated packages.json doesn't contain any autoload information, is it supposed to? It has been regenerated since the library's autoload information was added. As this is a testbed project, I'm tearing down composer entirely between runs, deleting composer.lock and the vendor directory and re-running composer install.Ratib
@Seladek - however, I can confirm that adding the autoload information directly to the packages.json has had the desired effect. Editing the original question now.Ratib
I think now the answer depends on how you configured satis. If you used "type":"package", then probably you shouldn't, because that must be used only with code that doesn't have a composer.json. If it does you should use a type "vcs" instead, then it will scan the composer.json of the git repo and include the autoload info correctly in the generated packages.jsonFlop
Ah, yes, you just caught me making exactly the same discovery. Thanks! Also apologies for mis-spelling your username previously...Ratib
@Seladek I know this is old, but thanks so much. I spent more than 2 days var_dumping and xdebugging the composer source code because satis-and-gitlab hosted packages were not having their classmap analyzed.Swelling
U
9

Try composer dump-autoload command.

Unsearchable answered 10/9, 2015 at 10:8 Comment(1)
This happened to me when I used php5.6 composer.phar install command (since I'm running 2 parallel PHP vers); the autoload file wasn't generated. Your solution sorted me out, thanks!Colombes
F
8

I see two possible mistakes you may have done that would cause this:

  • You forgot to update your satis repo so the autoload config for lxp/init is not up to date in there
  • You are running composer install from a lock file, and that means composer just reads the info from the composer.lock file and does not update package metadata to the latest version available in satis. To solve this you should run composer update instead.
Flop answered 7/5, 2013 at 7:12 Comment(5)
@Seladek - my satis-generated packages.json doesn't contain any autoload information, is it supposed to? It has been regenerated since the library's autoload information was added. As this is a testbed project, I'm tearing down composer entirely between runs, deleting composer.lock and the vendor directory and re-running composer install.Ratib
@Seladek - however, I can confirm that adding the autoload information directly to the packages.json has had the desired effect. Editing the original question now.Ratib
I think now the answer depends on how you configured satis. If you used "type":"package", then probably you shouldn't, because that must be used only with code that doesn't have a composer.json. If it does you should use a type "vcs" instead, then it will scan the composer.json of the git repo and include the autoload info correctly in the generated packages.jsonFlop
Ah, yes, you just caught me making exactly the same discovery. Thanks! Also apologies for mis-spelling your username previously...Ratib
@Seladek I know this is old, but thanks so much. I spent more than 2 days var_dumping and xdebugging the composer source code because satis-and-gitlab hosted packages were not having their classmap analyzed.Swelling
K
5

It depends how you installing your library via Composer. For example, when downloading it as package type (same I believe with composer type), Composer never reads the composer.json file, so instead you should use vcs or git type. See: GH-6846.

Here is composer.json which should work:

{
  "require": {
    "lxp/library": "dev-master"
  },
  "repositories": [
    {
      "type": "vcs",
      "url": "http://satis.repo.redacted/"
    }
  ]
}

Then run: composer install.

For troubleshooting, try running:

  • composer dump-autoload -o -vvv.
  • composer diagnose -vvv
Keven answered 27/11, 2017 at 12:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.