Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
walkowif committed Dec 12, 2023
1 parent 98a164d commit 13752bb
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 18 deletions.
88 changes: 70 additions & 18 deletions cmd/construct.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ import (
"strings"
)

const lowestPossiblePackageVersion = "0.0.0.0"

// ConstructOutputPackageList generates a list of all packages and their dependencies
// which should be included in the output renv.lock file,
// based on the list of package descriptions, and information contained in the PACKAGES files.
func ConstructOutputPackageList(packages []PackageDescription, packagesFiles map[string]PackagesFile,
repositoryList []string, allowedMissingDependencyTypes []string) []PackageDescription {
var outputPackageList []PackageDescription
fatalErrors := make(map[string]bool)
nonFatalErrors := make(map[string]bool)
fatalMissingPackageVersions := make(map[string]DependencyVersion)
nonFatalMissingPackageVersions := make(map[string]DependencyVersion)
// Add all input packages to output list, as the packages should be downloaded from git repositories.
for _, p := range packages {
outputPackageList = append(outputPackageList, PackageDescription{
Expand All @@ -46,25 +48,38 @@ func ConstructOutputPackageList(packages []PackageDescription, packagesFiles map
ResolveDependenciesRecursively(
&outputPackageList, d.DependencyName, d.VersionOperator,
d.VersionValue, d.DependencyType, allowedMissingDependencyTypes,
repositoryList, packagesFiles, 1, fatalErrors, nonFatalErrors,
repositoryList, packagesFiles, 1, fatalMissingPackageVersions,
nonFatalMissingPackageVersions,
)
}
}
}
}
var fatalErrorsString string
for fe, _ := range fatalErrors {
fatalErrorsString += fe
errorsString := "Packages not found in any repository:\n"
for packageName, versionConstraint := range nonFatalMissingPackageVersions {
var versionConstraintString string
if versionConstraint.VersionValue != lowestPossiblePackageVersion {
versionConstraintString = (" " +
versionConstraint.VersionOperator + " " +
versionConstraint.VersionValue)
}
errorsString += packageName + versionConstraintString + "\n"
}
if fatalErrorsString != "" {
log.Fatal(fatalErrorsString)
if len(errorsString) > len("Packages not found in any repository:\n") {
log.Error(errorsString)
}
var nonFatalErrorsString string
for nfe, _ := range nonFatalErrors {
nonFatalErrorsString += nfe
errorsString = "Packages not found in any repository:\n"
for packageName, versionConstraint := range fatalMissingPackageVersions {
var versionConstraintString string
if versionConstraint.VersionValue != lowestPossiblePackageVersion {
versionConstraintString = (" " +
versionConstraint.VersionOperator + " " +
versionConstraint.VersionValue)
}
errorsString += packageName + versionConstraintString + "\n"
}
if nonFatalErrorsString != "" {
log.Error(nonFatalErrorsString)
if len(errorsString) > len("Packages not found in any repository:\n") {
log.Fatal(errorsString)
}
return outputPackageList
}
Expand All @@ -76,7 +91,8 @@ func ConstructOutputPackageList(packages []PackageDescription, packagesFiles map
func ResolveDependenciesRecursively(outputList *[]PackageDescription, name string, versionOperator string,
versionValue string, dependencyType string, allowedMissingDependencyTypes []string,
repositoryList []string, packagesFiles map[string]PackagesFile, recursionLevel int,
fatalErrors map[string]bool, nonFatalErrors map[string]bool) {
fatalMissingPackageVersions map[string]DependencyVersion,
nonFatalMissingPackageVersions map[string]DependencyVersion) {
var indentation string
for i := 0; i < recursionLevel; i++ {
indentation += " "
Expand Down Expand Up @@ -118,7 +134,8 @@ func ResolveDependenciesRecursively(outputList *[]PackageDescription, name strin
ResolveDependenciesRecursively(
outputList, d.DependencyName, d.VersionOperator, d.VersionValue,
d.DependencyType, allowedMissingDependencyTypes, repositoryList,
packagesFiles, recursionLevel+1, fatalErrors, nonFatalErrors,
packagesFiles, recursionLevel+1, fatalMissingPackageVersions,
nonFatalMissingPackageVersions,
)
}
}
Expand All @@ -128,17 +145,52 @@ func ResolveDependenciesRecursively(outputList *[]PackageDescription, name strin
}
}
}
ProcessMissingPackage(
indentation, name, versionOperator, versionValue, dependencyType,
allowedMissingDependencyTypes, fatalMissingPackageVersions,
nonFatalMissingPackageVersions,
)
}

// ProcessMissingPackage saves information about missing packages (dependencies) and their versions.
// This information is later reported to the user, together with optionally exiting the application
// with failed status, depending on the types of missing dependencies and the configuration
// provided by --allowIncompleteRenvLock flag.
func ProcessMissingPackage(indentation string, packageName string, versionOperator string,
versionValue string, dependencyType string, allowedMissingDependencyTypes []string,
fatalMissingPackageVersions map[string]DependencyVersion,
nonFatalMissingPackageVersions map[string]DependencyVersion) {
var versionConstraint string
if versionOperator != "" && versionValue != "" {
versionConstraint = " (version " + versionOperator + " " + versionValue + ")"
} else {
versionOperator = ">="
versionValue = lowestPossiblePackageVersion
}
message := "Could not find package " + name + versionConstraint + " in any of the repositories.\n"
message := "Could not find package " + packageName + versionConstraint + " in any of the repositories.\n"
if stringInSlice(dependencyType, allowedMissingDependencyTypes) {
log.Warn(indentation + message)
nonFatalErrors[message] = true
val, ok := nonFatalMissingPackageVersions[packageName]
if !ok || (ok && !CheckIfVersionSufficient(val.VersionValue, versionOperator, versionValue)) {
// This is the first time we see this package as a missing dependency, or
// some other package already requires this missing dependency in a lower version
// and the currently processed package requires it in a higher version,
// so the current requirement is more important.
nonFatalMissingPackageVersions[packageName] = DependencyVersion{
versionOperator, versionValue,
}
log.Trace("Adding package ", packageName, " ", versionOperator, " ", versionValue, " to missing packages list.")
}
} else {
log.Error(indentation + message)
fatalErrors[message] = true
val, ok := fatalMissingPackageVersions[packageName]
if !ok || (ok && !CheckIfVersionSufficient(val.VersionValue, versionOperator, versionValue)) {
// See a comment above for explanation of this contidion.
fatalMissingPackageVersions[packageName] = DependencyVersion{
versionOperator, versionValue,
}
log.Trace("Adding package ", packageName, " ", versionOperator, " ", versionValue, " to missing packages list.")
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions cmd/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,8 @@ type Dependency struct {
VersionOperator string `json:"operator"`
VersionValue string `json:"value"`
}

type DependencyVersion struct {
VersionOperator string `json:"operator"`
VersionValue string `json:"value"`
}

0 comments on commit 13752bb

Please sign in to comment.