简体   繁体   English

Composer 仅安装 require 和 require-dev 块中的内容,而不是使用本地存储库时的依赖项

[英]Composer installs only what's in the require and require-dev blocks and not the dependencies when using local repository

Under regular circumstances, if I delete the vendor folder and the composer.lock file, and then run composer install , it will re-install everything, including the dependencies for the packages in the require and require-dev block.通常情况下,如果我删除vendor文件夹和composer.lock文件,然后运行composer install ,它将重新安装所有内容,包括requirerequire-dev块中包的依赖项。 But when I use composer install from my local repository, it only installs what's inside require and require-dev .但是当我使用本地存储库中的composer install时,它只会安装requirerequire-dev中的内容。

This is an example composer.json of a clean Laravel 9 project:这是一个干净的 Laravel 9 项目的示例composer.json

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",    
    "require": {
        "php": "^8.0.2",
        "guzzlehttp/guzzle": "^7.2",
        "laravel/framework": "^9.19",
        "laravel/sanctum": "^3.0",
        "laravel/tinker": "^2.7"

    },
    "require-dev": {
        "fakerphp/faker": "^1.9.1",
        "laravel/pint": "^1.0",
        "laravel/sail": "^1.0.1",
        "mockery/mockery": "^1.4.4",
        "nunomaduro/collision": "^6.1",
        "phpunit/phpunit": "^9.5.10",
        "spatie/laravel-ignition": "^1.0"
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-update-cmd": [
            "@php artisan vendor:publish --tag=laravel-assets --ansi --force"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true,
        "allow-plugins": {
            "pestphp/pest-plugin": true
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}

If I run composer install and set my local repository (add the following to composer.json ):如果我运行composer install并设置我的本地存储库(将以下内容添加到composer.json ):

"repositories": [ { "type": "composer", "url": "http://localhost/" } ],

it will install only the 11 packages defined above inside require and required-dev , but not their dependencies.它将只安装上面定义的 11 个包requirerequired-dev ,但不安装它们的依赖项。 Then it will throw an error about missing things:然后它会抛出一个关于丢失东西的错误:

> @php artisan vendor:publish --tag=laravel-assets --ansi --force

Fatal error: Uncaught Error: Class "Illuminate\Foundation\Application" not found

Why does this happen?为什么会这样? My local repository should work because when I compare the 11 packages installed by my local repository to the same 11 packages when installed by Packagist, they are the same, so I know my local repository should have the correct packages我的本地存储库应该可以工作,因为当我将本地存储库安装的 11 个包与 Packagist 安装的相同 11 个包进行比较时,它们是相同的,所以我知道我的本地存储库应该有正确的包

(And of course the local repository also contains the rest of the packages, not the these 11) (当然,本地存储库还包含软件包的 rest,而不是这 11 个)

I found the solution, to anyone encounters this problem in the future: even though each package zip file contains the composer.json file, it's not enough, since Satis does not read from that file, instead it reads the metadata from your repository.我找到了解决方案,对于将来遇到此问题的任何人:即使每个 package zip 文件都包含composer.json文件,这还不够,因为 Satis 不会从您的存储库中读取元数据。

So it's important to setup your satis.json to include all metadata fields that are required for Composer to work, so called root fields.因此,重要的是设置您的satis.json以包含 Composer 工作所需的所有元数据字段,即所谓的root字段。

In my case I only used require and autoload fields in satis.json , which caused the issue.在我的情况下,我只在satis.json中使用了requireautoload字段,这导致了这个问题。

After adding all the fields necessary - require , autoload , conflict , replace , provide and suggest , everything worked!在添加了所有必要的字段之后requireautoloadconflictreplaceprovidesuggest ,一切正常!

  • Not all packages have all these fields, I wrote a script to add only the present fields并非所有包都有所有这些字段,我编写了一个脚本来仅添加当前字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM