Skip to content
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

Fix failed to find root path when workspace changes #1689

Merged
merged 1 commit into from
Mar 16, 2021
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 @@ -47,11 +47,12 @@ public void preferencesChange(Preferences oldPreferences, Preferences newPrefere

try {
IFolder workspaceLinkFolder = javaProject.getProject().getFolder(ProjectUtils.WORKSPACE_LINK);
if (!workspaceLinkFolder.exists()) {
IPath rootPath = ProjectUtils.findBelongedWorkspaceRoot(workspaceLinkFolder.getLocation());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if workspaceLinkFolder does not exist, what is returned by workspaceLinkFolder.getLocation()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It depends.

If it's the linked folder of an invisible project, it will return the linked folder path. If it's a visible project, it just appends _, for example: D:/Workspace/spring-petclinic/_

Why .exists() does not work is that, it just check if the handle of the resource exists, not check the real one. Below is part of the document copied from .exists():

IResource objects are lightweight handle objects used to access resources in the workspace. However, having a handle object does not necessarily mean the workspace really has such a resource.

if (rootPath == null) {
continue;
}
List<IPath> sourcePaths = InvisibleProjectImporter.getSourcePaths(newPreferences.getInvisibleProjectSourcePaths(), workspaceLinkFolder);
List<IPath> excludingPaths = InvisibleProjectImporter.getExcludingPath(javaProject, null, workspaceLinkFolder);
List<IPath> excludingPaths = InvisibleProjectImporter.getExcludingPath(javaProject, rootPath, workspaceLinkFolder);
IPath outputPath = InvisibleProjectImporter.getOutputPath(javaProject, newPreferences.getInvisibleProjectOutputPath(), true /*isUpdate*/);
IClasspathEntry[] classpathEntries = InvisibleProjectImporter.resolveClassPathEntries(javaProject, sourcePaths, excludingPaths, outputPath);
javaProject.setRawClasspath(classpathEntries, outputPath, new NullProgressMonitor());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
Expand Down Expand Up @@ -117,4 +118,25 @@ public void testUpdateSourcePaths() throws Exception {
assertTrue(sourcePaths.contains("src"));
assertTrue(sourcePaths.contains("src2"));
}
}

@Test
public void testWhenRootPathChanged() throws Exception {
JavaLanguageClient client = mock(JavaLanguageClient.class);
ProjectsManager pm = JavaLanguageServerPlugin.getProjectsManager();
pm.setConnection(client);
doNothing().when(client).showMessage(any(MessageParams.class));
copyAndImportFolder("singlefile/simple", "src/App.java");

List<IPath> rootPaths = new ArrayList<>(preferenceManager.getPreferences().getRootPaths());
rootPaths.remove(0);
preferenceManager.getPreferences().setRootPaths(rootPaths);

Preferences newPreferences = new Preferences();
initPreferences(newPreferences);
newPreferences.setInvisibleProjectSourcePaths(Arrays.asList("src", "src2"));
InvisibleProjectPreferenceChangeListener listener = new InvisibleProjectPreferenceChangeListener();
listener.preferencesChange(preferenceManager.getPreferences(), newPreferences);

verify(client, times(0)).showMessage(any(MessageParams.class));
}
}