Skip to content

Os.getOsFamily: return an empty string instead of null - #425

Merged
slachiewicz merged 2 commits into
masterfrom
agent/os-getosfamily-empty
Sep 11, 2026
Merged

slachiewicz merged 2 commits into
masterfrom
agent/os-getosfamily-empty

Conversation

@slachiewicz

Copy link
Copy Markdown
Member

Os.getOsFamily() returns null when the current OS matches none of the known families, so Os.OS_FAMILY can be null and every caller has to guard for it. Returning an empty string removes that trap.

This matches the direction already taken in #403, where PathTool.getRelativeFilePath moved from null to "" for the no-answer case.

The commit is Elliotte Rusty Harold's, cherry-picked from the fix/os-getosfamily-null branch he pushed here on 2026-07-01 and never opened as a PR. Authorship is preserved, and the code is unchanged. Opening it because the fix is finished and 3.5.0 is close; happy to hand it back if he would rather carry it himself.

Note for reviewers: this is a behaviour change on a public constant. A caller testing Os.OS_FAMILY == null silently stops matching. In practice the branch is only reachable on an OS none of the families cover, and Os is deprecated in 3.5.0 in favour of org.apache.commons.lang3.SystemUtils, so the exposure is small -- but it is not zero.

Verified: mvn -B verify on JDK 17 -> Tests run: 788, Failures: 0, Errors: 0. Spotless clean.

This change was created with AI assistance.

@slachiewicz slachiewicz added bug Something isn't working java Pull requests that update Java code labels Sep 10, 2026
@slachiewicz
slachiewicz marked this pull request as ready for review September 10, 2026 08:31
@slachiewicz slachiewicz added this to the 3.5.0 milestone Sep 10, 2026
Comment thread src/test/java/org/apache/maven/shared/utils/OsTest.java Outdated
The review asked for assertEquals("", OS_FAMILY) instead of assertNotNull,
but the empty-string fallback cannot be reached on any JVM: File.pathSeparator
is ":" or ";", and the unix and dos families are catch-alls on those
separators, with openvms, mac and netware covering their exclusions.
Asserting a specific family is not stable either, since VALID_FAMILIES is a
HashSet and both os/2 and unix match the os.name the test forces.

isValidFamily(OS_FAMILY) is the strongest assertion that holds on every
platform and it subsumes the null check.
@slachiewicz
slachiewicz merged commit 5b31dc9 into master Sep 11, 2026
15 checks passed
@slachiewicz
slachiewicz deleted the agent/os-getosfamily-empty branch September 11, 2026 07:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working java Pull requests that update Java code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants