Skip to content

Commit

Permalink
fix - Create linked folder for resource if it's placed out of the pro…
Browse files Browse the repository at this point in the history
…ject (#1575)
  • Loading branch information
jdneo authored Aug 9, 2024
1 parent 57d4a77 commit 4473d70
Showing 1 changed file with 41 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public void updateClasspath(BuildServerConnection connection, IProject project,
if (resourceOutputFullPath != null) {
ResourcesResult resourcesResult = connection.buildTargetResources(
new ResourcesParams(Arrays.asList(buildTarget.getId()))).join();
List<IClasspathEntry> resourceEntries = getResourceEntries(project, resourcesResult, resourceOutputFullPath, isTest);
List<IClasspathEntry> resourceEntries = getResourceEntries(rootPath, project, resourcesResult, resourceOutputFullPath, isTest, monitor);
for (IClasspathEntry entry : resourceEntries) {
classpathMap.putIfAbsent(entry.getPath(), entry);
}
Expand Down Expand Up @@ -355,23 +355,9 @@ private List<IClasspathEntry> getSourceEntries(IPath rootPath, IProject project,
for (SourcesItem sources : sourcesResult.getItems()) {
for (SourceItem source : sources.getSources()) {
IPath sourcePath = ResourceUtils.filePathFromURI(source.getUri());
IPath projectLocation = project.getLocation();
IPath sourceFullPath;
if (projectLocation.isPrefixOf(sourcePath)) {
IPath relativeSourcePath = sourcePath.makeRelativeTo(project.getLocation());
sourceFullPath = project.getFolder(relativeSourcePath).getFullPath();
} else {
// if the source path is not relative to the project location, we need to create a linked folder for it.
IPath relativeSourcePath = sourcePath.makeRelativeTo(rootPath);
if (relativeSourcePath.isAbsolute()) {
JavaLanguageServerPlugin.logError("The source path is not relative to the workspace root: " + relativeSourcePath);
continue;
}
IFolder linkFolder = project.getFolder(String.join("_", relativeSourcePath.segments()));
if (!linkFolder.exists()) {
linkFolder.createLink(sourcePath, IResource.REPLACE | IResource.ALLOW_MISSING_LOCAL, monitor);
}
sourceFullPath = linkFolder.getFullPath();
IPath sourceFullPath = getFullPath(rootPath, project, sourcePath, monitor);
if (sourceFullPath == null) {
continue;
}
// Continue if this source path has already been added.
if (sourceEntries.stream().anyMatch(entry -> entry.getPath().equals(sourceFullPath))) {
Expand Down Expand Up @@ -400,13 +386,19 @@ private List<IClasspathEntry> getSourceEntries(IPath rootPath, IProject project,
return sourceEntries;
}

private List<IClasspathEntry> getResourceEntries(IProject project, ResourcesResult resourcesResult, IPath outputFullPath, boolean isTest) {
private List<IClasspathEntry> getResourceEntries(IPath rootPath, IProject project, ResourcesResult resourcesResult, IPath outputFullPath, boolean isTest, IProgressMonitor monitor) throws CoreException {
List<IClasspathEntry> resourceEntries = new LinkedList<>();
for (ResourcesItem resources : resourcesResult.getItems()) {
for (String resourceUri : resources.getResources()) {
IPath resourcePath = ResourceUtils.filePathFromURI(resourceUri);
IPath relativeResourcePath = resourcePath.makeRelativeTo(project.getLocation());
IPath resourceFullPath = project.getFolder(relativeResourcePath).getFullPath();
IPath resourceFullPath = getFullPath(rootPath, project, resourcePath, monitor);
if (resourceFullPath == null) {
continue;
};
// Continue if this resource path has already been added.
if (resourceEntries.stream().anyMatch(entry -> entry.getPath().equals(resourceFullPath))) {
continue;
}
List<IClasspathAttribute> classpathAttributes = new LinkedList<>();
if (isTest) {
classpathAttributes.add(testAttribute);
Expand All @@ -428,6 +420,34 @@ private List<IClasspathEntry> getResourceEntries(IProject project, ResourcesResu
return resourceEntries;
}

/**
* Get the full path which is used in the classpath entry for the input path
* @param rootPath the workspace root path.
* @param project the project.
* @param path the input path.
*/
private IPath getFullPath(IPath rootPath, IProject project, IPath path, IProgressMonitor monitor) throws CoreException {
IPath projectLocation = project.getLocation();
IPath fullPath;
if (projectLocation.isPrefixOf(path)) {
IPath relativeSourcePath = path.makeRelativeTo(project.getLocation());
fullPath = project.getFolder(relativeSourcePath).getFullPath();
} else {
// if the path is not relative to the project location, we need to create a linked folder for it.
IPath relativePath = path.makeRelativeTo(rootPath);
if (relativePath.isAbsolute()) {
JavaLanguageServerPlugin.logError("The path is not relative to the workspace root: " + relativePath);
return null;
}
IFolder linkFolder = project.getFolder(String.join("_", relativePath.segments()));
if (!linkFolder.exists()) {
linkFolder.createLink(path, IResource.REPLACE | IResource.ALLOW_MISSING_LOCAL, monitor);
}
fullPath = linkFolder.getFullPath();
}
return fullPath;
}

private void moveTestTargetsToEnd(List<BuildTarget> buildTargets) {
buildTargets.sort((bt1, bt2) -> {
return Boolean.compare(bt1.getTags().contains(BuildTargetTag.TEST),
Expand Down

0 comments on commit 4473d70

Please sign in to comment.