Skip to content

Commit 1185a1f

Browse files
authored
Only send request after initialization (#1503)
1 parent 2905593 commit 1185a1f

File tree

4 files changed

+56
-27
lines changed

4 files changed

+56
-27
lines changed

extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerBuildSupport.java

+19-13
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ public void onWillUpdate(Collection<IProject> projects, IProgressMonitor monitor
138138
}
139139
for (IPath rootPath : roots) {
140140
BuildServerConnection connection = ImporterPlugin.getBuildServerConnection(rootPath);
141+
if (connection == null) {
142+
JavaLanguageServerPlugin.logInfo("Skip reloading " + rootPath + " because the connection is not available.");
143+
continue;
144+
}
141145
connection.workspaceReload().join();
142146
}
143147
}
@@ -156,14 +160,18 @@ public void update(IProject project, boolean force, IProgressMonitor monitor) th
156160
return;
157161
}
158162
BuildServerConnection connection = ImporterPlugin.getBuildServerConnection(rootPath);
163+
if (connection == null) {
164+
JavaLanguageServerPlugin.logError("Cannot find build server connection for root: " + rootPath);
165+
return;
166+
}
159167
Map<URI, List<BuildTarget>> buildTargetMap = Utils.getBuildTargetsMappedByProjectPath(connection);
160168
for (URI uri : buildTargetMap.keySet()) {
161169
IProject projectFromUri = ProjectUtils.getProjectFromUri(uri.toString());
162170
if (projectFromUri == null || !Utils.isGradleBuildServerProject(projectFromUri)) {
163171
continue;
164172
}
165-
updateClasspath(projectFromUri, monitor);
166-
updateProjectDependencies(projectFromUri, monitor);
173+
updateClasspath(connection, projectFromUri, monitor);
174+
updateProjectDependencies(connection, projectFromUri, monitor);
167175
// TODO: in case that the projects/build targets are created or removed,
168176
// we can use the server->client notification: 'buildTarget/didChange' to support this case.
169177
}
@@ -180,29 +188,28 @@ public String buildToolName() {
180188
* add Java nature if necessary.
181189
* @throws CoreException
182190
*/
183-
public void updateClasspath(IProject project, IProgressMonitor monitor) throws CoreException {
191+
public void updateClasspath(BuildServerConnection connection, IProject project, IProgressMonitor monitor) throws CoreException {
184192
IPath rootPath = ProjectUtils.findBelongedWorkspaceRoot(project.getLocation());
185193
if (rootPath == null) {
186194
JavaLanguageServerPlugin.logError("Cannot find workspace root for project: " + project.getName());
187195
return;
188196
}
189-
BuildServerConnection buildServer = ImporterPlugin.getBuildServerConnection(rootPath);
190197
// use map to dedupe the classpath entries having the same path field.
191198
Map<IPath, IClasspathEntry> classpathMap = new LinkedHashMap<>();
192-
List<BuildTarget> buildTargets = Utils.getBuildTargetsByProjectUri(buildServer, project.getLocationURI());
199+
List<BuildTarget> buildTargets = Utils.getBuildTargetsByProjectUri(connection, project.getLocationURI());
193200
// put test targets to the end of the list
194201
moveTestTargetsToEnd(buildTargets);
195202

196203
for (BuildTarget buildTarget : buildTargets) {
197204
boolean isTest = buildTarget.getTags().contains(BuildTargetTag.TEST);
198-
OutputPathsResult outputResult = buildServer.buildTargetOutputPaths(
205+
OutputPathsResult outputResult = connection.buildTargetOutputPaths(
199206
new OutputPathsParams(Arrays.asList(buildTarget.getId()))).join();
200207
String sourceOutputUri = getOutputUriByKind(outputResult.getItems(), OUTPUT_KIND_SOURCE);
201208
IPath sourceOutputFullPath = getOutputFullPath(sourceOutputUri, project);
202209
if (sourceOutputFullPath == null) {
203210
JavaLanguageServerPlugin.logError("Cannot find source output path for build target: " + buildTarget.getId());
204211
} else {
205-
SourcesResult sourcesResult = buildServer.buildTargetSources(
212+
SourcesResult sourcesResult = connection.buildTargetSources(
206213
new SourcesParams(Arrays.asList(buildTarget.getId()))).join();
207214
List<IClasspathEntry> sourceEntries = getSourceEntries(rootPath, project, sourcesResult, sourceOutputFullPath, isTest, monitor);
208215
for (IClasspathEntry entry : sourceEntries) {
@@ -214,7 +221,7 @@ public void updateClasspath(IProject project, IProgressMonitor monitor) throws C
214221
IPath resourceOutputFullPath = getOutputFullPath(resourceOutputUri, project);
215222
// resource output is nullable according to Gradle API definition.
216223
if (resourceOutputFullPath != null) {
217-
ResourcesResult resourcesResult = buildServer.buildTargetResources(
224+
ResourcesResult resourcesResult = connection.buildTargetResources(
218225
new ResourcesParams(Arrays.asList(buildTarget.getId()))).join();
219226
List<IClasspathEntry> resourceEntries = getResourceEntries(project, resourcesResult, resourceOutputFullPath, isTest);
220227
for (IClasspathEntry entry : resourceEntries) {
@@ -246,7 +253,7 @@ public void updateClasspath(IProject project, IProgressMonitor monitor) throws C
246253

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

259266
// process jpms arguments.
260-
JavacOptionsResult javacOptions = buildServer.buildTargetJavacOptions(new JavacOptionsParams(
267+
JavacOptionsResult javacOptions = connection.buildTargetJavacOptions(new JavacOptionsParams(
261268
buildTargets.stream().map(BuildTarget::getId).collect(Collectors.toList()))).join();
262269
List<String> compilerArgs = new LinkedList<>();
263270
for (JavacOptionsItem item : javacOptions.getItems()) {
@@ -276,14 +283,13 @@ public void updateClasspath(IProject project, IProgressMonitor monitor) throws C
276283
* Update the project dependencies of the project.
277284
* @throws CoreException
278285
*/
279-
public void updateProjectDependencies(IProject project, IProgressMonitor monitor) throws CoreException {
286+
public void updateProjectDependencies(BuildServerConnection connection, IProject project, IProgressMonitor monitor) throws CoreException {
280287
IPath rootPath = ProjectUtils.findBelongedWorkspaceRoot(project.getLocation());
281288
if (rootPath == null) {
282289
JavaLanguageServerPlugin.logError("Cannot find workspace root for project: " + project.getName());
283290
return;
284291
}
285-
BuildServerConnection buildServer = ImporterPlugin.getBuildServerConnection(rootPath);
286-
List<BuildTarget> buildTargets = Utils.getBuildTargetsByProjectUri(buildServer, project.getLocationURI());
292+
List<BuildTarget> buildTargets = Utils.getBuildTargetsByProjectUri(connection, project.getLocationURI());
287293
Set<BuildTargetIdentifier> projectDependencies = new LinkedHashSet<>();
288294
for (BuildTarget buildTarget : buildTargets) {
289295
projectDependencies.addAll(buildTarget.getDependencies());

extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerProjectImporter.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public boolean applies(Collection<IPath> projectConfigurations, IProgressMonitor
143143
@Override
144144
public void importToWorkspace(IProgressMonitor monitor) throws OperationCanceledException, CoreException {
145145
IPath rootPath = ResourceUtils.filePathFromURI(rootFolder.toURI().toString());
146-
BuildServerConnection buildServer = ImporterPlugin.getBuildServerConnection(rootPath);
146+
BuildServerConnection buildServer = ImporterPlugin.getBuildServerConnection(rootPath, true);
147147

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

184184
GradleBuildServerBuildSupport buildSupport = new GradleBuildServerBuildSupport();
185185
for (IProject project : projects) {
186-
buildSupport.updateClasspath(project, monitor);
186+
buildSupport.updateClasspath(buildServer, project, monitor);
187187
}
188188

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

196196
for (IProject project : projects) {

extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/ImporterPlugin.java

+21
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,33 @@ public static DigestStore getDigestStore() {
7575
return instance.digestStore;
7676
}
7777

78+
/**
79+
* Get the build server connection for the given root path. If the connection doesn't exist,
80+
* returns <code>null</code>.
81+
* @param rootPath
82+
* @throws CoreException
83+
*/
7884
public static BuildServerConnection getBuildServerConnection(IPath rootPath) throws CoreException {
85+
return getBuildServerConnection(rootPath, false);
86+
}
87+
88+
/**
89+
* Get the build server connection for the given root path.
90+
* @param rootPath the root path of the workspace.
91+
* @param createIfMissing whether to create a new build server connection if it doesn't exist.
92+
* @return the build server connection.
93+
* @throws CoreException
94+
*/
95+
public static BuildServerConnection getBuildServerConnection(IPath rootPath, boolean createIfMissing) throws CoreException {
7996
Pair<BuildServerConnection, BuildClient> pair = instance.buildServers.get(rootPath);
8097
if (pair != null) {
8198
return pair.getLeft();
8299
}
83100

101+
if (!createIfMissing) {
102+
return null;
103+
}
104+
84105
String javaExecutablePath = getJavaExecutablePath();
85106
String[] classpaths = getBuildServerClasspath();
86107

extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/builder/BuildServerBuilder.java

+13-11
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,20 @@ protected IProject[] build(int kind, Map<String, String> args, IProgressMonitor
4848
return null;
4949
}
5050
BuildServerConnection buildServer = ImporterPlugin.getBuildServerConnection(rootPath);
51-
if (buildServer != null) {
52-
List<BuildTarget> targets = Utils.getBuildTargetsByProjectUri(buildServer, project.getLocationURI());
53-
List<BuildTargetIdentifier> ids = targets.stream().map(BuildTarget::getId).collect(Collectors.toList());
54-
if (ids != null) {
55-
// TODO: support clean build
56-
CompileResult result = buildServer.buildTargetCompile(new CompileParams(ids)).join();
57-
if (Objects.equals(result.getStatusCode(), StatusCode.ERROR)) {
58-
throw new CoreException(new Status(IStatus.ERROR, ImporterPlugin.PLUGIN_ID,
59-
IResourceStatus.BUILD_FAILED, "Build Failed.", null));
60-
}
61-
this.refreshOutputs(monitor);
51+
if (buildServer == null) {
52+
JavaLanguageServerPlugin.logInfo("Skip building project: " + project.getName() + " because build server is not available.");
53+
return null;
54+
}
55+
List<BuildTarget> targets = Utils.getBuildTargetsByProjectUri(buildServer, project.getLocationURI());
56+
List<BuildTargetIdentifier> ids = targets.stream().map(BuildTarget::getId).collect(Collectors.toList());
57+
if (ids != null) {
58+
// TODO: support clean build
59+
CompileResult result = buildServer.buildTargetCompile(new CompileParams(ids)).join();
60+
if (Objects.equals(result.getStatusCode(), StatusCode.ERROR)) {
61+
throw new CoreException(new Status(IStatus.ERROR, ImporterPlugin.PLUGIN_ID,
62+
IResourceStatus.BUILD_FAILED, "Build Failed.", null));
6263
}
64+
this.refreshOutputs(monitor);
6365
}
6466
return null;
6567
}

0 commit comments

Comments
 (0)