Skip to content

Commit

Permalink
Fix #822 - If multiple fragments match a bundle all items are added to
Browse files Browse the repository at this point in the history
the classpath while only the one with the highest version should match
  • Loading branch information
laeubi committed Apr 1, 2022
1 parent ad065eb commit e959f27
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008, 2020 Sonatype Inc. and others.
* Copyright (c) 2008, 2022 Sonatype Inc. and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -11,6 +11,7 @@
* Sonatype Inc. - initial API and implementation
* Christoph Läubrich - Bug 567098 - pomDependencies=consider should wrap non-osgi jars
* Issue #443 - Use regular Maven coordinates -when possible- for dependencies
* Issue #822 - If multiple fragments match a bundle all items are added to the classpath while only the one with the highest version should match
*******************************************************************************/
package org.eclipse.tycho.p2.resolver;

Expand All @@ -19,6 +20,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -132,11 +134,18 @@ public Set<IInstallableUnit> calculateDependencyFragments(ResolutionData data,
if (fragmentsList.isEmpty()) {
return Collections.emptySet();
}
MavenLogger logger = mavenContext.getLogger();
if (logger.isExtendedDebugEnabled() && !fragmentsList.isEmpty()) {
logger.debug("Possible candidate fragments:");
for (Entry<IInstallableUnit, IRequiredCapability> fragmentEntry : fragmentsList) {
logger.debug(" " + fragmentEntry.getKey().toString());
}
}

Map<String, List<IInstallableUnit>> resolvedUnitsById = resolvedUnits.stream()//
.collect(Collectors.groupingBy(iu -> iu.getId()));

return fragmentsList.stream()//
Map<String, List<IInstallableUnit>> matching = fragmentsList.stream()//
.filter(entry -> {
IRequiredCapability hostRequirement = entry.getValue();
List<IInstallableUnit> potentialHosts = resolvedUnitsById.get(hostRequirement.getName());
Expand All @@ -151,7 +160,21 @@ public Set<IInstallableUnit> calculateDependencyFragments(ResolutionData data,
return false;
})//
.map(entry -> entry.getKey())//
.collect(Collectors.toSet());
.collect(Collectors.groupingBy(IInstallableUnit::getId));
Set<IInstallableUnit> filteredResult = matching.values().stream().map(candidates -> {
if (candidates.size() == 1) {
return candidates.get(0);
}
return candidates.stream().max(Comparator.comparing(IInstallableUnit::getVersion)).get();
}).collect(Collectors.toSet());

if (logger.isDebugEnabled() && !filteredResult.isEmpty()) {
logger.info("Resolved fragments:");
for (IInstallableUnit unit : filteredResult) {
logger.info(" " + unit.toString());
}
}
return filteredResult;
}

private static Optional<Entry<IInstallableUnit, IRequiredCapability>> findFragmentHostRequirement(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Bundle 1
Bundle-SymbolicName: bundle1;singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-11
Require-Bundle: org.eclipse.swt;bundle-version="3.119.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.
14 changes: 14 additions & 0 deletions tycho-its/projects/resolver.multipleDownloads/bundle1/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.eclipse.tycho.its</groupId>
<artifactId>resolver-multiple-downloads-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>bundle1</artifactId>
<packaging>eclipse-plugin</packaging>

</project>
46 changes: 46 additions & 0 deletions tycho-its/projects/resolver.multipleDownloads/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.eclipse.tycho.its</groupId>
<artifactId>resolver-multiple-downloads-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<tycho-version>3.0.0-SNAPSHOT</tycho-version>
</properties>

<modules>
<module>bundle1</module>
</modules>

<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>${tycho-version}</version>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>target-platform-configuration</artifactId>
<version>${tycho-version}</version>
<configuration>
<target>
<file>../test.target</file>
</target>
<environments>
<environment>
<os>linux</os>
<ws>gtk</ws>
<arch>x86_64</arch>
</environment>
</environments>
</configuration>
</plugin>
</plugins>
</build>

</project>
10 changes: 10 additions & 0 deletions tycho-its/projects/resolver.multipleDownloads/sdk_4.21.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?pde version="3.8"?>
<target name="Eclipse 4.21">
<locations>
<location includeAllPlatforms="false" includeConfigurePhase="true" includeMode="planner" includeSource="true" type="InstallableUnit">
<repository location="https://download.eclipse.org/eclipse/updates/4.21/"/>
<unit id="org.eclipse.platform.ide" version="4.21.0.I20210906-0500"/>
</location>
</locations>
</target>
10 changes: 10 additions & 0 deletions tycho-its/projects/resolver.multipleDownloads/sdk_4.22.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?pde version="3.8"?>
<target name="Eclipse 4.22">
<locations>
<location includeAllPlatforms="false" includeConfigurePhase="true" includeMode="planner" includeSource="true" type="InstallableUnit">
<repository location="https://download.eclipse.org/eclipse/updates/4.22/"/>
<unit id="org.eclipse.platform.ide" version="4.22.0.I20211124-1800"/>
</location>
</locations>
</target>
10 changes: 10 additions & 0 deletions tycho-its/projects/resolver.multipleDownloads/sdk_4.23.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?pde version="3.8"?>
<target name="Eclipse 4.23">
<locations>
<location includeAllPlatforms="false" includeConfigurePhase="true" includeMode="planner" includeSource="true" type="InstallableUnit">
<repository location="https://download.eclipse.org/eclipse/updates/4.23/"/>
<unit id="org.eclipse.platform.ide" version="4.23.0.I20220308-0310"/>
</location>
</locations>
</target>
9 changes: 9 additions & 0 deletions tycho-its/projects/resolver.multipleDownloads/test.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?pde version="3.8"?>
<target name="test">
<locations>
<location type="Target" uri="file:${project_loc:/resolver-multiple-downloads-parent}/sdk_4.21.target"/>
<location type="Target" uri="file:${project_loc:/resolver-multiple-downloads-parent}/sdk_4.22.target"/>
<location type="Target" uri="file:${project_loc:/resolver-multiple-downloads-parent}/sdk_4.23.target"/>
</locations>
</target>
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,21 @@
*******************************************************************************/
package org.eclipse.tycho.test.resolver;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.List;

import org.apache.maven.it.Verifier;
import org.eclipse.tycho.test.AbstractTychoIntegrationTest;
import org.junit.Test;

public class ResolverTests extends AbstractTychoIntegrationTest {

private static final String SDK_23 = "org.eclipse.swt.gtk.linux.x86_64 3.119.0.v20220223-1102";
private static final String SDK_22 = "org.eclipse.swt.gtk.linux.x86_64 3.118.0.v20211123-0851";
private static final String SDK_21 = "org.eclipse.swt.gtk.linux.x86_64 3.117.0.v20210906-0842";

/**
* This test case tests a combination that at a first glance looks very simple
* but is hard to resolve due to the structure of 'org.eclipse.core.runtime'
Expand Down Expand Up @@ -53,4 +62,40 @@ public void testMixedReactor() throws Exception {
verifier.verifyErrorFreeLog();
}

@Test
public void testMultipleFragmentsOnlyOneIsChoosen() throws Exception {

Verifier verifier = getVerifier("resolver.multipleDownloads", false, false);
verifier.executeGoal("compile");
verifier.verifyErrorFreeLog();
List<String> lines = verifier.loadFile(verifier.getBasedir(), verifier.getLogFileName(), false);

// [INFO] Resolved fragments:
// [INFO] org.eclipse.swt.gtk.linux.x86_64 3.118.0.v20211123-0851
// [INFO] org.eclipse.swt.gtk.linux.x86_64 3.119.0.v20220223-1102
// [INFO] org.eclipse.swt.gtk.linux.x86_64 3.117.0.v20210906-0842
// [INFO] ------------------------------------------------------------------------
boolean startLine = false;
boolean highestVersionFound = false;
for (String ansiLine : lines) {
String line = Verifier.stripAnsi(ansiLine);
if (startLine) {
if (line.endsWith("------")) {
break;
}
if (line.endsWith(SDK_21)) {
fail("3.117 was found but should not be part of the result");
}
if (line.endsWith(SDK_22)) {
fail("3.118 was found but should not be part of the result");
}
highestVersionFound |= line.endsWith(SDK_23);
} else {
startLine = line.endsWith("Resolved fragments:");
}
}
assertTrue("Start line not found!", startLine);
assertTrue("Highest version was not found", highestVersionFound);
}

}

0 comments on commit e959f27

Please sign in to comment.