From 6c771bbb24d5dcfb2841760ae335ecbbbbc23059 Mon Sep 17 00:00:00 2001 From: Snjezana Peco Date: Tue, 2 Jul 2024 16:24:22 +0200 Subject: [PATCH] Fix build loop --- .../internal/handlers/JDTLanguageServer.java | 1 + .../internal/managers/IProjectsManager.java | 8 +++++ .../managers/StandardProjectsManager.java | 19 ++++++++++++ .../projects/maven/invalid2/LICENSE.txt | 0 .../projects/maven/invalid2/module1/pom.xml | 30 +++++++++++++++++++ .../projects/maven/invalid2/pom.xml | 19 ++++++++++++ .../managers/MavenProjectImporterTest.java | 17 +++++++++++ 7 files changed, 94 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..5e4df5ea9d 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 @@ -301,6 +301,7 @@ public void initialized(InitializedParams params) { @Override public IStatus run(IProgressMonitor monitor) { try { + pm.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/IProjectsManager.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/IProjectsManager.java index 52a77b2ad8..2e55dd0c03 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/IProjectsManager.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/IProjectsManager.java @@ -126,4 +126,12 @@ default void setShouldUpdateProjects(boolean update) { // do nothing } + /** + * Clean up the invalid Java projects + * https://github.com/redhat-developer/vscode-java/issues/3639 + */ + default void cleanInvalidJavaProjects(IProgressMonitor monitor) { + // do nothing + } + } 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..7191ebee6e 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,25 @@ public void projectsImported(IProgressMonitor monitor) { this.preferenceManager.getPreferences().updateAnnotationNullAnalysisOptions(); } + @Override + public 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..f741f09d19 --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/maven/invalid2/module1/pom.xml @@ -0,0 +1,30 @@ + + 4.0.0 + + foo.bar + multimodule + 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..0f24bf8eaa --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/maven/invalid2/pom.xml @@ -0,0 +1,19 @@ + + 4.0.0 + foo.bar + multimodule + 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..86383fbf82 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); + projectsManager.cleanInvalidJavaProjects(new NullProgressMonitor()); + javaProjects = ProjectUtils.getJavaProjects(); + assertEquals(0, javaProjects.length); + } + private static class MavenUpdateProjectJobSpy extends JobChangeAdapter { int updateProjectJobCalled;