Skip to content

SpringApplication.setEnvironmentPrefix is ignored when reading SPRING_PROFILES_ACTIVE #45387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
philwebb opened this issue May 7, 2025 · 2 comments
Assignees
Labels
type: bug A general bug
Milestone

Comments

@philwebb
Copy link
Member

philwebb commented May 7, 2025

See #45370 (comment) for a reproducer.

A bit of analysis shows that this line in ConfigDataEnvironmentContributor is creating the source, but unlike SpringConfigurationPropertySources.adapt it is not respecting the prefix.

@GrahamHannington
Copy link

GrahamHannington commented May 8, 2025

@philwebb, thanks for creating and analyzing this issue, especially after my unpromising false start in the original issue (bogus code snippet, no reproducer).

I gather from your analysis that Spring Boot does not read all environment variables via the same single code path.

Did I just "get lucky" 😉, and strike the only environment variable for which Spring Boot ignores the environment prefix?

Do other configuration properties also not respect the prefix?

I acknowledge that this question might not be straightforward to answer. I'm not asking you to go on a time-consuming bug hunt.

Still, given the analysis that you've done for this issue, would it be quick and easy for you to search for similar telltale code that ignores the environment prefix when reading environment variables? Doing that now might avoid creating multiple similar property-specific issues in the future. Just a thought. I totally get that you might not have time for this.

I'd like to set an environment prefix for my app, but the surprise discovery that Spring Boot does not (as you wrote) respect the prefix on all configuration properties spooked me. I don't want my app users to be similarly surprised. I can reduce that risk by testing the common properties that I think users are likely to use, but I'd prefer to have higher confidence that, to your (the Spring Boot developers') knowledge, Spring Boot consistently respects the prefix on all properties. (I acknowledge that "I don't want..." and "I'd prefer..." are my issues; my problems, not yours.)

@GrahamHannington
Copy link

I asked:

Do other configuration properties also not respect the prefix?

The Spring Boot source is largely opaque to me as a newbie, so I thought I'd attempt an answer by playing Battleships and trying an "adjacent" configuration property, spring.profiles.include.

Boom! The environment variable SPRING_PROFILES_INCLUDE_0 also does not respect the prefix. That is, Spring Boot ignores it if you prepend the environment prefix to its name. (By now, I doubt this is news to you, @philwebb.)

This issue is interesting (at least, to me): it tells me that nobody else who sets an environment prefix is also using environment variables to set profiles. Unless others have met this bug without reporting it.

This surprises me. I don't feel qualified to label profiles as "best practice" for external configuration, but they certainly offer a very flexible way of composing configurations from multiple reusable parts. Similarly, setting an environment prefix seems like a pretty good idea to me, especially for "generically" named properties such as server.port; it's nice to be able to namespace application properties.

Before encountering this bug, I would've guessed that the cool Spring Boot kids were doing both: setting an environment prefix
and, at least occasionally, using an environment variable to replace or add to profiles that are set in a configuration file. But the existence of this bug proves that guess was wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants