❓ WhyPackage
Explains why the specified package is installed in the project. Equivalent to composer why package/name.
When to use
- 🤔 You see an unfamiliar package in the dependency list and want to know who pulled it in.
- 🧹 Confirm whether other packages still depend on it before removing a package.
- 🐛 Troubleshoot "why a particular version of a transitive dependency was installed".
Signature
go
func (c *Composer) WhyPackage(packageName string) (string, error)Parameters
| Parameter | Type | Description |
|---|---|---|
packageName | string | Name of the package whose install reason to query |
Return value
| Value | Type | Description |
|---|---|---|
| Output | string | Output of the install reason, listing the dependency chain |
| Error | error | Returned when an error occurs during the query |
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)
}
output, err := comp.WhyPackage("symfony/polyfill-mbstring")
if err != nil {
log.Fatalf("query install reason failed: %v", err)
}
fmt.Println("install reason:", output)
}Advanced
WhyWithOptions
Use this when you need additional options (e.g. --recursive):
go
func (c *Composer) WhyWithOptions(packageName string, options map[string]string) (string, error)Reverse question
To find out "why a particular version cannot be installed", use WhyNotPackage; to find out "which packages depend on a package", use ShowReverseDependencies (composer depends).
Difference between why and depends
why answers "why does this package exist" and outputs the dependency chain; depends answers "who depends on it" and outputs reverse dependencies. The two perspectives are complementary.
🔍 Related methods
WhyNotPackage: explain why a specific version cannot be installed.ShowDependencyTree: view the full dependency tree.