Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
93 changes: 47 additions & 46 deletions org.eclipse.jdt.ls.core/gradle/scala/javals.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import org.gradle.api.artifacts.component.ModuleComponentIdentifier
import org.gradle.api.artifacts.component.ProjectComponentIdentifier
import org.gradle.api.tasks.scala.ScalaCompile
import org.gradle.jvm.JvmLibrary
import org.gradle.language.base.artifact.SourcesArtifact

Expand All @@ -7,62 +9,61 @@ allprojects {
if (project.tasks.findByName('javalsCheckProject')) return
project.tasks.register('javalsCheckProject') {
doLast {
println "JAVALS_START"
def targetConfNames = ['runtimeClasspath', 'testRuntimeClasspath', 'scalaRuntime']
def componentIds = [] as Set
def processedFiles = [] as Set
targetConfNames.findAll { project.configurations.findByName(it) }.each { name ->
def conf = project.configurations[name]
if (conf.canBeResolved) {
conf.incoming.resolutionResult.allComponents.each {
if (it.id instanceof ModuleComponentIdentifier) componentIds.add(it.id)
if (project.plugins.hasPlugin('scala')) {
println "JAVALS_START"
def targetConfNames = ['runtimeClasspath', 'testRuntimeClasspath', 'scalaRuntime']
def componentIds = [] as Set
def processedFiles = [] as Set
def addLib = { file, sourcePath ->
if (file == null) return
def binPath = file.absolutePath
if (!processedFiles.contains(binPath)) {
println "LIB|${binPath}|${sourcePath ?: 'NO_SOURCE'}"
processedFiles.add(binPath)
}
}
}
def sourceMap = [:]
if (!componentIds.isEmpty()) {
project.dependencies.createArtifactResolutionQuery()
.forComponents(componentIds)
.withArtifacts(JvmLibrary, SourcesArtifact)
.execute()
.resolvedComponents.each { res ->
def sources = res.getArtifacts(SourcesArtifact)
if (!sources.isEmpty()) {
sourceMap["${res.id.group}:${res.id.module}".toString()] = sources.iterator().next().file.absolutePath
}
}
}
targetConfNames.findAll { project.configurations.findByName(it) }.each { name ->
def conf = project.configurations[name]
if (conf.canBeResolved) {
conf.resolvedConfiguration.resolvedArtifacts.each { artifact ->
def binPath = artifact.file.absolutePath
if (!processedFiles.contains(binPath)) {
def id = artifact.moduleVersion.id
def key = "${id.group}:${id.name}".toString()
def srcPath = sourceMap[key] ?: "NO_SOURCE"
println "LIB|${binPath}|${srcPath}"
processedFiles.add(binPath)
project.tasks.withType(ScalaCompile).each { task ->
def classesDir = task.hasProperty('destinationDirectory')
? task.destinationDirectory.get().asFile
: task.destinationDir
addLib(classesDir, "NO_SOURCE")
}
targetConfNames.findAll { project.configurations.findByName(it) }.each { name ->
def conf = project.configurations[name]
if (conf.canBeResolved) {
conf.incoming.resolutionResult.allComponents.each {
if (it.id instanceof ModuleComponentIdentifier) componentIds.add(it.id)
}
}
}
}
if (project.hasProperty('sourceSets')) {
project.sourceSets.each { ss ->
def scope = ss.name // 'main', 'test', 'integrationTest'
ss.allSource.srcDirs.each { dir ->
if (dir.exists() && !ss.resources.srcDirs.contains(dir)) {
println "SRC|${scope}|${dir.absolutePath}"
def sourceMap = [:]
if (!componentIds.isEmpty()) {
project.dependencies.createArtifactResolutionQuery()
.forComponents(componentIds)
.withArtifacts(JvmLibrary, SourcesArtifact)
.execute()
.resolvedComponents.each { res ->
def sources = res.getArtifacts(SourcesArtifact)
if (!sources.isEmpty()) {
sourceMap["${res.id.group}:${res.id.module}".toString()] = sources.iterator().next().file.absolutePath
}
}
}
ss.resources.srcDirs.each { dir ->
if (dir.exists()) {
println "RES|${scope}|${dir.absolutePath}"
}
targetConfNames.findAll { project.configurations.findByName(it) }.each { name ->
def conf = project.configurations[name]
if (conf.canBeResolved) {
conf.resolvedConfiguration.resolvedArtifacts.each { artifact ->
if (!(artifact.id.componentIdentifier instanceof ProjectComponentIdentifier)) {
def id = artifact.moduleVersion?.id
def key = id == null ? null : "${id.group}:${id.name}".toString()
def srcPath = key == null ? "NO_SOURCE" : sourceMap[key] ?: "NO_SOURCE"
addLib(artifact.file, srcPath)
}
}
}
}
println "JAVALS_END"
}
println "JAVALS_END"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -519,7 +520,7 @@ public void compile(IResource resource, IProgressMonitor monitor) {
return;
}
if (resource == null) {
Set<IProject> projects = new HashSet<>();
Map<File, IProject> projectsByRoot = new HashMap<>();
for (IProject project : ProjectUtils.getGradleProjects()) {
if (!project.isOpen()) {
continue;
Expand All @@ -529,28 +530,37 @@ public void compile(IResource resource, IProgressMonitor monitor) {
GradleBuild gb = gradleBuild.get();
if (gb instanceof InternalGradleBuild igb) {
File rootDir = igb.getBuildConfig().getProperties().getRootProjectDirectory();
if (rootDir != null && rootDir.equals(project.getRawLocation().toFile())) {
projects.add(project);
if (rootDir == null) {
continue;
}
// Prefer the workspace project whose location IS the gradle root
// (single-project or root-imported case). Otherwise, fall back to
// the first workspace project belonging to that build so we still
// have a connection point for multi-project builds where the root
// itself is not imported.
File projectLocation = project.getRawLocation() == null ? null : project.getRawLocation().toFile();
if (rootDir.equals(projectLocation)) {
projectsByRoot.put(rootDir, project);
} else {
projectsByRoot.putIfAbsent(rootDir, project);
}
}
}
}
Map<String, GradleProject> roots = new HashMap<>();
for (IProject project : projects) {
File projectDir = project.getLocation().toFile();
try (ProjectConnection connection = GradleConnector.newConnector().forProjectDirectory(projectDir).connect()) {
Map<File, GradleProject> roots = new HashMap<>();
for (Map.Entry<File, IProject> entry : projectsByRoot.entrySet()) {
File rootDir = entry.getKey();
try (ProjectConnection connection = GradleConnector.newConnector().forProjectDirectory(rootDir).connect()) {
GradleProject gradleProject = connection.getModel(GradleProject.class);
roots.put(gradleProject.getPath(), gradleProject);
roots.put(rootDir, gradleProject);
}
}
for (GradleProject gradleProject : roots.values()) {
List<String> taskNames = new ArrayList<>();
Set<String> taskNames = new LinkedHashSet<>();
for (String taskName : includeTasks) {
if (hasTask(gradleProject, taskName)) {
taskNames.add(taskName);
}
collectTaskPaths(gradleProject, taskName, taskNames);
}
compile(gradleProject, taskNames, monitor);
compile(gradleProject, new ArrayList<>(taskNames), monitor);
}
}
}
Expand Down Expand Up @@ -596,10 +606,10 @@ private void compile(GradleProject gradleProject, List<String> taskNames, IProgr
String error = errorStream.toString();
if (!error.isBlank()) {
publishDiagnostics(error, new ParseOptions(
taskNames.contains(COMPILE_KOTLIN) || taskNames.contains(COMPILE_TEST_KOTLIN),
taskNames.contains(COMPILE_GROOVY) || taskNames.contains(COMPILE_TEST_GROOVY),
taskNames.contains(COMPILE_ASPECTJ) || taskNames.contains(COMPILE_TEST_ASPECTJ),
taskNames.contains(COMPILE_SCALA) || taskNames.contains(COMPILE_TEST_SCALA)
containsTask(taskNames, COMPILE_KOTLIN) || containsTask(taskNames, COMPILE_TEST_KOTLIN),
containsTask(taskNames, COMPILE_GROOVY) || containsTask(taskNames, COMPILE_TEST_GROOVY),
containsTask(taskNames, COMPILE_ASPECTJ) || containsTask(taskNames, COMPILE_TEST_ASPECTJ),
containsTask(taskNames, COMPILE_SCALA) || containsTask(taskNames, COMPILE_TEST_SCALA)
)
);
}
Expand All @@ -612,13 +622,20 @@ private void compile(GradleProject gradleProject, List<String> taskNames, IProgr
.map(container -> (IProject) container)
.toArray(IProject[]::new);
List<IProject> workspaceProjects = ProjectUtils.getGradleProjects();
Set<IProject> refreshed = new HashSet<>();
for (IProject project: projects) {
project.refreshLocal(IResource.DEPTH_INFINITE, monitor);
for (IProject wp: workspaceProjects) {
if (wp.isAccessible() && !wp.equals(project)) {
if (project.getLocation().isPrefixOf(wp.getLocation())) {
wp.refreshLocal(IResource.DEPTH_INFINITE, monitor);
}
if (refreshed.add(project)) {
project.refreshLocal(IResource.DEPTH_INFINITE, monitor);
}
}
// Also refresh any workspace gradle project located under the
// gradle root directory. This covers multi-project builds where
// the root directory itself is not imported as an Eclipse project
// (so the loop above would otherwise be a no-op).
for (IProject wp : workspaceProjects) {
if (wp.isAccessible() && wp.getLocation() != null && path.isPrefixOf(wp.getLocation())) {
if (refreshed.add(wp)) {
wp.refreshLocal(IResource.DEPTH_INFINITE, monitor);
}
}
}
Expand Down Expand Up @@ -650,6 +667,26 @@ private boolean hasTask(GradleProject project, String taskName) {
return false;
}

private void collectTaskPaths(GradleProject project, String taskName, Set<String> taskPaths) {
for (GradleTask task : project.getTasks()) {
if (taskName.equals(task.getName())) {
String projectPath = project.getPath();
if (projectPath == null || ":".equals(projectPath)) {
taskPaths.add(taskName);
} else {
taskPaths.add(projectPath + ":" + taskName);
}
}
}
for (GradleProject childProject : project.getChildren()) {
collectTaskPaths(childProject, taskName, taskPaths);
}
}

private boolean containsTask(List<String> taskNames, String taskName) {
return taskNames.stream().anyMatch(name -> taskName.equals(name) || name.endsWith(":" + taskName));
}

private void publishDiagnostics(String error, ParseOptions parseOptions) {
JavaLanguageServerPlugin.logError("Gradle Error log: " + error);
Map<String, List<Diagnostic>> diagnosticMap = new HashMap<>();
Expand Down
Loading
Loading