package main

import (
	
	
)

// upgradeModules recursively updates dependencies in the workspace.
func ( *workspace,  *state) (*state, error) {
	 := make(map[string]downloadedModule)
	for  := 1; ; ++ {
		log("iteration " + strconv.Itoa())
		var  []string
		for ,  := range .Modules {
			if .Main {
				continue
			}
			 = append(, .Path)
		}
		if len() == 0 {
			return , nil
		}

		log("looking for upgrades")
		,  := queryUpgradesCached(.Root(), , )
		if  != nil {
			return nil, 
		}
		var  []downloadedModule
		for ,  := range  {
			,  := .ModuleByPath[.Path]
			if ! {
				continue
			}
			if .Version == .Version {
				continue
			}
			log(.Path + "@" + .Version + " => " + .Version)
			 = append(, )
		}
		if len() == 0 {
			return , nil
		}
		log("found upgrades: " + strconv.Itoa(len()))

		 := make([]string, len())
		for ,  := range  {
			[] = .Path + "@" + .Version
		}

		log("updating require directives")
		for ,  := range .Paths {
			if  := addGoModRequires(, );  != nil {
				return nil, 
			}
		}

		log("syncing workspace")
		if  := syncWorkspace();  != nil {
			return nil, 
		}

		if ,  = loadState(.Root(), "all");  != nil {
			return nil, 
		}

		// Run go work sync again in case entries we do not actually
		// need were added to go.work.sum.
		if  := goworksync();  != nil {
			return nil, 
		}
	}
}

func ( string,  []string,  map[string]downloadedModule) ([]downloadedModule, error) {
	var  []string

	 := make(map[string]downloadedModule)
	for ,  := range  {
		if ,  := [];  {
			[] = 
			continue
		}
		 = append(, )
	}

	if len() != 0 {
		,  := queryUpgrades(, )
		if  != nil {
			return nil, 
		}
		for ,  := range  {
			[.Path] = 
			[.Path] = 
		}
	}

	 := make([]downloadedModule, 0, len())
	for ,  := range  {
		,  := []
		if ! {
			continue
		}
		 = append(, )
	}
	return , nil
}

func ( string,  []string) error {
	 := []string{"mod", "edit"}
	 := make([]string, len()+len())
	_ = copy(, )
	for ,  := range  {
		[+len()] = "-require=" + 
	}

	 := exec.Command("go", ...)
	.Dir = 
	return .Run()
}