Skip to content

Commit

Permalink
Clean up invalid projects during the initialized phase (#3208)
Browse files Browse the repository at this point in the history
  • Loading branch information
snjeza authored Jul 20, 2024
1 parent 9ba5c1e commit 715286a
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import org.eclipse.jdt.ls.core.internal.lsp.ValidateDocumentParams;
import org.eclipse.jdt.ls.core.internal.managers.ContentProviderManager;
import org.eclipse.jdt.ls.core.internal.managers.ProjectsManager;
import org.eclipse.jdt.ls.core.internal.managers.StandardProjectsManager;
import org.eclipse.jdt.ls.core.internal.managers.TelemetryManager;
import org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager;
import org.eclipse.jdt.ls.core.internal.preferences.Preferences;
Expand Down Expand Up @@ -301,6 +302,7 @@ public void initialized(InitializedParams params) {
@Override
public IStatus run(IProgressMonitor monitor) {
try {
StandardProjectsManager.cleanInvalidJavaProjects(monitor);
workspaceDiagnosticsHandler = new WorkspaceDiagnosticsHandler(JDTLanguageServer.this.client, pm, preferenceManager.getClientPreferences(), documentLifeCycleHandler);
workspaceDiagnosticsHandler.addResourceChangeListener();
classpathUpdateHandler = new ClasspathUpdateHandler(JDTLanguageServer.this.client, documentLifeCycleHandler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,24 @@ public void projectsImported(IProgressMonitor monitor) {
this.preferenceManager.getPreferences().updateAnnotationNullAnalysisOptions();
}

public static void cleanInvalidJavaProjects(IProgressMonitor monitor) {
for (IJavaProject javaProject : ProjectUtils.getJavaProjects()) {
IProject project = javaProject.getProject();
if (project.equals(getDefaultProject())) {
continue;
}
IFile classpathFile = project.getFile(IJavaProject.CLASSPATH_FILE_NAME);
if (!classpathFile.exists()) {
try {
JavaLanguageServerPlugin.logInfo("The '" + project.getName() + "' project has no .classpath. Removing Java nature and builder.");
ProjectUtils.removeJavaNatureAndBuilder(project, monitor);
} catch (CoreException e) {
JavaLanguageServerPlugin.logException(e.getMessage(), e);
}
}
}
}

@Override
public void projectsBuildFinished(IProgressMonitor monitor) {
this.buildFinished = true;
Expand Down
Empty file.
30 changes: 30 additions & 0 deletions org.eclipse.jdt.ls.tests/projects/maven/invalid2/module1/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<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>
<parent>
<groupId>foo.bar</groupId>
<artifactId>invalid2</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>module1</artifactId>
<packaging>jar</packaging>

<build>
<resources>
<resource>
<targetPath>module1</targetPath>
<directory>../</directory>
<includes>
<include>LICENSE.txt</include>
</includes>
</resource>
<resource>
<targetPath>module1</targetPath>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>


</project>
19 changes: 19 additions & 0 deletions org.eclipse.jdt.ls.tests/projects/maven/invalid2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<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>foo.bar</groupId>
<artifactId>invalid2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<revision>4.11</revision>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<modules>
<module>module1</module>
</modules>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,23 @@ public void testImportModulesWithSameArtifactId() throws Exception {
assertTrue(Arrays.stream(projects).anyMatch(p -> p.getName().equals("com.example.three-my-app")));
}

// https://github.com/redhat-developer/vscode-java/issues/3639
@Test
public void testInvalidProject() throws Exception {
try {
importMavenProject("invalid2");
} catch (Exception e) {
// ignore
}
IProject[] projects = ProjectUtils.getAllProjects(false);
assertEquals(2, projects.length); // invalid2 & module1
IJavaProject[] javaProjects = ProjectUtils.getJavaProjects();
assertEquals(1, javaProjects.length);
StandardProjectsManager.cleanInvalidJavaProjects(new NullProgressMonitor());
javaProjects = ProjectUtils.getJavaProjects();
assertEquals(0, javaProjects.length);
}

private static class MavenUpdateProjectJobSpy extends JobChangeAdapter {

int updateProjectJobCalled;
Expand Down

0 comments on commit 715286a

Please sign in to comment.