Skip to content

Commit

Permalink
Only send request after initialization (#1503)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdneo committed Jun 19, 2024
1 parent 2905593 commit 1185a1f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ public void onWillUpdate(Collection<IProject> projects, IProgressMonitor monitor
}
for (IPath rootPath : roots) {
BuildServerConnection connection = ImporterPlugin.getBuildServerConnection(rootPath);
if (connection == null) {
JavaLanguageServerPlugin.logInfo("Skip reloading " + rootPath + " because the connection is not available.");
continue;
}
connection.workspaceReload().join();
}
}
Expand All @@ -156,14 +160,18 @@ public void update(IProject project, boolean force, IProgressMonitor monitor) th
return;
}
BuildServerConnection connection = ImporterPlugin.getBuildServerConnection(rootPath);
if (connection == null) {
JavaLanguageServerPlugin.logError("Cannot find build server connection for root: " + rootPath);
return;
}
Map<URI, List<BuildTarget>> buildTargetMap = Utils.getBuildTargetsMappedByProjectPath(connection);
for (URI uri : buildTargetMap.keySet()) {
IProject projectFromUri = ProjectUtils.getProjectFromUri(uri.toString());
if (projectFromUri == null || !Utils.isGradleBuildServerProject(projectFromUri)) {
continue;
}
updateClasspath(projectFromUri, monitor);
updateProjectDependencies(projectFromUri, monitor);
updateClasspath(connection, projectFromUri, monitor);
updateProjectDependencies(connection, projectFromUri, monitor);
// TODO: in case that the projects/build targets are created or removed,
// we can use the server->client notification: 'buildTarget/didChange' to support this case.
}
Expand All @@ -180,29 +188,28 @@ public String buildToolName() {
* add Java nature if necessary.
* @throws CoreException
*/
public void updateClasspath(IProject project, IProgressMonitor monitor) throws CoreException {
public void updateClasspath(BuildServerConnection connection, IProject project, IProgressMonitor monitor) throws CoreException {
IPath rootPath = ProjectUtils.findBelongedWorkspaceRoot(project.getLocation());
if (rootPath == null) {
JavaLanguageServerPlugin.logError("Cannot find workspace root for project: " + project.getName());
return;
}
BuildServerConnection buildServer = ImporterPlugin.getBuildServerConnection(rootPath);
// use map to dedupe the classpath entries having the same path field.
Map<IPath, IClasspathEntry> classpathMap = new LinkedHashMap<>();
List<BuildTarget> buildTargets = Utils.getBuildTargetsByProjectUri(buildServer, project.getLocationURI());
List<BuildTarget> buildTargets = Utils.getBuildTargetsByProjectUri(connection, project.getLocationURI());
// put test targets to the end of the list
moveTestTargetsToEnd(buildTargets);

for (BuildTarget buildTarget : buildTargets) {
boolean isTest = buildTarget.getTags().contains(BuildTargetTag.TEST);
OutputPathsResult outputResult = buildServer.buildTargetOutputPaths(
OutputPathsResult outputResult = connection.buildTargetOutputPaths(
new OutputPathsParams(Arrays.asList(buildTarget.getId()))).join();
String sourceOutputUri = getOutputUriByKind(outputResult.getItems(), OUTPUT_KIND_SOURCE);
IPath sourceOutputFullPath = getOutputFullPath(sourceOutputUri, project);
if (sourceOutputFullPath == null) {
JavaLanguageServerPlugin.logError("Cannot find source output path for build target: " + buildTarget.getId());
} else {
SourcesResult sourcesResult = buildServer.buildTargetSources(
SourcesResult sourcesResult = connection.buildTargetSources(
new SourcesParams(Arrays.asList(buildTarget.getId()))).join();
List<IClasspathEntry> sourceEntries = getSourceEntries(rootPath, project, sourcesResult, sourceOutputFullPath, isTest, monitor);
for (IClasspathEntry entry : sourceEntries) {
Expand All @@ -214,7 +221,7 @@ public void updateClasspath(IProject project, IProgressMonitor monitor) throws C
IPath resourceOutputFullPath = getOutputFullPath(resourceOutputUri, project);
// resource output is nullable according to Gradle API definition.
if (resourceOutputFullPath != null) {
ResourcesResult resourcesResult = buildServer.buildTargetResources(
ResourcesResult resourcesResult = connection.buildTargetResources(
new ResourcesParams(Arrays.asList(buildTarget.getId()))).join();
List<IClasspathEntry> resourceEntries = getResourceEntries(project, resourcesResult, resourceOutputFullPath, isTest);
for (IClasspathEntry entry : resourceEntries) {
Expand Down Expand Up @@ -246,7 +253,7 @@ public void updateClasspath(IProject project, IProgressMonitor monitor) throws C

for (BuildTarget buildTarget : buildTargets) {
boolean isTest = buildTarget.getTags().contains(BuildTargetTag.TEST);
DependencyModulesResult dependencyModuleResult = buildServer.buildTargetDependencyModules(
DependencyModulesResult dependencyModuleResult = connection.buildTargetDependencyModules(
new DependencyModulesParams(Arrays.asList(buildTarget.getId()))).join();
List<IClasspathEntry> dependencyEntries = getDependencyJars(dependencyModuleResult, isTest, isModular);
for (IClasspathEntry entry : dependencyEntries) {
Expand All @@ -257,7 +264,7 @@ public void updateClasspath(IProject project, IProgressMonitor monitor) throws C
javaProject.setRawClasspath(classpathMap.values().toArray(new IClasspathEntry[0]), monitor);

// process jpms arguments.
JavacOptionsResult javacOptions = buildServer.buildTargetJavacOptions(new JavacOptionsParams(
JavacOptionsResult javacOptions = connection.buildTargetJavacOptions(new JavacOptionsParams(
buildTargets.stream().map(BuildTarget::getId).collect(Collectors.toList()))).join();
List<String> compilerArgs = new LinkedList<>();
for (JavacOptionsItem item : javacOptions.getItems()) {
Expand All @@ -276,14 +283,13 @@ public void updateClasspath(IProject project, IProgressMonitor monitor) throws C
* Update the project dependencies of the project.
* @throws CoreException
*/
public void updateProjectDependencies(IProject project, IProgressMonitor monitor) throws CoreException {
public void updateProjectDependencies(BuildServerConnection connection, IProject project, IProgressMonitor monitor) throws CoreException {
IPath rootPath = ProjectUtils.findBelongedWorkspaceRoot(project.getLocation());
if (rootPath == null) {
JavaLanguageServerPlugin.logError("Cannot find workspace root for project: " + project.getName());
return;
}
BuildServerConnection buildServer = ImporterPlugin.getBuildServerConnection(rootPath);
List<BuildTarget> buildTargets = Utils.getBuildTargetsByProjectUri(buildServer, project.getLocationURI());
List<BuildTarget> buildTargets = Utils.getBuildTargetsByProjectUri(connection, project.getLocationURI());
Set<BuildTargetIdentifier> projectDependencies = new LinkedHashSet<>();
for (BuildTarget buildTarget : buildTargets) {
projectDependencies.addAll(buildTarget.getDependencies());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public boolean applies(Collection<IPath> projectConfigurations, IProgressMonitor
@Override
public void importToWorkspace(IProgressMonitor monitor) throws OperationCanceledException, CoreException {
IPath rootPath = ResourceUtils.filePathFromURI(rootFolder.toURI().toString());
BuildServerConnection buildServer = ImporterPlugin.getBuildServerConnection(rootPath);
BuildServerConnection buildServer = ImporterPlugin.getBuildServerConnection(rootPath, true);

// for all the path in this.directories, find the out most directory which belongs
// to rootFolder and use that directory as the root folder for the build server.
Expand Down Expand Up @@ -183,14 +183,14 @@ public void importToWorkspace(IProgressMonitor monitor) throws OperationCanceled

GradleBuildServerBuildSupport buildSupport = new GradleBuildServerBuildSupport();
for (IProject project : projects) {
buildSupport.updateClasspath(project, monitor);
buildSupport.updateClasspath(buildServer, project, monitor);
}

// We need to add the project dependencies after the Java nature is set to all
// the projects, which is done in 'updateClasspath(IProject, IProgressMonitor)',
// otherwise JDT will thrown exception when adding projects as dependencies.
for (IProject project : projects) {
buildSupport.updateProjectDependencies(project, monitor);
buildSupport.updateProjectDependencies(buildServer, project, monitor);
}

for (IProject project : projects) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,33 @@ public static DigestStore getDigestStore() {
return instance.digestStore;
}

/**
* Get the build server connection for the given root path. If the connection doesn't exist,
* returns <code>null</code>.
* @param rootPath
* @throws CoreException
*/
public static BuildServerConnection getBuildServerConnection(IPath rootPath) throws CoreException {
return getBuildServerConnection(rootPath, false);
}

/**
* Get the build server connection for the given root path.
* @param rootPath the root path of the workspace.
* @param createIfMissing whether to create a new build server connection if it doesn't exist.
* @return the build server connection.
* @throws CoreException
*/
public static BuildServerConnection getBuildServerConnection(IPath rootPath, boolean createIfMissing) throws CoreException {
Pair<BuildServerConnection, BuildClient> pair = instance.buildServers.get(rootPath);
if (pair != null) {
return pair.getLeft();
}

if (!createIfMissing) {
return null;
}

String javaExecutablePath = getJavaExecutablePath();
String[] classpaths = getBuildServerClasspath();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,20 @@ protected IProject[] build(int kind, Map<String, String> args, IProgressMonitor
return null;
}
BuildServerConnection buildServer = ImporterPlugin.getBuildServerConnection(rootPath);
if (buildServer != null) {
List<BuildTarget> targets = Utils.getBuildTargetsByProjectUri(buildServer, project.getLocationURI());
List<BuildTargetIdentifier> ids = targets.stream().map(BuildTarget::getId).collect(Collectors.toList());
if (ids != null) {
// TODO: support clean build
CompileResult result = buildServer.buildTargetCompile(new CompileParams(ids)).join();
if (Objects.equals(result.getStatusCode(), StatusCode.ERROR)) {
throw new CoreException(new Status(IStatus.ERROR, ImporterPlugin.PLUGIN_ID,
IResourceStatus.BUILD_FAILED, "Build Failed.", null));
}
this.refreshOutputs(monitor);
if (buildServer == null) {
JavaLanguageServerPlugin.logInfo("Skip building project: " + project.getName() + " because build server is not available.");
return null;
}
List<BuildTarget> targets = Utils.getBuildTargetsByProjectUri(buildServer, project.getLocationURI());
List<BuildTargetIdentifier> ids = targets.stream().map(BuildTarget::getId).collect(Collectors.toList());
if (ids != null) {
// TODO: support clean build
CompileResult result = buildServer.buildTargetCompile(new CompileParams(ids)).join();
if (Objects.equals(result.getStatusCode(), StatusCode.ERROR)) {
throw new CoreException(new Status(IStatus.ERROR, ImporterPlugin.PLUGIN_ID,
IResourceStatus.BUILD_FAILED, "Build Failed.", null));
}
this.refreshOutputs(monitor);
}
return null;
}
Expand Down

0 comments on commit 1185a1f

Please sign in to comment.