Skip to content

🧪 RequireDryRun Simulate adding a package dependency

🔍 RequireDryRun simulates adding a specified dependency package to the project without actually modifying composer.json or installing the package. It is equivalent to running composer require --dry-run packageName:version, suitable for previewing the dependency resolution result and potential conflicts before performing an addition.

📋 Signature

go
func (c *Composer) RequireDryRun(packageName string, version string) (string, error)

📥 Parameters

ParameterTypeDescription
packageNamestringThe name of the package to simulate adding, e.g. symfony/console
versionstringVersion constraint, e.g. ^5.0; pass an empty string "" to use the latest version

📤 Return value

Return valueTypeDescription
First return valuestringThe text output of the simulated addition process
Second return valueerrorReturned when an error occurs during the simulation; nil on success

📝 Example

go
package main

import (
	"fmt"
	"log"

	"github.com/scagogogo/composer-skills/pkg/composer"
)

func main() {
	comp, err := composer.NewComposer()
	if err != nil {
		log.Fatalf("failed to create Composer instance: %v", err)
	}

	// Simulate adding a specific version
	output, err := comp.RequireDryRun("symfony/console", "^5.0")
	if err != nil {
		log.Fatalf("simulate addition failed: %v", err)
	}
	fmt.Println("Simulated addition result:")
	fmt.Println(output)

	// Simulate adding the latest version
	output, err = comp.RequireDryRun("monolog/monolog", "")
	if err != nil {
		log.Fatalf("simulate addition failed: %v", err)
	}
	fmt.Println(output)
}

🚀 Advanced

  • ✅ After the dry run passes, use RequirePackage(packageName, version, dev) to perform the real addition; to carry custom options (e.g. --prefer-source, --no-update), use RequirePackageWithOptions and add "dry-run": "" to options for an equivalent effect.
  • 🧪 Run RequireDryRun before introducing a new dependency to catch version conflicts or unresolvable constraints early, avoiding polluting composer.json.
  • 📦 To simulate adding multiple packages at once, refer to RequireMultiple and add "dry-run": "" to the options.
  • 🔗 Under the hood, based on whether version is empty, it concatenates package:version or just package, then calls c.Run("require", "--dry-run", ...).

Released under the MIT License