Skip to content

Commit

Permalink
Report type validation problems from DefaultNodeValidator (#29025)
Browse files Browse the repository at this point in the history
  • Loading branch information
bot-gradle committed May 4, 2024
2 parents e75ecd1 + 0bb7475 commit e966489
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ public void validateTaskClasses() throws IOException {
messages.collect(joining()));
} else {
reportProblems(problems);

throw WorkValidationException.forProblems(messages.collect(toImmutableList()))
.withSummaryForPlugin()
.getWithExplanation(annotateTaskPropertiesDoc());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,43 @@ class ProblemProgressEventCrossVersionTest extends ToolingApiSpecification {
problems[0].definition.id.group.name == 'compilation'
}

def "Property validation failure should produce problem report with domain-specific additional data"() {
setup:
file('buildSrc/src/main/java/MyTask.java') << '''
import org.gradle.api.*;
import org.gradle.api.tasks.*;
import org.gradle.work.*;
@DisableCachingByDefault(because = "test task")
public class MyTask extends DefaultTask {
@Optional @Input
boolean getPrimitive() {
return true;
}
@TaskAction public void execute() {}
}
'''
buildFile << '''
tasks.register('myTask', MyTask)
'''

when:
def listener = new ProblemProgressListener()
withConnection { connection ->
connection.newBuild()
.forTasks("myTask")
.addProgressListener(listener)
.setStandardError(System.err)
.setStandardOutput(System.out)
.addArguments("--info")

.run()
}

then:
thrown(BuildException)
listener.problems.size() == 1
}

@TargetGradleVersion("=8.6")
def "8.6 version doesn't send failure"() {
buildFile """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

import org.gradle.api.internal.GeneratedSubclasses;
import org.gradle.api.internal.TaskInternal;
import org.gradle.api.problems.internal.Problem;
import org.gradle.api.problems.Severity;
import org.gradle.api.problems.internal.InternalProblems;
import org.gradle.api.problems.internal.Problem;
import org.gradle.internal.deprecation.DeprecationLogger;
import org.gradle.internal.execution.WorkValidationContext;
import org.gradle.internal.execution.WorkValidationException;
Expand All @@ -39,6 +40,13 @@
* the build by throwing a {@link WorkValidationException} if any errors are found.
*/
public class DefaultNodeValidator implements NodeValidator {

private final InternalProblems problemsService;

public DefaultNodeValidator(InternalProblems problemsService) {
this.problemsService = problemsService;
}

@Override
public boolean hasValidationProblems(LocalTaskNode node) {
WorkValidationContext validationContext = validateNode(node);
Expand Down Expand Up @@ -73,6 +81,10 @@ private void logWarnings(List<? extends Problem> problems) {
}

private void reportErrors(List<? extends Problem> problems, TaskInternal task, WorkValidationContext validationContext) {
for (Problem problem : problems) {
problemsService.getInternalReporter().report(problem);
}

Set<String> uniqueErrors = getUniqueErrors(problems);
if (!uniqueErrors.isEmpty()) {
throw WorkValidationException.forProblems(uniqueErrors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ReceivedProblem implements Problem {
this.details = problemDetails['details'] as String
this.solutions = problemDetails['solutions'] as List<String>
this.locations = fromList(problemDetails['locations'] as List<Object>)
this.additionalData = problemDetails['additionalData'] as Map<String, Object>
this.additionalData = (problemDetails['additionalData'] as Map<String, Object>).findAll { k, v -> v != null }
this.exception = problemDetails['exception'] == null ? null : new ReceivedException(problemDetails['exception'] as Map<String, Object>)
}

Expand Down

0 comments on commit e966489

Please sign in to comment.