Skip to content

Create the default project only when it is necessary. #3140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ static ICompilationUnit getFakeCompilationUnit(URI uri, IProgressMonitor monitor
return null;
}

try {
ProjectsManager.createJavaProject(ProjectsManager.getDefaultProject(), new NullProgressMonitor());
ProjectsManager.cleanupResources(ProjectsManager.getDefaultProject());
} catch (Exception e) {
// continue
}
IProject project = JavaLanguageServerPlugin.getProjectsManager().getDefaultProject();
if (project == null || !project.isAccessible()) {
String fileName = path.getFileName().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public BuildWorkspaceStatus buildWorkspace(boolean forceReBuild, IProgressMonito
if (monitor.isCanceled()) {
return BuildWorkspaceStatus.CANCELLED;
}
projectsManager.cleanupResources(projectsManager.getDefaultProject());
if (ProjectUtils.getAllProjects(false).length == 0) {
ProjectsManager.cleanupResources(ProjectsManager.getDefaultProject());
}
if (forceReBuild) {
SubMonitor subMonitor = SubMonitor.convert(monitor, 100);
ResourcesPlugin.getWorkspace().build(IncrementalProjectBuilder.CLEAN_BUILD, subMonitor.split(50));
Expand All @@ -67,7 +69,7 @@ public BuildWorkspaceStatus buildWorkspace(boolean forceReBuild, IProgressMonito
List<IMarker> problemMarkers = new ArrayList<>();
IProject[] projects = ProjectUtils.getAllProjects();
for (IProject project : projects) {
if (!project.equals(projectsManager.getDefaultProject())) {
if (!project.equals(ProjectsManager.getDefaultProject())) {
List<IMarker> markers = ResourceUtils.getErrorMarkers(project);
if (markers != null) {
problemMarkers.addAll(markers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -51,15 +50,14 @@
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaModelStatus;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.internal.core.ClasspathEntry;
import org.eclipse.jdt.ls.core.internal.AbstractProjectImporter;
import org.eclipse.jdt.ls.core.internal.IConstants;
import org.eclipse.jdt.ls.core.internal.JDTUtils;
Expand Down Expand Up @@ -102,6 +100,9 @@ public void importToWorkspace(IProgressMonitor monitor) throws OperationCanceled
return;
}

ProjectsManager.createJavaProject(ProjectsManager.getDefaultProject(), new NullProgressMonitor());
ProjectsManager.cleanupResources(ProjectsManager.getDefaultProject());

IPath rootPath = ResourceUtils.filePathFromURI(rootFolder.toPath().toUri().toString());
Optional<IPath> triggerJavaFile = triggerFiles.stream().filter(triggerFile -> rootPath.isPrefixOf(triggerFile)).findFirst();
if (!triggerJavaFile.isPresent()) {
Expand Down Expand Up @@ -201,7 +202,7 @@ private static Collection<IPath> collectSourcePaths(IPath triggerFilePath, IFold
return Collections.emptySet();
}
Collection<IPath> triggerFiles = collectTriggerFiles(currentProject, foldersToSearch);

Set<IPath> sourcePaths = new HashSet<>();
sourcePaths.add(triggerFolder.getFullPath());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,10 @@ public void initializeProjects(final Collection<IPath> rootPaths, IProgressMonit
if (!preferenceManager.getClientPreferences().skipProjectConfiguration()) {
SubMonitor subMonitor = SubMonitor.convert(monitor, 100);
cleanInvalidProjects(rootPaths, subMonitor.split(20));
createJavaProject(getDefaultProject(), subMonitor.split(10));
cleanupResources(getDefaultProject());
if (rootPaths.isEmpty() && !ProjectsManager.getDefaultProject().exists()) {
ProjectsManager.createJavaProject(ProjectsManager.getDefaultProject(), subMonitor.split(10));
ProjectsManager.cleanupResources(ProjectsManager.getDefaultProject());
}
Collection<IPath> projectConfigurations = preferenceManager.getPreferences().getProjectConfigurations();
if (projectConfigurations == null) {
// old way to import project
Expand Down Expand Up @@ -336,7 +338,7 @@ public IStatus runInWorkspace(IProgressMonitor monitor) {
return job;
}

public void cleanupResources(IProject project) throws CoreException {
public static void cleanupResources(IProject project) throws CoreException {
IJavaProject javaProj = JavaCore.create(project);
if (javaProj == null) {
return;
Expand Down Expand Up @@ -724,7 +726,7 @@ public IStatus runInWorkspace(IProgressMonitor monitor) {
for (Entry<IBuildSupport, List<IProject>> entry : groupByBuildSupport(projects).entrySet()) {
IStatus onWillUpdateStatus = onWillConfigurationUpdate(entry.getKey(),
entry.getValue(), monitor);

// if onWillUpdate() failed, skip updating the projects.
if (!onWillUpdateStatus.isOK()) {
status.add(onWillUpdateStatus);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal;

import static org.eclipse.jdt.ls.core.internal.ProjectUtils.getJavaSourceLevel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
Expand All @@ -30,6 +31,7 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.URIUtil;
import org.eclipse.jdt.core.IJavaProject;
Expand All @@ -42,11 +44,13 @@
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.launching.LibraryLocation;
import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
import org.eclipse.jdt.ls.core.internal.handlers.WorkspaceSymbolHandler;
import org.eclipse.jdt.ls.core.internal.managers.AbstractInvisibleProjectBasedTest;
import org.eclipse.jdt.ls.core.internal.preferences.ClientPreferences;
import org.eclipse.jdt.ls.core.internal.preferences.Preferences;
import org.eclipse.lsp4j.MessageParams;
import org.eclipse.lsp4j.MessageType;
import org.eclipse.lsp4j.SymbolInformation;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -246,6 +250,18 @@ public void testInvalidRuntime() throws Exception {
assertEquals("Invalid runtime for " + runtime.getName() + ": 'bin' should be removed from the path (" + runtime.getPath() + ").", notification.getMessage());
}

// https://github.com/redhat-developer/vscode-java/issues/3452
@Test
public void testJavaRuntimesDoNotLeak() throws Exception {
importProjects("maven/salut-java11");
IProject project = WorkspaceHelper.getProject("salut-java11");
assertIsJavaProject(project);
assertEquals("11", getJavaSourceLevel(project));
List<SymbolInformation> results = WorkspaceSymbolHandler.search("java.lang.Object", new NullProgressMonitor());
int numOfObjectSymbols = results.stream().filter(s -> "java.lang".equals(s.getContainerName()) && "Object".equals(s.getName())).toList().size();
assertEquals(1, numOfObjectSymbols);
}

private void assertComplianceAndPreviewSupport(IJavaProject javaProject, String compliance, boolean previewEnabled) {
assertEquals(previewEnabled ? JavaCore.ENABLED : JavaCore.DISABLED, javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true));
assertEquals(compliance, javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ public void testIsTestFileForGradle() throws Exception {
public void getAllJavaProject() throws Exception {
importProjects("maven/multimodule");
List<URI> projects = ProjectCommand.getAllJavaProjects();
assertEquals(4, projects.size());
assertEquals(3, projects.size());
}

private static Range START_OF_DOCUMENT = new Range(new Position(0, 0), new Position(0, 0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void testMetadataFileLocation2() throws Exception {
@Test
public void testSettingsGradle() throws Exception {
List<IProject> projects = importProjects("gradle/sample");
assertEquals(3, projects.size());//default, app, sample
assertEquals(2, projects.size()); // app, sample
IProject root = WorkspaceHelper.getProject("sample");
assertIsGradleProject(root);
IProject project = WorkspaceHelper.getProject("app");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void testMetadataFileSync() throws Exception {
@Test
public void testInvalidProject() throws Exception {
List<IProject> projects = importProjects(MAVEN_INVALID);
assertEquals(2, projects.size());
assertEquals(1, projects.size());
IProject invalid = WorkspaceHelper.getProject(INVALID);
assertIsMavenProject(invalid);
IFile projectFile = invalid.getFile(IProjectDescription.DESCRIPTION_FILE_NAME);
Expand All @@ -123,7 +123,7 @@ public void testInvalidProject() throws Exception {
file.delete();
assertFalse(file.exists());
projects = importProjects(MAVEN_INVALID);
assertEquals(2, projects.size());
assertEquals(1, projects.size());
invalid = WorkspaceHelper.getProject(INVALID);
assertIsMavenProject(invalid);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void testParallelBuildForEclipseProjects() throws Exception
preferences.setMaxBuildCount(4);

List<IProject> projects = importProjects("eclipse/multi");
assertEquals(3, projects.size());
assertEquals(2, projects.size());

BuildWorkspaceStatus result = handler.buildWorkspace(false, monitor);
assertEquals(String.format("BuildWorkspaceStatus is: %s.", result.toString()), result, BuildWorkspaceStatus.SUCCEED);
Expand All @@ -98,7 +98,7 @@ public void testParallelBuildSupport() throws Exception {
preferences.setMaxBuildCount(4);

List<IProject> projects = importProjects("maven/multimodule");
assertEquals(6, projects.size());
assertEquals(5, projects.size());

BuildWorkspaceStatus result = handler.buildWorkspace(false, monitor);
assertEquals(String.format("BuildWorkspaceStatus is: %s.", result.toString()), result, BuildWorkspaceStatus.SUCCEED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void testImportNewMavenProjects() throws Exception {
importProjects("maven/multimodule");
waitForJobs();
projects = workspace.getRoot().getProjects();
assertEquals(6, projects.length);
assertEquals(5, projects.length);

// Add new sub-module
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject("multimodule");
Expand Down Expand Up @@ -103,7 +103,7 @@ public void testImportNewMavenProjects() throws Exception {

// Verify no projects imported
projects = workspace.getRoot().getProjects();
assertEquals(6, projects.length);
assertEquals(5, projects.length);

// Verify import projects
projectsManager.setConnection(client);
Expand All @@ -112,7 +112,7 @@ public void testImportNewMavenProjects() throws Exception {
IProject newProject = workspace.getRoot().getProject("module4");
assertTrue(newProject.exists());
projects = workspace.getRoot().getProjects();
assertEquals(7, projects.length);
assertEquals(6, projects.length);

ArgumentCaptor<EventNotification> argument = ArgumentCaptor.forClass(EventNotification.class);
verify(client, times(1)).sendEventNotification(argument.capture());
Expand All @@ -130,7 +130,7 @@ public void testManualImportNewMavenProjects() throws Exception {
importProjects("maven/multimodule");
waitForJobs();
projects = workspace.getRoot().getProjects();
assertEquals(6, projects.length);
assertEquals(5, projects.length);

// Add new sub-module
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject("multimodule");
Expand Down Expand Up @@ -161,7 +161,7 @@ public void testManualImportNewMavenProjects() throws Exception {

// Verify no projects imported
projects = workspace.getRoot().getProjects();
assertEquals(6, projects.length);
assertEquals(5, projects.length);

// Verify import projects
projectsManager.setConnection(client);
Expand All @@ -173,7 +173,7 @@ public void testManualImportNewMavenProjects() throws Exception {
IProject newProject = workspace.getRoot().getProject("module4");
assertTrue("New module is imported", newProject.exists());
projects = workspace.getRoot().getProjects();
assertEquals(7, projects.length);
assertEquals(6, projects.length);

ArgumentCaptor<EventNotification> argument = ArgumentCaptor.forClass(EventNotification.class);
verify(client, times(1)).sendEventNotification(argument.capture());
Expand All @@ -192,7 +192,7 @@ public void testImportNewGradleProjects() throws Exception {
importProjects("gradle/multi-module");
waitForJobs();
projects = workspace.getRoot().getProjects();
assertEquals(4, projects.length);
assertEquals(3, projects.length);
// Add new sub-module
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject("multi-module");
File projectBasePath = project.getLocation().toFile();
Expand All @@ -207,7 +207,7 @@ public void testImportNewGradleProjects() throws Exception {

// Verify no projects imported
projects = workspace.getRoot().getProjects();
assertEquals(4, projects.length);
assertEquals(3, projects.length);

// Verify import projects
projectsManager.setConnection(client);
Expand All @@ -216,7 +216,7 @@ public void testImportNewGradleProjects() throws Exception {
IProject newProject = workspace.getRoot().getProject("test");
assertTrue(newProject.exists());
projects = workspace.getRoot().getProjects();
assertEquals(5, projects.length);
assertEquals(4, projects.length);

ArgumentCaptor<EventNotification> argument = ArgumentCaptor.forClass(EventNotification.class);
verify(client, times(1)).sendEventNotification(argument.capture());
Expand All @@ -231,7 +231,7 @@ public void testImportMixedProjects() throws Exception {
IProject[] projects = workspace.getRoot().getProjects();
assertEquals(0, projects.length);
importProjects("mixed");
assertEquals(4, wsRoot.getProjects().length);
assertEquals(3, wsRoot.getProjects().length);
IProject hello = wsRoot.getProject("hello");
assertNotNull(hello);
assertIsJavaProject(hello);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,12 @@ public class NavigateToDefinitionHandlerTest extends AbstractProjectsManagerBase

private NavigateToDefinitionHandler handler;
private IProject project;
private IProject defaultProject;

@Before
public void setUp() throws Exception {
handler = new NavigateToDefinitionHandler(preferenceManager);
importProjects("maven/salut");
project = WorkspaceHelper.getProject("salut");
defaultProject = linkFilesToDefaultProject("singlefile/Single.java").getProject();
Job.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_BUILD, monitor);
Job.getJobManager().join(ResourcesPlugin.FAMILY_MANUAL_BUILD, monitor);
Job.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_REFRESH, monitor);
Expand Down Expand Up @@ -144,6 +142,7 @@ public void testMethodInAnonymousClass2() throws Exception {
public void testJdkClasses() throws Exception {
// because for test, we are using fake rt.jar(rtstubs.jar), the issue of issue https://bugs.eclipse.org/bugs/show_bug.cgi?id=541573 will
// never occur in test cases
IProject defaultProject = linkFilesToDefaultProject("singlefile/Single.java").getProject();
String uri = ClassFileUtil.getURI(defaultProject, "Single");
TextDocumentIdentifier identifier = new TextDocumentIdentifier(uri);
handler.definition(new TextDocumentPositionParams(identifier, new Position(1, 31)), monitor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void testWorkspaceSearch() {
String query = "Array";
List<SymbolInformation> results = WorkspaceSymbolHandler.search(query, monitor);
assertNotNull(results);
assertEquals("Unexpected results", 22, results.size());
assertEquals("Unexpected results", 11, results.size());
Range defaultRange = JDTUtils.newRange();
for (SymbolInformation symbol : results) {
assertNotNull("Kind is missing", symbol.getKind());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.eclipse.core.runtime.IRegistryChangeEvent;
import org.eclipse.core.runtime.IRegistryChangeListener;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.jobs.Job;
Expand Down Expand Up @@ -211,6 +212,12 @@ protected IJavaProject newDefaultProject() throws Exception {
}

protected IFile linkFilesToDefaultProject(String path) throws Exception {
try {
ProjectsManager.createJavaProject(ProjectsManager.getDefaultProject(), new NullProgressMonitor());
ProjectsManager.cleanupResources(ProjectsManager.getDefaultProject());
} catch (OperationCanceledException e) {
} catch (CoreException e) {
}
IProject testProject = ProjectsManager.getDefaultProject();
String fullpath = copyFiles(path, true).getAbsolutePath().replace('\\', '/');
String fileName = fullpath.substring(fullpath.lastIndexOf("/") + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void ignoreMissingResourceFilters() throws Exception {
@Test
public void importMultipleJavaProject() throws Exception {
List<IProject> projects = importProjects("eclipse/multi");
assertEquals(3, projects.size());
assertEquals(2, projects.size());

IProject bar = getProject("bar");
assertIsJavaProject(bar);
Expand Down Expand Up @@ -123,7 +123,7 @@ public void testJavaImportExclusions() throws Exception {
try {
javaImportExclusions.add(BAR_PATTERN);
List<IProject> projects = importProjects("eclipse/multi");
assertEquals(2, projects.size());
assertEquals(1, projects.size());
IProject bar = getProject("bar");
assertNull(bar);
IProject foo = getProject("foo");
Expand Down
Loading
Loading