Home > Net >  Composer is not installing the required dependencies of a forked library
Composer is not installing the required dependencies of a forked library

Time:04-26

There is a library on Github, named abc/xyz.

I have forked the library to update its composer.json.

The require section of its composer.json is:

{
   "require": {
       "xyz/abc": "^1.2",
   }
}

Therequire section of the forked version is:

{
    "require": {
        "xyz/abc": "^2.4",
    }
}

When I do composer require abc/xyz, it also downloads and installs the xyz/abc library.

But when I use the forked version and add the following to the repositories section of my root composer.json

{
   "repositories": [
      {
         "type": "package",
           "package": {
               "name": "abc/xyz",
               "version": "1.2",
               "dist": {
                  "url": "url to zip folder of the forked version",
                  "type": "zip"
                },
            "type": "library"
           }
      }
   ]
}

Now when I do composer require abc/xyz, it only downloads and installs the actual package and not the xyz/abc.

I also tried with the type: git instead of zip and changed the url to the git version but the same results.

It only downloads and installs that dependency if I use it in the root composer.json

"type": "package",
                "package": {
                    "name": "abc/xyz",
                    "version": "1.2",
                    "dist": {
                       "url": "url to zip folder of the forked version",
                       "type": "zip"
                    },
                    "type": "library",
                    "require": {
                      "xyz/abc": "^2.4",
                    }
                }

Is this a standard way? OR I am doing something wrong here?

CodePudding user response:

When you use a repository of type package, the linked package's composer.json is not read.

The package type is meant to define the composer.json file inline, mostly for packages that do not support composer.

From the docs:

package: If you depend on a project that does not have any support for Composer whatsoever you can define the package inline using a package repository. You basically inline the composer.json object.

(emphasis mine)

If your package does support composer (e.g. includes a composer.json file that defines dependencies, autoloader, etc.), use type vcs.

To use a forked version, you simply add a repository that includes the forked version. Typically, for forks I simply depend on dev-master/dev-main, because the fork is not really published and only includes some hot-fixes that do not exist in the "main" package. Or dev-whatever-branch-your-fixes-exist.

Assuming your fork of xyz/abc is hosted on https://github.com/ahmad/abc, your root composer.json should be something like:

{
   "repositories": [
      {
         "type": "vcs",
         "url": "https://github.com/ahmad/abc"
      }
   ],
   "require": {
      "xyz/abc": "dev-master"
    }
}

(Just added the relevant parts, obviously this doesn't mean you have to delete the rest of your root composer.json file, just change the appropriate bits)

  • Related