From d2373f7d8935f3659e99ab86a4c54ecef1c100e7 Mon Sep 17 00:00:00 2001 From: Jinbo Wang Date: Wed, 18 Oct 2023 20:00:49 +0800 Subject: [PATCH] Fix the error 'Invalid project description' caused by incorrect glob patterns for Windows paths Signed-off-by: Jinbo Wang --- .../managers/EclipseProjectImporter.java | 10 +++++++++- .../managers/GradleProjectImporter.java | 14 +++++--------- .../internal/managers/MavenProjectImporter.java | 2 +- .../managers/EclipseProjectImporterTest.java | 12 ++++++++++++ .../managers/GradleProjectImporterTest.java | 17 +++++++++++++++++ .../managers/MavenProjectImporterTest.java | 17 +++++++++++++++++ 6 files changed, 61 insertions(+), 11 deletions(-) diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/EclipseProjectImporter.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/EclipseProjectImporter.java index ca7fff7bb5..4ca464ac9f 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/EclipseProjectImporter.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/EclipseProjectImporter.java @@ -48,7 +48,15 @@ public boolean applies(IProgressMonitor monitor) throws CoreException { .addExclusions("**/bin");//default Eclipse build dir for (IProject project : ProjectUtils.getAllProjects(false)) { File projectFile = project.getLocation().toFile(); - eclipseDetector.addExclusions(projectFile.getAbsolutePath()); + /** + * The exclusion pattern will be used as a regular expression + * that matches the files or folders to be excluded. On Windows, + * the path separator (\) is a special character in regular + * expressions, so it needs to be escaped with another backslash (\) + * to be treated literally. For example, to exclude the folder + * C:\Users\Hello, the exclusion pattern should be C:\\Users\\Hello. + */ + eclipseDetector.addExclusions(projectFile.getAbsolutePath().replace("\\", "\\\\")); } directories = eclipseDetector.scan(monitor); } diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradleProjectImporter.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradleProjectImporter.java index ab9cff8f8f..8def245767 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradleProjectImporter.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradleProjectImporter.java @@ -148,7 +148,7 @@ public boolean applies(IProgressMonitor monitor) throws CoreException { for (IProject project : ProjectUtils.getAllProjects()) { if (!ProjectUtils.isGradleProject(project)) { String path = project.getLocation().toOSString(); - gradleDetector.addExclusions(path); + gradleDetector.addExclusions(path.replace("\\", "\\\\")); } } directories = gradleDetector.scan(monitor); @@ -552,15 +552,11 @@ private static boolean checkGradlePersistence(IProject project, File projectDir) if (persistentFile.exists()) { long modified = persistentFile.lastModified(); if (projectDir.exists()) { - File[] files = projectDir.listFiles(new FilenameFilter() { - - @Override - public boolean accept(File dir, String name) { - if (name != null && GradleBuildSupport.GRADLE_FILE_EXT.matcher(name).matches()) { - return new File(dir, name).lastModified() > modified; - } - return false; + File[] files = projectDir.listFiles((FilenameFilter) (dir, name) -> { + if (name != null && GradleBuildSupport.GRADLE_FILE_EXT.matcher(name).matches()) { + return new File(dir, name).lastModified() > modified; } + return false; }); shouldSynchronize = files != null && files.length > 0; } diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/MavenProjectImporter.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/MavenProjectImporter.java index c9d37bb2ac..6316bdd861 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/MavenProjectImporter.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/MavenProjectImporter.java @@ -99,7 +99,7 @@ public boolean applies(IProgressMonitor monitor) throws OperationCanceledExcepti for (IProject project : ProjectUtils.getAllProjects()) { if (!ProjectUtils.isMavenProject(project)) { String path = project.getLocation().toOSString(); - mavenDetector.addExclusions(path); + mavenDetector.addExclusions(path.replace("\\", "\\\\")); } } directories = mavenDetector.scan(monitor); diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/EclipseProjectImporterTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/EclipseProjectImporterTest.java index 080793b4cf..7463889d0e 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/EclipseProjectImporterTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/EclipseProjectImporterTest.java @@ -281,6 +281,18 @@ public void testNullAnalysis() throws Exception { } } + @Test + public void DoNotDuplicateImportProject() throws Exception { + String name = "hello"; + importProjects("eclipse/"+name); + IProject project = getProject(name ); + assertIsJavaProject(project); + EclipseProjectImporter importer = new EclipseProjectImporter(); + importer.initialize(project.getLocation().toFile()); + boolean hasUnimportedProjects = importer.applies(new NullProgressMonitor()); + assertFalse(hasUnimportedProjects); + } + @After public void after() { importer = null; diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/GradleProjectImporterTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/GradleProjectImporterTest.java index 6020cead29..12428ad8b6 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/GradleProjectImporterTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/GradleProjectImporterTest.java @@ -565,6 +565,23 @@ public void avoidImportDuplicatedProjects() throws Exception { } } + @Test + public void avoidImportDuplicatedProjects2() throws Exception { + try { + this.preferences.setImportGradleEnabled(false); + importProjects("multi-buildtools"); + IProject project = getProject("multi-build-tools"); + assertIsJavaProject(project); + GradleProjectImporter importer = new GradleProjectImporter(); + importer.initialize(project.getLocation().toFile()); + + this.preferences.setImportGradleEnabled(true); + assertFalse(importer.applies(null)); + } finally { + this.preferences.setImportGradleEnabled(true); + } + } + @Test public void testProtoBufSupport() throws Exception { try { 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 1c6b0befeb..a068dda651 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 @@ -378,6 +378,23 @@ public void avoidImportDuplicatedProjects() throws Exception { } } + @Test + public void avoidImportDuplicatedProjects2() throws Exception { + try { + this.preferences.setImportMavenEnabled(false); + importProjects("multi-buildtools"); + IProject project = WorkspaceHelper.getProject("multi-buildtools"); + assertIsJavaProject(project); + MavenProjectImporter importer = new MavenProjectImporter(); + importer.initialize(project.getLocation().toFile()); + + this.preferences.setImportMavenEnabled(true); + assertFalse(importer.applies(null)); + } finally { + this.preferences.setImportMavenEnabled(true); + } + } + // https://github.com/redhat-developer/vscode-java/issues/2712 @Test public void testNullAnalysisDisabled() throws Exception {