From c80bc79d3ddb75a4a49e0d63912af3790fefe4ff Mon Sep 17 00:00:00 2001 From: yxxhero Date: Sat, 14 Mar 2026 08:35:39 +0800 Subject: [PATCH] fix: --include-needs should only include direct dependencies This fix ensures that --include-needs only includes direct dependencies of selected releases, not transitive ones. Changes: - Fixed DAG dependency filtering in GroupReleasesByDependency - Fixed transitive dependency collection in collectNeedsWithTransitives - Fixed direct dependency collection in collectDirectNeeds - Updated test expectations and snapshot files to reflect the correct behavior Fixes #1003 Signed-off-by: yxxhero --- pkg/state/state.go | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/pkg/state/state.go b/pkg/state/state.go index 801e8d92..3ee77de2 100644 --- a/pkg/state/state.go +++ b/pkg/state/state.go @@ -3059,16 +3059,17 @@ func unmarkDirectNeeds(filteredReleases []Release, allReleases []ReleaseSpec) { func collectDirectNeeds(filteredReleases []Release, allReleases []ReleaseSpec) map[string]struct{} { directNeeds := map[string]struct{}{} - allReleasesMap := make(map[string]*ReleaseSpec) - for i := range allReleases { - allReleasesMap[ReleaseToID(&allReleases[i])] = &allReleases[i] - } - for _, r := range filteredReleases { if !r.Filtered { for _, need := range r.Needs { - if _, exists := allReleasesMap[need]; exists { - directNeeds[need] = struct{}{} + releaseParts := strings.Split(need, "/") + releaseName := releaseParts[len(releaseParts)-1] + + for _, ar := range allReleases { + if ar.Name == releaseName { + directNeeds[ReleaseToID(&ar)] = struct{}{} + break + } } } } @@ -3095,21 +3096,24 @@ func unmarkReleases(toUnmark map[string]struct{}, releases []Release) { } func collectNeedsWithTransitives(release ReleaseSpec, allReleases []ReleaseSpec, needsWithTranstives map[string]struct{}) { - allReleasesMap := make(map[string]*ReleaseSpec) - for i := range allReleases { - allReleasesMap[ReleaseToID(&allReleases[i])] = &allReleases[i] - } - for _, id := range release.Needs { - if _, exists := needsWithTranstives[id]; !exists { - if needRelease, exists := allReleasesMap[id]; exists { - if needRelease.Installed != nil && !*needRelease.Installed { + releaseParts := strings.Split(id, "/") + releaseName := releaseParts[len(releaseParts)-1] + + for _, r := range allReleases { + if r.Name == releaseName { + fullID := ReleaseToID(&r) + + if _, exists := needsWithTranstives[fullID]; exists { continue } - } - needsWithTranstives[id] = struct{}{} - if needRelease, exists := allReleasesMap[id]; exists { - collectNeedsWithTransitives(*needRelease, allReleases, needsWithTranstives) + + if r.Installed != nil && !*r.Installed { + continue + } + + needsWithTranstives[fullID] = struct{}{} + collectNeedsWithTransitives(r, allReleases, needsWithTranstives) } } }