UnexpectedValueException Could not parse version constraint mybranch: Invalid version string "mybranch" - php

I'm trying to develop a PHP library (called foo/bar) using Composer in dir /work/a with the composer.json contents:
{
"name": "foo/bar",
"require": {
"php": ">=7.2"
}
}
/work/a is a git project and I'm on the branch mybranch
I'm trying to use this lib in another project locally (called testing/foobar) using Composer in dir work/b with the composer.json contents:
{
"name": "testing/foobar",
"type": "project",
"repositories": [
{
"type": "vcs",
"url": "/work/a"
}
],
"require": {
"php": "^7.4",
"foo/bar": "mybranch"
}
}
When running $ composer install in /work/b I get the error:
[UnexpectedValueException]
Could not parse version constraint mybranch: Invalid version string "mybranch"

You have to prefix your branch name with dev-, so your branch name will have to be dev-mybranch.
Loading a package from a VCS repository
...
In composer.json, you should prefix your custom branch name with "dev-".
...
Also check this Q/A "Composer require branch name".
Change branch name to have dev- prefix, add it to /work/b project:
{
"name": "testing/foobar",
"type": "project",
"repositories": [
{
"type": "vcs",
"url": "/work/a"
}
],
"require": {
"php": "^7.4",
"foo/bar": "dev-mybranch"
}
}
Run composer install:
❯ composer install
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing foo/bar (dev-mybranch 85c97b7): Cloning 85c97b7b23 from cache
Writing lock file
Generating autoload files

Related

Composer does not find my forked repository declared on a custom package's "repository" settings

I'm currently developing my own composer packages, but they failed trying to execute composer update.
This is my root composer.json:
{
...
"repositories": {
"bar": {
"type": "path",
"url": "packages/pinnokkio/bar",
"options": {
"symlink": true
}
}
},
"require": {
"php": "^8.0",
...
"pinnokkio/bar": "#dev"
},
...
}
This means, package pinnokkio/bar should be installed. And package pinnokkio/bar requires pinnokkio/foo as a dependency.
composer.json of pinnokkio/bar:
{
"name": "pinnokkio/bar",
...
"repositories": {
"foo": {
"type": "path",
"url": "../foo",
"options": {
"symlink": true
}
}
},
"require": {
"pinnokkio/foo": "#dev"
},
...
}
composer.json of pinnokkio/foo:
{
"name": "pinnokkio/foo",
"require": {
"php": "^8.0"
},
...
}
All in all, in my main composer.json, I'm going to require a package that requires pinnokkio/foo to run. And further upcoming packages all require pinnokkio/foo in order to run, but unfortunately, I'm getting this error trying to execute composer update. All packages are located in <root>/packages/:
Problem 1
- pinnokkio/bar[dev-feature-modules, dev-master] require pinnokkio/foo#dev -> could not be found in any version, there may be a typo in the package nam
e.
- Root composer.json requires pinnokkio/bar#dev -> satisfiable by pinnokkio/bar[dev-feature-modules, dev-master].
The repositories key is only read on the root composer.json.
As explained on the docs:
Repositories are not resolved recursively. You can only add them to your main composer.json. Repository declarations of dependencies' composer.jsons are ignored.
Resolving repositories recursively would be problematic. If multiple packages declared different repositories that included the same package, how would composer know which to use?
Also, this would open the door for dependencies hijacking your system by providing alternate repositories for known packages, and you'd end up installing untrusted packages in your system.
If you are installing multiple packages from a custom repositories, all the custom repositories need to be declared on the root configuration file.

Configuring class autoloading in composer

there is a project on Github that works stably. Below is a composer file:
{
"name": "wnull/userbars-warface-generator",
"description": "Simple and free library to generate userbars game Warface",
"keywords": [
"wf",
"warface",
"generator",
"userbars"
],
"type": "library",
"license": "MIT",
"require": {
"php": ">=7.1"
},
"autoload": {
"psr-4": {
"WF\\": "src/WF"
}
}
}
The problem is that after each successful installation of the project through the composer, you have to do the following command:
>>> composer dump-autoload -o
After that, all all classes will work correctly. Log from the console:
C:\hangry>composer require wnull/userbars-warface-generator
Using version ^1.0 for wnull/userbars-warface-generator
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing wnull/userbars-warface-generator (v1.0): Loading from cache
Writing lock file
Generating autoload files
C:\hangry>
C:\hangry>composer dump-autoload -o
Generated optimized autoload files containing 6 classes
Question: how do I avoid entering this command every time, and everything worked correctly during normal installation?
The problem was solved by replacing the PSR-4 to classmap. Thanks #MagnusEriksson.
Composer file:
{
"name": "wnull/userbars-warface-generator",
"description": "Simple and free library to generate userbars game Warface",
"keywords": [
"wf",
"warface",
"generator",
"userbars"
],
"type": "library",
"license": "MIT",
"require": {
"php": ">=7.1"
},
"autoload": {
"classmap": ["src/WF"]
}
}

How to force Composer to download a local package?

Let's say we have the following directory structure, we assume my-package also exists on Packagist:
- apps
\_ my-app
\_ composer.json
- packages
\_ my-package
\_ composer.json
To add my/package as a dependency of my-app, the documentation states we can use the following configuration:
{
"repositories": [
{
"type": "path",
"url": "../../packages/my-package"
}
],
"require": {
"my/package": "*"
}
}
However when I composer update, the dependency is still downloaded from Packagist. So, to see, I disabled Packagist.org:
{
"repositories": [
{
"type": "path",
"url": "../../packages/my-package",
"packagist.org": false
}
],
"require": {
"my/package": "*"
}
}
I cleared the cache with composer clearcache, removed my/package with composer remove my/package and installed it again with composer require my/package --prefer-source (I didn't understand if --prefer-source is for vcs only). The downloaded package is still not the local one. How to force composer to use the local one?
"require": {
"my/package": "*"
}
In case of VCS or path repository types, you need to specify version of the package you request. So instead of using *, as you have currently, use #dev:
"require": {
"my/package": "#dev"
}
For
composer --version
Composer version 2.0.14
in composer.json file
"require": {
"my/package": "dev-master"
}

Composer - Forked Laravel 4.2 not installing

I have cloned the Laravel 4.2 branch from Github and pushed it to a private GitLab server. I've created a new branch from 4.2 with the name dev-bugfix and added a comment in 1 file to see if composer would install my fork and not the official Laravel.
My steps:
Cloned Laravel 4.2 branch from Github
Pushed the repo to a private GitLab server
In an existing Laravel application, removed composer.lock, ran composer dump-autoload and removed the entire vendor folder
Edited composer.json to include my private repo:
"repositories": [{
"type": "package",
"package": {
"version": "dev-bugfix",
"name": "laravel/framework",
"source": {
"url": "my-gitlab-repo",
"type": "git",
"reference": "dev-bugfix"
}
}
}],
"require": {
"laravel/framework": "dev-bugfix",
"barryvdh/laravel-debugbar": "~1.8"
},
Ran composer install
Composer starts with cloning my fork of Laravel-framework after which it installs a few dependencies. Then, Artisan wants to clean compiled, where it fails. Complete output click
What am I missing? What am I doing wrong?
You need to install patchwork/utf8 package.
In the require section in your composer.json add: "patchwork/utf8": "1.2.*" and then do composer update.

Unable to create composer package - InvalidArgumentException

I have been trying to create a simple composer package. But I'm stuck with the following issue. Dont know how to resolve this.
[InvalidArgumentException]
Could not find package sarav/sample-package at any version for your minimum-stability (stable). Check the package spelling or your minimum-stability
Package Url : https://packagist.org/packages/sarav/sample-package
I ran the following composer command
composer require sarav/sample-package
My composer contents
{
"name": "sarav/sample-package",
"description": "Sample package",
"type": "Library",
"license": "MIT",
"authors": [
{
"name": "Sarav",
"email": "me#sarav.co"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=5.3.0"
},
"autoload": {
"psr-0": {
"Sarav": "src/"
}
}
}
Your package config looks good to me, but your Git repo doesn't have any tagged versions.
Use git tag v1.0.0 or whatever version is appropriate, then git push origin --tags to update on GitHub.
Alternatively, you can go without tagged versions by specifying the master branch when you require the package, like so:
composer require sarav/sample-package dev-master
You can require any branch in this manner, but the dev- prefix is mandatory.

Categories