Hi,

this PR fixes #24319 by removing the current profile from group profiles in order to avoid an endless loop when expanding/flattening the profile list.

Cheers, Christoph

Comment From: mbhave

I'm not sure if just removing the current profile is enough. I think we'd end up with the same issue if we had a configuration like:

spring.profiles.group.a=e,f
spring.profiles.group.e=a,x,y

Maybe we should throw an exception in this case since it is an invalid configuration.

Comment From: dreis2211

@mbhave Good catch - I covered the additional case now.

Which sort of exception do you want to be thrown here? IllegalStateException or a new one? Just let me know if I should add the exception and if so which type. The "maybe" suggests that you're not sure yet.

Comment From: philwebb

I think an IllegalStateException would be fine. We can always change it later if we need to.

Comment From: dreis2211

@philwebb & @mbhave I added the IllegalStateException. Let me know if this is what you had in mind.

Comment From: philwebb

Thanks once again @dreis2211! I've merged this into master with a minor update to make the message catch duplicates in the expanded set as well as the stack.

Comment From: philwebb

Reopening because I just realized that a user might want to have the same expansion in different groups:

    @Test
    void test() {
        MockEnvironment environment = new MockEnvironment();
        environment.setProperty("spring.profiles.active", "a,b,c");
        environment.setProperty("spring.profiles.group.a", "e,f");
        environment.setProperty("spring.profiles.group.e", "f,g");
        Binder binder = Binder.get(environment);
        assertThatIllegalStateException().isThrownBy(() -> new Profiles(environment, binder, null))
                .withMessageContaining("Profiles could not be resolved. Remove profile 'a' from group: 'e'");
    }

Comment From: philwebb

After some more thought, I think it's best if we tolerate duplicates rather than throwing an exception. I've changed things again in commit bef5fe29e3827f538ed95468fe6ea532f0b84867.

@mbhave @dreis2211 I'd appreciate a review if you have time. (I'll leave the issue open for now)

Comment From: dreis2211

Looks good to me, @philwebb