Requesting specific tagged version for locally developed composer package

Viewed 21

I am developing a package for a Laravel project on my local machine. I have also spun up a Laravel app so I can manually test the package. My package is located at /home/me/packages/me/my-package and a commit (git) has been tagged with '0.1'.

I want to be able to switch between tagged versions and use specific versions in different projects but having issues.

In my main apps composer file, I am requiring the package like so:

...
"require" : {
    "me/my-package" : "0.1"
}
...

"repositories" : [
    {
        "type": "path",
        "url": "/home/me/packages/me/my-package"
    }
]

This results in an error:

Problem 1
    - Root composer.json requires me/my-package 0.1, found me/my-package[dev-main] but it does not match the constraint.

I have also tried:

"require" : {
    "me/my-package" : "dev-main#0.1"
}

(This was an idea taken from How to use a specific tag/version with composer and a private git repository?). This goes through without any errors but:

$ composer show | grep me/my-package

me/my-package dev-main     My Package

What is the correct way install a specific version of a package when developing it locally?

1 Answers

Probably the only thing why you hit this message is that you have "type": "path" and not "type": "vcs".

This is that Composer will only refer one version and only one version dev-main. The reason is:

If the package [path repository] is a local VCS repository, the version may be inferred by the branch or tag that is currently checked out. (ref)

You have the main branch checked out at /home/me/packages/me/my-package (/home/me/packages/me/my-package/.git/HEAD content is ref: refs/heads/main and /home/me/packages/me/my-package/.git/refs/heads/main points to the git revision) and composer will only take that one.

You should have no problem to make that change from path to vcs given:

  • You already have a (git) repository at /home/me/packages/me/my-package (looks so by your question)
  • You know the absolute path on your local system to that repository (again, looks so by your question: /home/me/packages/me/my-package).

Given these two points, Composer is able to obtain the VCS tagged versions from that path. So basically only the change of the "type":

"repositories" : [
    {
        "type": "vcs",
        "url": "/home/me/packages/me/my-package"
    }
]

Just take care that "url" contains the absolute path (and there is a git repository at that place). Likely already all set in your case, just saying.

Git is very prominent that's why I mentioned it here, for other types of VCS Composer also has options at hand. The details - also for git etc. - are available here:

Related