@@ -142,6 +142,10 @@ export class OrgListUtil {
142142 filename . match ( / ^ 0 0 D .{ 15 } \. j s o n $ / g)
143143 ) ;
144144
145+ // Get default org configuration to always include it even if it's a secondary user
146+ const configAggregator = await ConfigAggregator . create ( ) ;
147+ const defaultOrg = configAggregator . getPropertyValue ( OrgConfigProperties . TARGET_ORG ) ;
148+
145149 const allAuths : Array < AuthInfo | undefined > = await Promise . all (
146150 fileNames . map ( async ( fileName ) => {
147151 try {
@@ -169,6 +173,13 @@ export class OrgListUtil {
169173 usernames : string [ ] ;
170174 } ;
171175 const usernames = orgFileContent . usernames ;
176+
177+ // Always include the default org, even if it's a secondary user
178+ if ( defaultOrg === auth . getFields ( ) . username ) {
179+ return auth ;
180+ }
181+
182+ // Otherwise, only include primary users (first in the usernames array)
172183 if ( usernames && usernames [ 0 ] === auth . getFields ( ) . username ) {
173184 return auth ;
174185 }
@@ -241,6 +252,7 @@ export class OrgListUtil {
241252 'CreatedBy.Username' ,
242253 'SignupUsername' ,
243254 'LoginUrl' ,
255+ 'ScratchOrg' ,
244256 ] ;
245257
246258 try {
@@ -266,8 +278,14 @@ export class OrgListUtil {
266278 ) : Promise < FullyPopulatedScratchOrgFields [ ] > {
267279 const contentMap = new Map ( updatedContents . map ( ( org ) => [ org . SignupUsername , org ] ) ) ;
268280
281+ // Also create map by ScratchOrg (orgId) to handle cases where user authenticated as different user
282+ const contentMapByOrgId = new Map ( updatedContents . map ( ( org ) => [ org . ScratchOrg , org ] ) ) ;
283+
269284 const results = orgs . map ( ( scratchOrgInfo ) : FullyPopulatedScratchOrgFields | string => {
270- const updatedOrgInfo = contentMap . get ( scratchOrgInfo . username ) ;
285+ // Try to match by username first, then by orgId
286+ const updatedOrgInfo =
287+ contentMap . get ( scratchOrgInfo . username ) ?? contentMapByOrgId . get ( trimTo15 ( scratchOrgInfo . orgId ) ) ;
288+
271289 return updatedOrgInfo
272290 ? {
273291 ...scratchOrgInfo ,
0 commit comments