From c5435e46f465c145c98a87d85578486c7624d227 Mon Sep 17 00:00:00 2001 From: Snjezana Peco Date: Tue, 2 Jul 2024 16:24:22 +0200 Subject: [PATCH] Clean up invalid projects during the initialized phase --- .../internal/handlers/JDTLanguageServer.java | 2 ++ .../managers/StandardProjectsManager.java | 18 +++++++++++ .../projects/maven/invalid2/LICENSE.txt | 0 .../projects/maven/invalid2/module1/pom.xml | 30 +++++++++++++++++++ .../projects/maven/invalid2/pom.xml | 19 ++++++++++++ .../managers/MavenProjectImporterTest.java | 17 +++++++++++ 6 files changed, 86 insertions(+) create mode 100644 org.eclipse.jdt.ls.tests/projects/maven/invalid2/LICENSE.txt create mode 100644 org.eclipse.jdt.ls.tests/projects/maven/invalid2/module1/pom.xml create mode 100644 org.eclipse.jdt.ls.tests/projects/maven/invalid2/pom.xml diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/JDTLanguageServer.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/JDTLanguageServer.java index 43d3e6b14b..ea799410f3 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/JDTLanguageServer.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/JDTLanguageServer.java @@ -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; @@ -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); diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/StandardProjectsManager.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/StandardProjectsManager.java index c98897ff3c..bb93a6bd1b 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/StandardProjectsManager.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/StandardProjectsManager.java @@ -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; diff --git a/org.eclipse.jdt.ls.tests/projects/maven/invalid2/LICENSE.txt b/org.eclipse.jdt.ls.tests/projects/maven/invalid2/LICENSE.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/org.eclipse.jdt.ls.tests/projects/maven/invalid2/module1/pom.xml b/org.eclipse.jdt.ls.tests/projects/maven/invalid2/module1/pom.xml new file mode 100644 index 0000000000..7a0843a460 --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/maven/invalid2/module1/pom.xml @@ -0,0 +1,30 @@ + + 4.0.0 + + foo.bar + invalid2 + 0.0.1-SNAPSHOT + + module1 + jar + + + + + module1 + ../ + + LICENSE.txt + + + + module1 + src/main/resources + + + + + + diff --git a/org.eclipse.jdt.ls.tests/projects/maven/invalid2/pom.xml b/org.eclipse.jdt.ls.tests/projects/maven/invalid2/pom.xml new file mode 100644 index 0000000000..25bf24ccbc --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/maven/invalid2/pom.xml @@ -0,0 +1,19 @@ + + 4.0.0 + foo.bar + invalid2 + 0.0.1-SNAPSHOT + pom + + 4.11 + UTF-8 + 17 + 17 + + + module1 + + + diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/MavenProjectImporterTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/MavenProjectImporterTest.java index e368f674de..2b7b6892f7 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/MavenProjectImporterTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/MavenProjectImporterTest.java @@ -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;