@@ -41,11 +41,13 @@ type apmLockEntry struct {
4141
4242// apmNode is one lockfile entry with its identity and graph position.
4343type apmNode struct {
44- entry apmLockEntry
45- depth int
46- repo string
47- key string
48- dep * resolve.Dep
44+ entry apmLockEntry
45+ depth int
46+ folded bool // repository path is case-insensitive
47+ repo string // repo_url as APM loads it
48+ host string // host prefix of key, if any
49+ key string
50+ dep * resolve.Dep
4951}
5052
5153// parseAPMDir builds the graph from manifests' APM identities plus depth and resolved_by.
@@ -118,8 +120,7 @@ func apmNodes(entries []apmLockEntry) []*apmNode {
118120 if entry .RepoURL == "<self>" || depth == 0 {
119121 continue
120122 }
121- repo := apmRepo (entry )
122- nodes = append (nodes , & apmNode {entry : entry , depth : depth , repo : repo , key : apmLockKey (entry , repo )})
123+ nodes = append (nodes , newAPMNode (entry , depth ))
123124 }
124125 return nodes
125126}
@@ -128,11 +129,11 @@ func apmNodes(entries []apmLockEntry) []*apmNode {
128129func apmParent (nodes []* apmNode , child * apmNode ) (* apmNode , error ) {
129130 ref := child .entry .ResolvedBy
130131 // APM writes bare repo_urls for v1 and v2; a fuller key is exact when one appears.
131- explicit := apmCandidates (nodes , child , func (n * apmNode ) bool { return n .key == ref && n . repo != ref })
132+ explicit := apmCandidates (nodes , child , func (n * apmNode ) bool { return n .matchesKey ( ref ) && ! n . matchesRepo ( ref ) })
132133 if len (explicit ) == 1 {
133134 return explicit [0 ], nil
134135 }
135- candidates := apmCandidates (nodes , child , func (n * apmNode ) bool { return n .key == ref || n .repo == ref })
136+ candidates := apmCandidates (nodes , child , func (n * apmNode ) bool { return n .matchesKey ( ref ) || n .matchesRepo ( ref ) })
136137 switch len (candidates ) {
137138 case 0 :
138139 return nil , fmt .Errorf ("%w: %s at depth %d has resolved_by %q" , ErrAPMMissingParent , child .key , child .depth , ref )
@@ -154,19 +155,39 @@ func apmCandidates(nodes []*apmNode, child *apmNode, match func(*apmNode) bool)
154155 return candidates
155156}
156157
157- // apmRepo returns repo_url case-folded as APM loads it.
158- func apmRepo (entry apmLockEntry ) string {
158+ // newAPMNode builds the node and its key as APM's build_dependency_unique_key does.
159+ func newAPMNode (entry apmLockEntry , depth int ) * apmNode {
160+ n := & apmNode {entry : entry , depth : depth , folded : apmFolds (entry ), repo : entry .RepoURL }
161+ if n .folded {
162+ n .repo = strings .ToLower (n .repo )
163+ }
164+ if entry .Source == "local" && entry .LocalPath != "" {
165+ n .key = entry .LocalPath
166+ if entry .AnchoredLocal != "" {
167+ n .key = "local:" + entry .AnchoredLocal
168+ }
169+ return n
170+ }
171+ if host := strings .ToLower (strings .TrimSpace (entry .Host )); entry .RegistryPrefix == "" && host != "" && host != "github.com" {
172+ n .host = host
173+ }
174+ n .key = n .repo + n .virtualSuffix ()
175+ if n .host != "" {
176+ n .key = n .host + "/" + n .key
177+ }
178+ return n
179+ }
180+
181+ // apmFolds reports whether APM lowercases the entry's repo_url, as it does since microsoft/apm#3370.
182+ func apmFolds (entry apmLockEntry ) bool {
159183 if entry .Source == "local" {
160- return entry . RepoURL
184+ return false
161185 }
162186 if entry .Source == "registry" || entry .RegistryPrefix != "" {
163- return strings . ToLower ( entry . RepoURL )
187+ return true
164188 }
165189 host := strings .ToLower (strings .TrimSpace (entry .Host ))
166- if (host == "" || host == "github.com" || strings .HasSuffix (host , ".ghe.com" )) && ! apmLocator (entry .RepoURL ) {
167- return strings .ToLower (entry .RepoURL )
168- }
169- return entry .RepoURL
190+ return (host == "" || host == "github.com" || strings .HasSuffix (host , ".ghe.com" )) && ! apmLocator (entry .RepoURL )
170191}
171192
172193// apmLocator reports a URL or host-qualified repo_url, which APM does not write.
@@ -175,23 +196,39 @@ func apmLocator(repo string) bool {
175196 return strings .Contains (repo , ":" ) || (strings .Contains (first , "." ) && strings .Contains (rest , "/" ))
176197}
177198
178- // apmLockKey mirrors APM's lockfile identity key.
179- func apmLockKey (entry apmLockEntry , repo string ) string {
180- if entry .Source == "local" && entry .LocalPath != "" {
181- if entry .AnchoredLocal != "" {
182- return "local:" + entry .AnchoredLocal
183- }
184- return entry .LocalPath
199+ // virtualSuffix returns "/virtual_path" for a virtual entry.
200+ func (n * apmNode ) virtualSuffix () string {
201+ if n .entry .IsVirtual && n .entry .VirtualPath != "" {
202+ return "/" + n .entry .VirtualPath
185203 }
186- host := strings .ToLower (strings .TrimSpace (entry .Host ))
187- key := repo
188- if entry .IsVirtual && entry .VirtualPath != "" {
189- key += "/" + entry .VirtualPath
204+ return ""
205+ }
206+
207+ // matchesRepo compares ref to repo_url, folding ref by the node's case rules.
208+ func (n * apmNode ) matchesRepo (ref string ) bool {
209+ if n .folded {
210+ ref = strings .ToLower (ref )
190211 }
191- if entry .RegistryPrefix == "" && host != "" && host != "github.com" {
192- key = host + "/" + key
212+ return ref == n .repo
213+ }
214+
215+ // matchesKey compares ref to the full key, keeping the virtual path case-sensitive.
216+ func (n * apmNode ) matchesKey (ref string ) bool {
217+ if n .entry .Source == "local" && n .entry .LocalPath != "" {
218+ return ref == n .key
219+ }
220+ rest , ok := strings .CutSuffix (ref , n .virtualSuffix ())
221+ if ! ok {
222+ return false
223+ }
224+ if n .host != "" {
225+ hostPart , path , ok := strings .Cut (rest , "/" )
226+ if ! ok || ! strings .EqualFold (hostPart , n .host ) {
227+ return false
228+ }
229+ rest = path
193230 }
194- return key
231+ return n . matchesRepo ( rest )
195232}
196233
197234func init () {
0 commit comments