wecodemore / composer-linked-packages
Composer plugin to develop packages locally against a project: link packages, update with them into composer.local.lock, and install from it.
Package info
github.com/wecodemore/composer-linked-packages
Type:composer-plugin
pkg:composer/wecodemore/composer-linked-packages
Requires
- php: >= 7.4 <= 8.6
- composer-plugin-api: ^2.9
Requires (Dev)
- composer/composer: ^2.9.8
- phpcompatibility/php-compatibility: ^10@dev
- phpstan/phpstan: ^2.3.7
- phpunit/phpunit: ^9.6.39
- syde/phpcs: ^1.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A Composer plugin to solve local development with Composer.
Installation
The recommended way is to install the plugin globally and allow it globally:
composer global config allow-plugins.wecodemore/composer-linked-packages true
composer global require wecodemore/composer-linked-packages
Because both .linked-packages.json and composer.local.lock are meant to be VCS-ignored, when using Git the suggestion is to add them both to the global Git ignore file (by default at ~/.config/git/ignore).
Quick Start
- Link the packages you work on
cd ~/code/acme-foo composer link
- Use the linked packages in the project
cd ~/code/my-project # this is the project that requires acme-foo composer use-links # this is executed once per project composer update
Now my-project will install acme-foo, symlinking it from its local path. my-project's composer.json and composer.lock are untouched.
Workflow
composer use-linksgenerates or updates a.linked-packages.json, which lists the packages to be linked and their paths.composer update, in the presence of a.linked-packages.json, besides installing the linked packages, generates acomposer.local.lockfile, so the defaultcomposer.lockis not messed with. The first time,composer.local.lockis generated from a copy ofcomposer.lock(if it exists). Subsequent updates work oncomposer.local.lock, unless.linked-packages.jsonchanges.- When
composer.local.lockexists,composer installautomatically installs from it, without asking for confirmation or offering options. It only emits an info message saying it is using the local lock. composer require/removekeep working oncomposer.jsonandcomposer.lockas usual, and do not update the local lock. This means that after arequire/remove, an explicitcomposer updateis required to update both the vendor folder andcomposer.local.lock. When working with linked packages,composer require <packages> --no-install && composer updateis preferable and faster.- When a
.linked-packages.jsonexists, there's no flag to ignore linked packages oninstallorupdate. Deleting the file is always an option, but for a less permanent approach, it is possible to runuse-links --disable. After that, bothinstallandupdatewill behave as if this plugin were not active, ignoring linked packages. Please note that this command does not change the vendor folder content, so aninstallorupdatemight be needed to reflect the setting.
Commands Reference
link
Adds a package to the list of available links in the global (or provided) local repository. To be executed in the package's root folder (or wherever its composer.json is). Linking an already linked package replaces the link.
composer link [--as-version=VERSION] [--linked-repo-path=DIR]
--as-version=VERSION: the version to install the package as, e.g.1.x-devor2.0.x-dev.--linked-repo-path=DIR: the directory holdinglinked-packages.json. Defaults to the Composer home directory.
unlink
Removes a package from the list of available links in the global (or provided) local repository. To be executed in the package's root folder (or wherever its composer.json is).
composer unlink [--linked-repo-path=DIR]
use-links
Makes the project in the current directory use the linked packages by writing .linked-packages.json. Running it again replaces the file, unless --add is used.
composer use-links [--linked-repo-path=DIR]|[--add] [PACKAGES...] composer use-links --disable|--enable composer use-links --purge-links [--linked-repo-path=DIR]
PACKAGES: only uses the linked packages matching these names. Wildcards are supported. Without them, all the linked packages are used. It fails if no linked package matches.--linked-repo-path=DIR: uses the links stored in a directory other than the Composer home directory.--addcan be used to add new linked packages instead of replacing the file content on each run. This is not compatible with--linked-repo-path, which always replaces the file.--disablecan be used as the only flag to temporarily disable linked packages for the project. That is equivalent to deleting the.linked-packages.jsonfile. Note that the command does not update the vendor folder content. Also note that, when disabled, a newuse-linkscall re-creates the.linked-packages.jsonfile (unless--addis used), but as disabled. Only an explicit--enablecall (or a manual edit) can re-enable a disabled configuration.--enableis used to enable linked packages after a--disable.--purge-linksremoves from the global (or provided via--linked-repo-path) repository the links whose directory does not exist anymore, for example because the package was deleted or moved without runningunlink. It lists them and asks for confirmation, defaulting to yes. In non-interactive mode (-n) it removes them without asking. It can only be combined with--linked-repo-path, and it does not need a project, so it can run from any directory.
require-link
Adds entries to config.linked-packages.require to add or override a linked-package requirement. Similarly to how require updates composer.lock, it will update composer.local.lock and will update dependencies after updating the requirement, unless the --no-install or --no-update flags are used. It requires linked packages to be in use, that is, a .linked-packages.json file must exist (it is created by use-links).
composer require-link PACKAGE[:VERSION] \ [--no-update] [--no-install] \ [--prefer-stable] [--prefer-lowest] [--minimal-changes] \ [--update-no-dev] [--update-with-dependencies|-w] [--update-with-all-dependencies|-W] \ [--ignore-platform-reqs] [--ignore-platform-req] [--no-progress] [--sort-packages] \ [--optimize-autoloader|-o] [--classmap-authoritative|-a] \ [--apcu-autoloader] [--apcu-autoloader-prefix] \ [--no-audit] [--audit-format] [--no-security-blocking] [--no-blocking] [--no-cooldown]
--sort-packageswill sort packages inconfig.linked-packages.requireand will not touch packages in top-levelrequireorrequire-dev.
For all other flags, please refer to the composer require documentation.
remove-link
Removes entries from config.linked-packages.require. Similarly to how remove updates composer.lock, it will update composer.local.lock and will update dependencies after updating the requirement, unless the --no-install or --no-update flags are used. It requires linked packages to be in use, that is, a .linked-packages.json file must exist (it is created by use-links).
composer remove-link PACKAGE \ [--no-update] [--no-install] \ [--update-no-dev] [--update-with-dependencies|-w] [--update-with-all-dependencies|-W] \ [--ignore-platform-reqs] [--ignore-platform-req] [--minimal-changes] \ [--optimize-autoloader|-o] [--classmap-authoritative|-a] \ [--apcu-autoloader] [--apcu-autoloader-prefix] \ [--no-audit] [--audit-format] [--no-security-blocking] [--no-blocking] [--no-cooldown] \ [--no-progress] [--sort-packages]
For all flags, please refer to the composer remove documentation.
Learn more
Filter linked packages
When calling composer use-links, all the packages required in the project that also exist as linked packages on the current system will be pulled from their local paths. Chances are we are interested in linking only a subset of those. This is done by passing the wanted packages as arguments to use-links. The Composer convention for filtering packages applies here: we can list explicit package names or use wildcards. For example:
composer use-links "foo/bar" "acme/*"
Version resolution
By default, the only thing the plugin does with regard to resolving linked packages is force dev stability for them. For example, in a project with "minimum-stability": "stable", a dependency required with ^1 would normally install only stable versions. If that package is linked, this plugin will, at runtime, set its stability to dev, which is equivalent to requiring it with ^1@dev. This allows installing the linked package from a local in-development branch, which is the entire point of linked packages. The overall minimum-stability must still be satisfied, including by the linked packages' transitive dependencies.
That said, when linking a package, it is possible to override the version Composer "sees" for it. This is done via the --as-version flag of the link command. For example:
composer link --as-version="1.x-dev"
With the above, Composer will consider the code in the path as representing that version, regardless of the package's VCS references or its branch-alias configuration.
Alternative repository location
By default, when linking a package, the information about the link is stored in a file located in the Composer global home. Via the --linked-repo-path flag of the link command, it is possible to use an alternative location. This means that multiple "linked packages repositories" can exist on a local system. For example, we might have two repositories, for personal and company projects, or even one repository per project.
When an alternative location is used to link packages, the same flag must be used for use-links or the consuming package will keep searching for linked packages in the global repository.
Combined with the --as-version flag mentioned above, this means we can have the same source code listed in multiple local repositories, with different versions.
Requiring packages only when linked
At times, it might be desirable to require a package only when working locally, or to require a package that is not released at all, and so can't be required in composer.json. This plugin makes this possible via configuration in composer.json, for example:
{
"config": {
"linked": {
"require": {
"acme/debug-tools": "~1.0.0",
"acme/unreleased-plugin": "dev-master"
}
}
}
}
With the configuration above, a regular update or install will not install the two packages. They will be installed only when a .linked-packages.json is there. Not only that, these packages must be linked, even if they are available on Packagist or in any other repository for the project.
To add and remove these requirements, the plugin provides the require-link and remove-link commands, which work similarly to Composer's require and remove, including most of their flags. A notable difference is that if no version constraint is provided, require-link, unlike require, uses * as the version constraint.
Copy instead of symlink
By default, Composer symlinks path repositories in the vendor folder, and falls back to copying if symlinking is not possible. This plugin uses path repositories for linked packages and lets Composer use its default mechanism.
However, when manually declaring a path repository, Composer supports an options.symlink setting to disable symlinking and force copying. This plugin exposes the config.linked-packages.symlink setting for the same purpose. This can be a boolean applying to all linked packages, or a map of package names (wildcards supported) to booleans. In the latter case, packages not listed get the default behavior (symlink if possible, copy otherwise).
Gotchas
- This plugin uses the Composer locker to write the lock, so when the
config.locksetting is set tofalse, Composer will install linked packages but will not generate acomposer.local.lock. In the absence of the local lock,composer installwill not use the linked packages. In short, withconfig.lock = false, installing linked packages always requiresupdate. - The plugin uses
configfor its own settings becauseextra, which is what plugins should use, is included in thecomposer.lockcontent hash. This means that ifextrawere used, other users of a committedcomposer.lockwould likely see a "lock file not in sync with composer.json" warning, even though nothing is actually out of sync, considering they don't have linked packages. - When the directory of a linked package is deleted or moved without running
unlink, its link stays in the repository. Projects using links keep working as long as they don't require that package: its directory is only read when the package is needed. When it is required,updatefails, saying the directory does not exist. Link the package again from its new location, or remove the links of missing directories withcomposer use-links --purge-links. - The
--bump-after-updateflag and thebump-after-updateconfig are ignored when updating with linked packages, because we don't want changes to the local lock file to have any effect oncomposer.json. Thebumpcommand still works, of course, but it bumps requirements in the defaultcomposer.jsonbased on the defaultcomposer.lock, and when using linked packages, a subsequentcomposer updatemight be needed for the installed packages to reflect the changes.
Development
composer install composer qa
Requirements
- Composer 2.9 or newer
- PHP 7.4 or newer
License
MIT, see LICENSE.