@@ -551,7 +551,7 @@ func createNewEnvironment(
551551 envName string ,
552552) (* azdext.Environment , error ) {
553553 if envName != "" {
554- if existingEnv := getExistingEnvironment (ctx , & initFlags { env : envName } , azdClient ); existingEnv != nil {
554+ if existingEnv := getExistingEnvironment (ctx , envName , azdClient ); existingEnv != nil {
555555 return existingEnv , nil
556556 }
557557 }
@@ -575,8 +575,11 @@ func createNewEnvironment(
575575 if exterrors .IsCancellation (err ) {
576576 return nil , exterrors .Cancelled ("environment creation was cancelled" )
577577 }
578- if envName != "" && isEnvironmentAlreadyExistsError (err ) {
579- if existingEnv := getExistingEnvironment (ctx , & initFlags {env : envName }, azdClient ); existingEnv != nil {
578+ // The workflow may have created the environment on disk before returning
579+ // an "already exists" error (e.g. a concurrent process raced, or a previous
580+ // partial run left env files behind). Re-fetch so we can reuse it.
581+ if envName != "" && status .Code (err ) == codes .AlreadyExists {
582+ if existingEnv := getExistingEnvironment (ctx , envName , azdClient ); existingEnv != nil {
580583 return existingEnv , nil
581584 }
582585 }
@@ -588,7 +591,7 @@ func createNewEnvironment(
588591 }
589592
590593 // Re-fetch the environment after creation
591- env := getExistingEnvironment (ctx , & initFlags { env : envName } , azdClient )
594+ env := getExistingEnvironment (ctx , envName , azdClient )
592595 if env == nil {
593596 return nil , exterrors .Dependency (
594597 exterrors .CodeEnvironmentNotFound ,
@@ -600,24 +603,6 @@ func createNewEnvironment(
600603 return env , nil
601604}
602605
603- // isEnvironmentAlreadyExistsError detects the duplicate-environment failure returned from
604- // `azd env new`. Today the workflow surfaces this as a gRPC Internal error whose message
605- // embeds the underlying azd error text, so we fall back to checking the status message
606- // after confirming the status code when no dedicated AlreadyExists status is available.
607- func isEnvironmentAlreadyExistsError (err error ) bool {
608- if status .Code (err ) == codes .AlreadyExists {
609- return true
610- }
611-
612- if status .Code (err ) != codes .Internal {
613- return false
614- }
615-
616- errText := strings .ToLower (status .Convert (err ).Message ())
617- return strings .Contains (errText , "creating new environment: environment" ) &&
618- strings .Contains (errText , "already exists" )
619- }
620-
621606// loadAzureContext reads the current Azure context values (tenant, subscription, location)
622607// from the azd environment and returns a populated AzureContext. Missing values are left empty.
623608func loadAzureContext (
0 commit comments