Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.artifacts.Configuration;
Expand Down Expand Up @@ -64,12 +65,19 @@ public boolean canBuild(String modelName) {

public Object buildAll(String modelName, Project project) {
cachedTasks.clear();
DefaultGradleProject gradleProject = (DefaultGradleProject) this.registry
.getBuilder("org.gradle.tooling.model.GradleProject").buildAll(modelName, project);
if (gradleProject == null) {
return null;

GradleProjectModel rootModel = null;
int cmpVersion = GradleVersion.current().compareTo(GradleVersion.version("5.0.0"));

if (cmpVersion == -1) {
rootModel = buildModel(project, project);
} else {
DefaultGradleProject gradleProject = (DefaultGradleProject) this.registry
.getBuilder("org.gradle.tooling.model.GradleProject").buildAll(modelName, project);

rootModel = buildModel(project, project.getName(), gradleProject);
}
GradleProjectModel rootModel = buildModel(project, project.getName(), gradleProject);

// add task selectors for root project
Set<String> taskNames = new HashSet<>();
for (GradleTask existingTask : rootModel.getTasks()) {
Expand All @@ -93,6 +101,59 @@ public Object buildAll(String modelName, Project project) {
return rootModel;
}

private GradleProjectModel buildModel(Project rootProject, Project project) {
if (rootProject == null || project == null) {
return null;
}

ScriptHandler buildScript = project.getBuildscript();

ClassPath classpath = ((DefaultScriptHandler) buildScript).getScriptClassPath();

List<String> scriptClasspaths = classpath.getAsFiles().stream().map(file -> file.getAbsolutePath())
.collect(Collectors.toList());

GradleDependencyNode node = generateDefaultGradleDependencyNode(project);

List<String> plugins = getPlugins(project);

List<GradleClosure> closures = getPluginClosures(project);

Map<String, Project> childProjects = project.getChildProjects();

List<GradleProjectModel> subModels = childProjects.values().stream()
.map(childProject -> buildModel(rootProject, childProject)).filter(subModel -> subModel != null)
.collect(Collectors.toList());

List<GradleTask> tasks = getGradleTasks(rootProject, project);

return new DefaultGradleProjectModel(project.getParent() == null, project.getProjectDir().getAbsolutePath(),
subModels, tasks, node, plugins, closures, scriptClasspaths);
}

private List<GradleTask> getGradleTasks(Project rootProject, Project project) {
Copy link
Copy Markdown
Member

@jdneo jdneo Nov 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just directly fetch the tasks with this approach no matter the project's gradle version is > or < 5? Since they look very similar.

// The original code is not implemented by me so I'm not aware of the history of it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for long response, as far as I understand plugin's local gradle project model does not support some lazy tasks, so this commit was made as fix #1302 in which now plugin uses gradle built in model. I couldn't reproduce this bug though so I can try to unify these methods to be compatible with everything, but it surely will need more testing

Copy link
Copy Markdown

@anand-ts anand-ts Apr 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this PR is still relevant for users on Gradle 4.x.
I’m also running into what appears to be this same issue with a Gradle 4.10.3 project.

From the discussion (and to my understanding), it sounds like #1302 moved task discovery toward Gradle's built-in model to avoid lazy task/configuration-resolution issues like #1275. That makes sense for newer Gradle versions.

The problem is that Gradle 4.10.3 does not appear to support the same DefaultGradleProject API path, so the extension can fail task discovery for legacy projects even though ./gradlew tasks --all works from the terminal.

My environment:

  • OS: Windows 11
  • VS Code: 1.117.0
  • vscode-gradle: 3.7.13
  • Gradle wrapper: 4.10.3
  • Java: Temurin/OpenJDK 8

Any leads would be greatly appreciated.
Thank you!

List<GradleTask> tasks = new ArrayList<>();
TaskContainer taskContainer = project.getTasks();

if (taskContainer instanceof TaskContainerInternal) {
TaskContainerInternal taskContainerInternal = (TaskContainerInternal) taskContainer;
taskContainerInternal.discoverTasks();
taskContainerInternal.realize();
taskContainerInternal.forEach(task -> {
String buildFile = task.getProject().getBuildscript().getSourceFile().getAbsolutePath();
boolean debuggable = (task instanceof JavaExec) || (task instanceof Test);
GradleTask newTask = new DefaultGradleTask(task.getName(), task.getGroup(), task.getPath(),
task.getProject().getName(), buildFile, rootProject.getName(), task.getDescription(),
debuggable);

tasks.add(newTask);
cachedTasks.add(newTask);
});
}

return tasks;
}

private GradleProjectModel buildModel(Project project, String rootProjectName, DefaultGradleProject gradleProject) {
if (project == null) {
return null;
Expand Down Expand Up @@ -277,4 +338,4 @@ private boolean isDeprecated(AccessibleObject object) {
}
return false;
}
}
}