🔒 LockPackageVersion
Locks the specified package to an exact version. Equivalent to calling UpdatePackageVersion with an ExactVersion constraint; it writes the version back to composer.json via the require command.
When to use
Use this when you need to guarantee that a dependency installs the same version in any environment (e.g. for reproducible builds before release). After locking, the constraint is an exact form like 1.2.3, rather than ^1.2.3.
Signature
go
func (c *Composer) LockPackageVersion(packageName string, version string) errorParameters
| Parameter | Type | Description |
|---|---|---|
packageName | string | Package name, e.g. symfony/console |
version | string | The exact version to lock to, e.g. 5.4.0 |
Return value
| Return value | Type | Description |
|---|---|---|
| Sole return value | error | Returned when the require command or write-back fails |
Example
go
package main
import (
"fmt"
"log"
"github.com/scagogogo/composer-skills/pkg/composer"
)
func main() {
comp, err := composer.New(composer.DefaultOptions())
if err != nil {
log.Fatalf("init failed: %v", err)
}
if err := comp.LockPackageVersion("symfony/console", "5.4.0"); err != nil {
log.Fatalf("lock version failed: %v", err)
}
fmt.Println("symfony/console locked to 5.4.0")
}Advanced
- This method is a shortcut for UpdatePackageVersion with
ExactVersion. - For other constraint forms (
^,~, ranges, wildcards), use UpdatePackageVersion. - See FormatVersionConstraint for the constraint formatting logic.