From f0dcc1926187994bddf4d14a91d13697efa3d063 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 3 Oct 2025 09:16:00 +0000 Subject: [PATCH] Fix IllegalArgumentException with duplicate natures Changed ProjectDescription.hasPrivateChanges() to use HashSet instead of Set.of() to handle duplicate natures gracefully. Set.of() throws IllegalArgumentException on duplicates, but HashSet tolerates them and correctly compares nature sets. Also added test case testDuplicateNatures() to ensure this regression doesn't occur again. --- .../resources/ProjectDescription.java | 3 ++- .../core/tests/resources/NatureTest.java | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ProjectDescription.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ProjectDescription.java index 5a1b5d02f6c..69c5e408513 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ProjectDescription.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ProjectDescription.java @@ -24,6 +24,7 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; @@ -581,7 +582,7 @@ public boolean hasPrivateChanges(ProjectDescription description) { return true; } // has natures changed? - if (!Set.of(natures).equals(Set.of(description.natures))) { + if (!new HashSet<>(Arrays.asList(natures)).equals(new HashSet<>(Arrays.asList(description.natures)))) { return true; } // has buildspec changed? diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/NatureTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/NatureTest.java index c5483cd4bc9..d4c82a80eae 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/NatureTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/NatureTest.java @@ -167,6 +167,30 @@ public void testInvalidRemovals() throws Throwable { assertHasEnabledNature(NATURE_SNOW); } + /** + * Tests that duplicate natures in the project description don't cause + * IllegalArgumentException. This is a regression test for a bug where + * Set.of() was used which throws on duplicates. + */ + @Test + public void testDuplicateNatures() throws Throwable { + createInWorkspace(project); + + // Set initial nature + setNatures(project, new String[] { NATURE_SIMPLE }, false); + assertHasEnabledNature(NATURE_SIMPLE); + + // Try to set natures with a duplicate - this should not throw IllegalArgumentException + // The duplicate should be handled gracefully (deduplication happens automatically) + IProjectDescription desc = project.getDescription(); + desc.setNatureIds(new String[] { NATURE_SIMPLE, NATURE_SIMPLE }); + // This should not throw IllegalArgumentException when hasPrivateChanges is called + project.setDescription(desc, IResource.KEEP_HISTORY, createTestMonitor()); + + // After deduplication, only one instance of the nature should remain + assertHasEnabledNature(NATURE_SIMPLE); + } + @Test public void testNatureLifecyle() throws Throwable { createInWorkspace(project);