🔍 Packages
This page covers methods in pkg/composer for single/multi-package operations, defined in packages.go, additional_methods.go, and result_types.go. They correspond to composer's require, remove, show, search, depends, why, why-not, outdated, bump, reinstall, browse, and other subcommands.
🎯 When to Use
- ➕ Add a new dependency to the project →
RequirePackage - ➖ Remove an unneeded dependency →
Remove - 🔎 View an installed package's version/dependencies/source location →
ShowPackageor structuredShowPackageInfo - 🌐 Find packages on Packagist →
Searchor structuredSearchInfo - 🌳 Understand dependency relationships between packages →
ShowDependencyTree/WhyPackage/ShowReverseDependencies - ⬆️ Find which packages can be upgraded →
OutdatedPackagesor structuredGetOutdatedInfo - 🚫 Troubleshoot why a version can't be installed →
WhyNotPackage
📋 Method Overview
| Method | Signature Summary | Equivalent Command |
|---|---|---|
➕ RequirePackage | RequirePackage(packageName, version string, dev bool) error | composer require [--dev] pkg:ver |
RequirePackageWithOptions | RequirePackageWithOptions(packageName, version string, options map[string]string) error | composer require [options] pkg:ver |
RequireMultiple | RequireMultiple(packages map[string]string, dev bool) error | composer require [--dev] pkg1:ver1 pkg2:ver2 ... |
🧪 RequireDryRun | RequireDryRun(packageName, version string) (string, error) | composer require --dry-run pkg:ver |
➖ Remove | Remove(packageName string, dev bool) error | composer remove [--dev] pkg |
RemoveWithOptions | RemoveWithOptions(packageName string, options map[string]string) error | composer remove [options] pkg |
RemoveMultiple | RemoveMultiple(packages []string, dev bool) error | composer remove [--dev] pkg1 pkg2 ... |
🧪 RemoveDryRun | RemoveDryRun(packageName string) (string, error) | composer remove --dry-run pkg |
🔎 ShowPackage | ShowPackage(packageName string) (string, error) | composer show pkg |
ShowPackageWithFormat | ShowPackageWithFormat(packageName, format string) (string, error) | composer show pkg --format=FMT |
✨ ShowPackageInfo | ShowPackageInfo(packageName string) (*PackageInfo, error) | composer show pkg --format=json (structured) |
ShowAllPackages | ShowAllPackages() (string, error) | composer show |
ShowDirectPackages | ShowDirectPackages() (string, error) | composer show --direct |
ShowSelfPackage | ShowSelfPackage() (string, error) | composer show --self |
ShowLatestVersions | ShowLatestVersions() (string, error) | composer show --latest |
ShowWithOptions | ShowWithOptions(options map[string]string) (string, error) | composer show [options] |
🌳 ShowDependencyTree | ShowDependencyTree(packageName string) (string, error) | composer show --tree [pkg] |
🔗 ShowReverseDependencies | ShowReverseDependencies(packageName string) (string, error) | composer depends pkg |
DependsWithOptions | DependsWithOptions(packageName string, options map[string]string) (string, error) | composer depends pkg [options] |
❓ WhyPackage | WhyPackage(packageName string) (string, error) | composer why pkg |
WhyWithOptions | WhyWithOptions(packageName string, options map[string]string) (string, error) | composer why pkg [options] |
🚫 WhyNotPackage | WhyNotPackage(packageName, version string) (string, error) | composer why-not pkg ver |
WhyNotWithOptions | WhyNotWithOptions(packageName, version string, options map[string]string) (string, error) | composer why-not pkg ver [options] |
⬆️ OutdatedPackages | OutdatedPackages() (string, error) | composer outdated |
OutdatedPackagesDirect | OutdatedPackagesDirect() (string, error) | composer outdated --direct |
OutdatedWithOptions | OutdatedWithOptions(options map[string]string) (string, error) | composer outdated [options] |
OutdatedWithFormat | OutdatedWithFormat(format string) (string, error) | composer outdated --format=FMT |
ShowOutdatedWithFormat | ShowOutdatedWithFormat(format string) (string, error) | composer outdated --format=FMT (alias) |
ShowOutdatedMinorOnly | ShowOutdatedMinorOnly() (string, error) | composer outdated --minor-only |
✨ GetOutdatedInfo | GetOutdatedInfo() (*OutdatedResult, error) | composer outdated --format=json (structured) |
✨ GetOutdatedInfoWithOptions | GetOutdatedInfoWithOptions(options map[string]string) (*OutdatedResult, error) | Same + options |
🌐 Search | Search(query string) (string, error) | composer search query |
SearchWithFormat | SearchWithFormat(query, format string) (string, error) | composer search query --format=FMT |
SearchOnlyName | SearchOnlyName(query string) (string, error) | composer search query --only-name |
SearchWithType | SearchWithType(query, packageType string) (string, error) | composer search query --type=TYPE |
✨ SearchInfo | SearchInfo(query string) (*SearchResult, error) | composer search query --format=json (structured) |
📈 BumpPackages | BumpPackages(packages []string) error | composer bump [packages...] |
BumpPackagesWithOptions | BumpPackagesWithOptions(packages []string, options map[string]string) error | composer bump [options] [packages...] |
🔁 Reinstall | Reinstall(packageName string) error | composer reinstall pkg |
ReinstallWithOptions | ReinstallWithOptions(packageName string, options map[string]string) error | composer reinstall [options] pkg |
ReinstallMultiple | ReinstallMultiple(packages []string) error | composer reinstall pkg1 pkg2 ... |
ReinstallMultipleWithOptions | ReinstallMultipleWithOptions(packages []string, options map[string]string) error | composer reinstall [options] pkg1 ... |
🌍 BrowsePackage | BrowsePackage(packageName string) error | composer browse pkg |
BrowsePackageWithOptions | BrowsePackageWithOptions(packageName string, options map[string]string) error | composer browse pkg [options] |
✨ marked methods return structured Go types instead of raw strings; see the "Structured Return Values" section below.
➕ RequirePackage
Add a new dependency package to the project, write it to composer.json, and install immediately.
Signature
func (c *Composer) RequirePackage(packageName string, version string, dev bool) errorParameters
| Parameter | Type | Description |
|---|---|---|
packageName | string | Package name, e.g., "symfony/console" |
version | string | Version constraint, e.g., "^5.0"; empty means latest version |
dev | bool | true adds as dev dependency (--dev) |
Return Values
| Type | Description |
|---|---|
error | On failure, returns error wrapping ErrRequirePackageFailed |
Example
// Add production dependency
err := comp.RequirePackage("symfony/console", "^5.0", false)
if err != nil {
log.Fatalf("Failed to add dependency: %v", err)
}
// Add dev dependency
err = comp.RequirePackage("phpunit/phpunit", "^9.0", true)Advanced
- To add multiple packages at once, use
RequireMultiple(map[string]string{"symfony/console": "^5.0", "monolog/monolog": "^2.0"}, false). - To dry-run without modifying
composer.json, useRequireDryRun(packageName, version). - For more options (like
--prefer-source,--no-update), useRequirePackageWithOptions.
➖ Remove
Remove the specified dependency package from the project.
Signature
func (c *Composer) Remove(packageName string, dev bool) errorParameters
| Parameter | Type | Description |
|---|---|---|
packageName | string | Package name to remove |
dev | bool | true removes from dev dependencies (--dev) |
Example
// Remove production dependency
err := comp.Remove("symfony/console", false)
// Remove dev dependency
err = comp.Remove("phpunit/phpunit", true)Advanced
- Batch remove with
RemoveMultiple([]string{"a/b", "c/d"}, false). - Dry-run with
RemoveDryRun(packageName).
🔎 ShowPackage / ✨ ShowPackageInfo
Show detailed info of a specified package (version, dependencies, install location, etc.).
Signatures
func (c *Composer) ShowPackage(packageName string) (string, error)
func (c *Composer) ShowPackageInfo(packageName string) (*PackageInfo, error)Parameters
| Parameter | Type | Description |
|---|---|---|
packageName | string | Package name to show info for |
Return Values
ShowPackage:(string, error)— composer raw text output; returnsErrShowPackageFailedon failure.ShowPackageInfo:(*PackageInfo, error)— parsed struct (internally executescomposer show pkg --format=json).
Example
// Raw text
output, err := comp.ShowPackage("symfony/console")
fmt.Println(output)
// Structured
info, err := comp.ShowPackageInfo("symfony/console")
if err != nil {
log.Fatalf("Failed to get package info: %v", err)
}
fmt.Printf("Package %s version %s\n", info.Name, info.Version)
fmt.Printf("Type: %s, Homepage: %s\n", info.Type, info.Homepage)
fmt.Printf("License: %v\n", info.License)Advanced
- For custom (non-json) output format, use
ShowPackageWithFormat(packageName, format). - To view all installed packages, use
ShowAllPackages(); for direct dependencies only, useShowDirectPackages().
🌐 Search / ✨ SearchInfo
Search for packages matching keywords on Packagist.
Signatures
func (c *Composer) Search(query string) (string, error)
func (c *Composer) SearchInfo(query string) (*SearchResult, error)Parameters
| Parameter | Type | Description |
|---|---|---|
query | string | Search keywords |
Return Values
Search:(string, error)— raw text; returnsErrSearchFailedon failure.SearchInfo:(*SearchResult, error)— structured result (internally executescomposer search query --format=json).
Example
// Structured search
res, err := comp.SearchInfo("logger")
if err != nil {
log.Fatalf("Search failed: %v", err)
}
for _, r := range res.Results {
fmt.Printf("%s: %s\n", r.Name, r.Description)
}Advanced
- For exact name match only, use
SearchOnlyName(query), reducing noise from descriptions. - For type filtering, use
SearchWithType(query, "composer-plugin"), supporting types likelibrary/composer-plugin/project. - For custom output format, use
SearchWithFormat(query, "json").
⬆️ OutdatedPackages / ✨ GetOutdatedInfo
Show all outdated packages in the project and available updates.
Signatures
func (c *Composer) OutdatedPackages() (string, error)
func (c *Composer) GetOutdatedInfo() (*OutdatedResult, error)
func (c *Composer) GetOutdatedInfoWithOptions(options map[string]string) (*OutdatedResult, error)Return Values
OutdatedPackages:(string, error)— raw text.GetOutdatedInfo:(*OutdatedResult, error)— structured result (internally executescomposer outdated --format=json).
Example
// Structured query of outdated packages
outdated, err := comp.GetOutdatedInfo()
if err != nil {
log.Fatalf("Failed to get outdated package info: %v", err)
}
for _, p := range outdated.Installed {
fmt.Printf("⬆️ %s: %s -> %s (%s)\n",
p.Name, p.Installed, p.Latest, p.LatestStatus)
}
fmt.Printf("Total %d outdated packages\n", outdated.Count)Advanced
- For direct dependencies only, use
OutdatedPackagesDirect(). - For minor version updates only, use
ShowOutdatedMinorOnly(). - For custom options, use
OutdatedWithOptions(map[string]string{"direct": "", "minor-only": ""})orGetOutdatedInfoWithOptions. - For custom format, use
OutdatedWithFormat("json")/ShowOutdatedWithFormat("text").
About Exit Codes
composer outdated may return non-zero exit code when there are outdated packages, but the output still contains valid JSON. GetOutdatedInfo handles this: when err != nil but output == "", it returns an empty result instead of an error.
🌳 ShowDependencyTree
Display a package's dependency relationships as a tree structure.
Signature
func (c *Composer) ShowDependencyTree(packageName string) (string, error)Parameters
| Parameter | Type | Description |
|---|---|---|
packageName | string | Package name; empty string shows the entire project's dependency tree |
Example
// Entire project's dependency tree
output, err := comp.ShowDependencyTree("")
// Specific package's dependency tree
output, err = comp.ShowDependencyTree("symfony/console")Advanced
- To parse the tree into Go structs, use
ParseDependencyTreeJSON(output)inparsing.go, which returns[]DependencyNode, each node hasName,Version,Children.
❓ WhyPackage / 🚫 WhyNotPackage / 🔗 ShowReverseDependencies
These three method groups are for understanding dependency relationships:
| Method | Signature | Equivalent Command | Purpose |
|---|---|---|---|
WhyPackage | WhyPackage(packageName string) (string, error) | composer why pkg | Explain why a package is installed (depended on by whom) |
WhyNotPackage | WhyNotPackage(packageName, version string) (string, error) | composer why-not pkg ver | Explain why a version can't be installed (conflict source) |
ShowReverseDependencies | ShowReverseDependencies(packageName string) (string, error) | composer depends pkg | Show which installed packages depend on this one |
Example
// Why is polyfill-mbstring installed?
why, _ := comp.WhyPackage("symfony/polyfill-mbstring")
fmt.Println("Install reason:", why)
// Why can't symfony/console v4.0.0 be installed?
whyNot, _ := comp.WhyNotPackage("symfony/console", "v4.0.0")
fmt.Println("Reason it can't be installed:", whyNot)
// Who depends on polyfill-mbstring?
deps, _ := comp.ShowReverseDependencies("symfony/polyfill-mbstring")
fmt.Println("Reverse dependencies:", deps)Advanced
- All three have
WithOptionsvariants:WhyWithOptions,WhyNotWithOptions,DependsWithOptions, accepting options likemap[string]string{"format": "json"}.
📈 BumpPackages
Upgrade specified packages to the latest version matching the version constraint in composer.json (doesn't change constraints, only updates lock versions). Requires Composer 2.4+.
Signatures
func (c *Composer) BumpPackages(packages []string) error
func (c *Composer) BumpPackagesWithOptions(packages []string, options map[string]string) errorParameters
| Parameter | Type | Description |
|---|---|---|
packages | []string | List of package names to bump; empty slice bumps all packages |
Example
// Bump multiple packages
err := comp.BumpPackages([]string{"symfony/console", "symfony/process"})
if err != nil {
log.Fatalf("Failed to bump packages: %v", err)
}
// Bump all packages
err = comp.BumpPackages([]string{})
// With options (dev-only + dry-run)
options := map[string]string{
"dev-only": "",
"prefer-stable": "",
"dry-run": "",
}
err = comp.BumpPackagesWithOptions([]string{"symfony/console"}, options)🔁 Reinstall
Reinstall specified packages using Composer 2.2+'s native reinstall command.
Signatures
func (c *Composer) Reinstall(packageName string) error
func (c *Composer) ReinstallWithOptions(packageName string, options map[string]string) error
func (c *Composer) ReinstallMultiple(packages []string) error
func (c *Composer) ReinstallMultipleWithOptions(packages []string, options map[string]string) errorExample
// Reinstall single package
err := comp.Reinstall("symfony/console")
// Reinstall with prefer-source
err = comp.ReinstallWithOptions("symfony/console", map[string]string{"prefer-source": ""})
// Batch reinstall
err = comp.ReinstallMultiple([]string{"symfony/console", "symfony/process"})Version Requirement
The reinstall command requires Composer 2.2 or higher. For older versions, use Remove + RequirePackage as a substitute.
🌍 BrowsePackage
Open the specified package's project page (usually the GitHub repository) in the default browser.
Signatures
func (c *Composer) BrowsePackage(packageName string) error
func (c *Composer) BrowsePackageWithOptions(packageName string, options map[string]string) errorExample
// Open package homepage
err := comp.BrowsePackage("symfony/console")
// Open docs page
err = comp.BrowsePackageWithOptions("symfony/console", map[string]string{"docs": ""})
// Open issue tracker page
err = comp.BrowsePackageWithOptions("symfony/console", map[string]string{"issues": ""})Environment Dependency
Requires OS support for opening a browser, and the package must declare a project URL in composer.json. Server environments without GUI are generally not applicable.
🧱 Structured Return Values
Structured methods for package operations return the following types (all defined in result_types.go):
PackageInfo (returned by ShowPackageInfo)
type PackageInfo struct {
Name string `json:"name"`
Version string `json:"version"`
Description string `json:"description,omitempty"`
Type string `json:"type,omitempty"`
Keywords []string `json:"keywords,omitempty"`
Homepage string `json:"homepage,omitempty"`
License []string `json:"license,omitempty"`
Authors []PackageAuthor `json:"authors,omitempty"`
Support map[string]string `json:"support,omitempty"`
Require map[string]string `json:"require,omitempty"`
RequireDev map[string]string `json:"require_dev,omitempty"`
Autoload map[string]interface{} `json:"autoload,omitempty"`
Source PackageSource `json:"source,omitempty"`
Dist PackageDist `json:"dist,omitempty"`
Abandoned interface{} `json:"abandoned,omitempty"` // bool or string
Time string `json:"time,omitempty"`
}OutdatedResult (returned by GetOutdatedInfo)
type OutdatedResult struct {
Installed []OutdatedPackage `json:"installed"`
Count int `json:"count,omitempty"`
}
type OutdatedPackage struct {
Name string `json:"name"`
Latest string `json:"latest"`
Installed string `json:"version"`
LatestStatus string `json:"latest_status"` // "semver-safe-update" | "update-possible" | "up-to-date"
Abandoned interface{} `json:"abandoned,omitempty"`
}SearchResult (returned by SearchInfo)
type SearchResult struct {
Results []SearchResultItem `json:"results"`
Total int `json:"total,omitempty"`
}
type SearchResultItem struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
URL string `json:"url,omitempty"`
Repository string `json:"repository,omitempty"`
}When to Use Structured Variants
When you need to programmatically check version numbers, count outdated packages, store search results in a database, or render to UI, definitely use ShowPackageInfo / GetOutdatedInfo / SearchInfo. Raw string methods are only suitable for human reading or log output.
🧭 Next Steps
- 📦 Dependencies —
Install/Update/DumpAutoload - 🛠️ Core Runtime —
Run/RunWithContext - 🧩 Convenience Queries — Higher-level wrappers like
IsPackageInstalled/GetDirectDependencyNames