From e933ba52fa5a40b48d7ac95b5e684c5f237379c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Tue, 6 Feb 2024 07:38:42 +0100 Subject: [PATCH] Print not mapped API messages to the log Currently if a message can't be mapped to the logfile it is simply ignored. This now always print them to the log if they can't be mapped and print a waring about the issue as it is usually some configuration problem. --- .../tycho/apitools/ApiAnalysisMojo.java | 27 +++++++++++++++---- .../tycho/apitools/LogFileEnhancer.java | 14 +++++++--- .../core/TargetPlatformConfiguration.java | 9 +++++++ .../tycho/core/TychoProjectManager.java | 7 +++-- ...aultTargetPlatformConfigurationReader.java | 15 +++++------ 5 files changed, 52 insertions(+), 20 deletions(-) diff --git a/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiAnalysisMojo.java b/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiAnalysisMojo.java index f6f711ba68..9bf5b1041d 100644 --- a/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiAnalysisMojo.java +++ b/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiAnalysisMojo.java @@ -22,6 +22,7 @@ import java.util.Objects; import java.util.Optional; import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Consumer; import java.util.stream.Collectors; @@ -195,10 +196,6 @@ public void execute() throws MojoExecutionException, MojoFailureException { .collect(Collectors.groupingBy(IApiProblem::getSeverity)); List errors = problems.getOrDefault(ApiPlugin.SEVERITY_ERROR, List.of()); List warnings = problems.getOrDefault(ApiPlugin.SEVERITY_WARNING, List.of()); - if (printSummary) { - log.info(errors.size() + " API ERRORS"); - log.info(warnings.size() + " API warnings"); - } if (printProblems) { for (IApiProblem problem : errors) { printProblem(problem, "API ERROR", log::error); @@ -209,11 +206,31 @@ public void execute() throws MojoExecutionException, MojoFailureException { } if (enhanceLogs && logDirectory != null && logDirectory.isDirectory()) { try { - LogFileEnhancer.enhanceXml(logDirectory, analysisResult); + AtomicInteger notMapped = new AtomicInteger(); + LogFileEnhancer.enhanceXml(logDirectory, analysisResult, notfound -> { + notMapped.incrementAndGet(); + if (printProblems) { + // it was already printed before... + return; + } + if (ApiPlugin.SEVERITY_ERROR == notfound.getSeverity()) { + printProblem(notfound, "API ERROR", log::error); + } else if (ApiPlugin.SEVERITY_WARNING == notfound.getSeverity()) { + printProblem(notfound, "API WARNING", log::warn); + } + }); + int count = notMapped.get(); + if (count > 0) { + log.warn(count + " API problems can't be mapped to the compiler log!"); + } } catch (IOException e) { log.warn("Can't enhance logs in directory " + logDirectory); } } + if (printSummary) { + log.info(errors.size() + " API ERRORS"); + log.info(warnings.size() + " API warnings"); + } if (errors.size() > 0 && failOnError) { String msg = errors.stream().map(problem -> { if (problem.getResourcePath() == null) { diff --git a/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/LogFileEnhancer.java b/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/LogFileEnhancer.java index c425750e8f..1560b441a7 100644 --- a/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/LogFileEnhancer.java +++ b/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/LogFileEnhancer.java @@ -24,7 +24,9 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Objects; import java.util.Set; +import java.util.function.Consumer; import java.util.stream.Collectors; import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin; @@ -46,9 +48,11 @@ public class LogFileEnhancer { private static final String ATTRIBUTES_INFOS = "infos"; private static final String ATTRIBUTES_ERRORS = "errors"; - public static void enhanceXml(File logDirectory, ApiAnalysisResult analysisResult) throws IOException { + public static void enhanceXml(File logDirectory, ApiAnalysisResult analysisResult, + Consumer notFoundConsumer) throws IOException { Map> problems = analysisResult.problems() - .collect(Collectors.groupingBy(IApiProblem::getResourcePath)); + .collect(Collectors.groupingBy(problem -> Objects.requireNonNullElse(problem.getResourcePath(), + "no-path-" + System.identityHashCode(problem)))); if (problems.isEmpty()) { return; } @@ -56,6 +60,7 @@ public static void enhanceXml(File logDirectory, ApiAnalysisResult analysisResul Map documents = readDocuments(logDirectory); for (Entry> problemEntry : problems.entrySet()) { String path = problemEntry.getKey(); + boolean found = false; for (Entry documentEntry : documents.entrySet()) { Document document = documentEntry.getValue(); Element statsElement = getStatsElement(document); @@ -63,6 +68,7 @@ public static void enhanceXml(File logDirectory, ApiAnalysisResult analysisResul for (Element source : sources.getChildren("source")) { String pathAttribute = source.getAttributeValue("path"); if (pathAttribute != null && !pathAttribute.isEmpty() && pathAttribute.endsWith(path)) { + found = true; needsUpdate.add(documentEntry.getKey()); Element problemsElement = getProblemsElement(source); List list = problemEntry.getValue(); @@ -90,7 +96,9 @@ public static void enhanceXml(File logDirectory, ApiAnalysisResult analysisResul } } } - + if (!found) { + problemEntry.getValue().forEach(notFoundConsumer); + } } writeDocuments(needsUpdate, documents); } diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/TargetPlatformConfiguration.java b/tycho-core/src/main/java/org/eclipse/tycho/core/TargetPlatformConfiguration.java index 26a450f199..472e14437d 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/TargetPlatformConfiguration.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/TargetPlatformConfiguration.java @@ -92,6 +92,7 @@ public enum InjectP2MavenMetadataHandling { private String resolver; private List environments = new ArrayList<>(); + private List filteredEnvironments = new ArrayList<>(); private boolean implicitTargetEnvironment = true; @@ -340,4 +341,12 @@ public void addTargetLocation(Xpp3Dom locationDom) { xmlFragments.add(locationDom); } + public void addFilteredEnvironment(TargetEnvironment environment) { + filteredEnvironments.add(environment); + } + + public List getFilteredEnvironments() { + return filteredEnvironments; + } + } diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/TychoProjectManager.java b/tycho-core/src/main/java/org/eclipse/tycho/core/TychoProjectManager.java index 7ef1c54697..ae9d9d4a8d 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/TychoProjectManager.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/TychoProjectManager.java @@ -63,7 +63,6 @@ import org.eclipse.tycho.helper.PluginRealmHelper; import org.eclipse.tycho.model.project.EclipseProject; import org.eclipse.tycho.targetplatform.TargetDefinition; -import org.osgi.framework.Filter; import aQute.bnd.osgi.Processor; @@ -185,10 +184,10 @@ public TargetPlatformConfiguration getTargetPlatformConfiguration(ReactorProject public Collection getTargetEnvironments(MavenProject project) { TychoProject tychoProject = projectTypes.get(project.getPackaging()); if (tychoProject != null) { - Filter environmentFilter = tychoProject.getTargetEnvironmentFilter(project); - return getTargetPlatformConfiguration(project).getEnvironments().stream() - .filter(te -> te.match(environmentFilter)).toList(); + //these will already be filtered at reading the target configuration + return getTargetPlatformConfiguration(project).getEnvironments(); } + //if no tycho project, just assume the default running environment return List.of(TargetEnvironment.getRunningEnvironment()); } diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/resolver/DefaultTargetPlatformConfigurationReader.java b/tycho-core/src/main/java/org/eclipse/tycho/core/resolver/DefaultTargetPlatformConfigurationReader.java index 5df5c96198..56d47f25e3 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/resolver/DefaultTargetPlatformConfigurationReader.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/resolver/DefaultTargetPlatformConfigurationReader.java @@ -21,7 +21,6 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.text.MessageFormat; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Objects; @@ -332,8 +331,8 @@ private void setBREEHeaderSelectionPolicy(TargetPlatformConfiguration result, Xp /** * Take the constraints of the configured execution environment into account when resolving * dependencies or target definitions. These constraints include the list of system packages and - * the Bundle-RequiredExecutionEnvironment header. When set to true, the - * dependency resolution verifies that the bundle and all required bundles can be used in an + * the Bundle-RequiredExecutionEnvironment header. When set to true, + * the dependency resolution verifies that the bundle and all required bundles can be used in an * OSGi container with the configured execution environment. */ private void setResolveWithEEContraints(TargetPlatformConfiguration result, Xpp3Dom resolverDom) { @@ -372,20 +371,20 @@ private void addTargetEnvironments(TargetPlatformConfiguration result, MavenProj Xpp3Dom environmentsDom = configuration.getChild(ENVIRONMENTS); if (environmentsDom != null) { Filter filter = getTargetEnvironmentFilter(tychoProject, project); - List skipped = new ArrayList<>(); for (Xpp3Dom environmentDom : environmentsDom.getChildren("environment")) { TargetEnvironment environment = newTargetEnvironment(environmentDom); if (environment.match(filter)) { result.addEnvironment(environment); } else { - skipped.add(environment); + result.addFilteredEnvironment(environment); } } - if (!skipped.isEmpty()) { + List filteredEnvironments = result.getFilteredEnvironments(); + if (!filteredEnvironments.isEmpty()) { logger.debug(MessageFormat.format( "Declared TargetEnvironment(s) {0} are skipped for {1} as they do not match the project filter {2}.", - skipped.stream().map(TargetEnvironment::toFilterProperties).map(String::valueOf) - .collect(Collectors.joining(", ")), + filteredEnvironments.stream().map(TargetEnvironment::toFilterProperties) + .map(String::valueOf).collect(Collectors.joining(", ")), project.getId(), filter)); } }