-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathInferConfig.java
More file actions
505 lines (459 loc) · 18.8 KB
/
InferConfig.java
File metadata and controls
505 lines (459 loc) · 18.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
package org.javacs;
import com.google.devtools.build.lib.analysis.AnalysisProtos;
import com.google.devtools.build.lib.analysis.AnalysisProtosV2;
import com.google.devtools.build.lib.analysis.AnalysisProtosV2.PathFragment;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import java.util.stream.Stream;
class InferConfig {
private static final Logger LOG = Logger.getLogger("main");
/** Root of the workspace that is currently open in VSCode */
private final Path workspaceRoot;
/** External dependencies specified manually by the user */
private final Collection<String> externalDependencies;
/** Location of the maven repository, usually ~/.m2 */
private final Path mavenHome;
/** Location of the gradle cache, usually ~/.gradle */
private final Path gradleHome;
InferConfig(Path workspaceRoot, Collection<String> externalDependencies, Path mavenHome, Path gradleHome) {
this.workspaceRoot = workspaceRoot;
this.externalDependencies = externalDependencies;
this.mavenHome = mavenHome;
this.gradleHome = gradleHome;
}
InferConfig(Path workspaceRoot, Collection<String> externalDependencies) {
this(workspaceRoot, externalDependencies, defaultMavenHome(), defaultGradleHome());
}
InferConfig(Path workspaceRoot) {
this(workspaceRoot, Collections.emptySet(), defaultMavenHome(), defaultGradleHome());
}
private static Path defaultMavenHome() {
return Paths.get(System.getProperty("user.home")).resolve(".m2");
}
private static Path defaultGradleHome() {
return Paths.get(System.getProperty("user.home")).resolve(".gradle");
}
/** Find .jar files for external dependencies, for examples maven dependencies in ~/.m2 or jars in bazel-genfiles */
Set<Path> classPath() {
// externalDependencies
if (!externalDependencies.isEmpty()) {
var result = new HashSet<Path>();
for (var id : externalDependencies) {
var a = Artifact.parse(id);
var found = findAnyJar(a, false);
if (found == NOT_FOUND) {
LOG.warning(String.format("Couldn't find jar for %s in %s or %s", a, mavenHome, gradleHome));
continue;
}
result.add(found);
}
return result;
}
// Maven
var pomXml = workspaceRoot.resolve("pom.xml");
if (Files.exists(pomXml)) {
return mvnDependencies(pomXml, "dependency:list");
}
// Bazel
var bazelWorkspaceRoot = bazelWorkspaceRoot();
if (Files.exists(bazelWorkspaceRoot.resolve("WORKSPACE"))) {
return bazelClasspath(bazelWorkspaceRoot);
}
return Collections.emptySet();
}
private Path bazelWorkspaceRoot() {
for (var current = workspaceRoot; current != null; current = current.getParent()) {
if (Files.exists(current.resolve("WORKSPACE"))) {
return current;
}
}
return workspaceRoot;
}
/** Find source .jar files in local maven repository. */
Set<Path> buildDocPath() {
// externalDependencies
if (!externalDependencies.isEmpty()) {
var result = new HashSet<Path>();
for (var id : externalDependencies) {
var a = Artifact.parse(id);
var found = findAnyJar(a, true);
if (found == NOT_FOUND) {
LOG.warning(String.format("Couldn't find doc jar for %s in %s or %s", a, mavenHome, gradleHome));
continue;
}
result.add(found);
}
return result;
}
// Maven
var pomXml = workspaceRoot.resolve("pom.xml");
if (Files.exists(pomXml)) {
return mvnDependencies(pomXml, "dependency:sources");
}
// Bazel
var bazelWorkspaceRoot = bazelWorkspaceRoot();
if (Files.exists(bazelWorkspaceRoot.resolve("WORKSPACE"))) {
return bazelSourcepath(bazelWorkspaceRoot);
}
return Collections.emptySet();
}
private Path findAnyJar(Artifact artifact, boolean source) {
Path maven = findMavenJar(artifact, source);
if (maven != NOT_FOUND) {
return maven;
} else return findGradleJar(artifact, source);
}
Path findMavenJar(Artifact artifact, boolean source) {
var jar =
mavenHome
.resolve("repository")
.resolve(artifact.groupId.replace('.', File.separatorChar))
.resolve(artifact.artifactId)
.resolve(artifact.version)
.resolve(fileName(artifact, source));
if (!Files.exists(jar)) {
LOG.warning(jar + " does not exist");
return NOT_FOUND;
}
return jar;
}
private Path findGradleJar(Artifact artifact, boolean source) {
// Search for caches/modules-*/files-*/groupId/artifactId/version/*/artifactId-version[-sources].jar
var base = gradleHome.resolve("caches");
var pattern =
"glob:"
+ String.join(
File.separator,
base.toString(),
"modules-*",
"files-*",
artifact.groupId,
artifact.artifactId,
artifact.version,
"*",
fileName(artifact, source));
var match = FileSystems.getDefault().getPathMatcher(pattern);
try {
return Files.walk(base, 7).filter(match::matches).findFirst().orElse(NOT_FOUND);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private String fileName(Artifact artifact, boolean source) {
return artifact.artifactId + '-' + artifact.version + (source ? "-sources" : "") + ".jar";
}
static Set<Path> mvnDependencies(Path pomXml, String goal) {
Objects.requireNonNull(pomXml, "pom.xml path is null");
try {
// TODO consider using mvn valide dependency:copy-dependencies -DoutputDirectory=??? instead
// Run maven as a subprocess
String[] command = {
getMvnCommand(),
"--batch-mode", // Turns off ANSI control sequences
"validate",
goal,
"-DincludeScope=test",
"-DoutputAbsoluteArtifactFilename=true",
};
var output = Files.createTempFile("java-language-server-maven-output", ".txt");
LOG.info("Running " + String.join(" ", command) + " ...");
var workingDirectory = pomXml.toAbsolutePath().getParent().toFile();
var process =
new ProcessBuilder()
.command(command)
.directory(workingDirectory)
.redirectError(ProcessBuilder.Redirect.INHERIT)
.redirectOutput(output.toFile())
.start();
// Wait for process to exit
var result = process.waitFor();
if (result != 0) {
LOG.severe("`" + String.join(" ", command) + "` returned " + result);
return Set.of();
}
// Read output
var dependencies = new HashSet<Path>();
for (var line : Files.readAllLines(output)) {
var jar = readDependency(line);
if (jar != NOT_FOUND) {
dependencies.add(jar);
}
}
return dependencies;
} catch (InterruptedException | IOException e) {
throw new RuntimeException(e);
}
}
private static final Pattern DEPENDENCY =
Pattern.compile("^\\[INFO\\]\\s+(.*:.*:.*:.*:.*):(/.*?)( -- module .*)?$");
static Path readDependency(String line) {
var match = DEPENDENCY.matcher(line);
if (!match.matches()) {
return NOT_FOUND;
}
var artifact = match.group(1);
var path = match.group(2);
LOG.info(String.format("...%s => %s", artifact, path));
return Paths.get(path);
}
static String getMvnCommand() {
var mvnCommand = "mvn";
if (File.separatorChar == '\\') {
mvnCommand = findExecutableOnPath("mvn.cmd");
if (mvnCommand == null) {
mvnCommand = findExecutableOnPath("mvn.bat");
}
}
return mvnCommand;
}
private static String findExecutableOnPath(String name) {
for (var dirname : System.getenv("PATH").split(File.pathSeparator)) {
var file = new File(dirname, name);
if (file.isFile() && file.canExecute()) {
return file.getAbsolutePath();
}
}
return null;
}
private boolean buildProtos(Path bazelWorkspaceRoot) {
var targets = bazelQuery(bazelWorkspaceRoot, "java_proto_library");
if (targets.size() == 0) {
return false;
}
bazelDryRunBuild(bazelWorkspaceRoot, targets);
return true;
}
private Set<Path> bazelClasspath(Path bazelWorkspaceRoot) {
var absolute = new HashSet<Path>();
// Add protos
if (buildProtos(bazelWorkspaceRoot)) {
for (var relative : bazelAQuery(bazelWorkspaceRoot, "Javac", "--output", "proto_library")) {
absolute.add(bazelWorkspaceRoot.resolve(relative));
}
}
// Add rest of classpath
for (var relative :
bazelAQuery(bazelWorkspaceRoot, "Javac", "--classpath", "java_library", "java_test", "java_binary")) {
absolute.add(bazelWorkspaceRoot.resolve(relative));
}
return absolute;
}
private Set<Path> bazelSourcepath(Path bazelWorkspaceRoot) {
var absolute = new HashSet<Path>();
var outputBase = bazelOutputBase(bazelWorkspaceRoot);
for (var relative :
bazelAQuery(
bazelWorkspaceRoot, "JavaSourceJar", "--sources", "java_library", "java_test", "java_binary")) {
absolute.add(outputBase.resolve(relative));
}
// Add proto source files
if (buildProtos(bazelWorkspaceRoot)) {
for (var relative : bazelAQuery(bazelWorkspaceRoot, "Javac", "--source_jars", "proto_library")) {
absolute.add(bazelWorkspaceRoot.resolve(relative));
}
}
return absolute;
}
private Path bazelOutputBase(Path bazelWorkspaceRoot) {
// Run bazel as a subprocess
String[] command = {
"bazel", "info", "output_base",
};
var output = fork(bazelWorkspaceRoot, command, false);
if (output == NOT_FOUND) {
return NOT_FOUND;
}
// Read output
try {
var out = Files.readString(output).trim();
return Paths.get(out);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void bazelDryRunBuild(Path bazelWorkspaceRoot, Set<String> targets) {
var command = new ArrayList<String>();
command.add("bazel");
command.add("build");
command.add("--keep_going");
command.add("--nobuild");
command.addAll(targets);
String[] c = new String[command.size()];
c = command.toArray(c);
var output = fork(bazelWorkspaceRoot, c, true);
if (output == NOT_FOUND) {
return;
}
return;
}
private Set<String> bazelQuery(Path bazelWorkspaceRoot, String filterKind) {
String[] command = {"bazel", "query", "--keep_going", "kind(" + filterKind + ",//...)"};
var output = fork(bazelWorkspaceRoot, command, true);
if (output == NOT_FOUND) {
return Set.of();
}
return readQueryResult(output);
}
private Set<String> readQueryResult(Path output) {
try {
Stream<String> stream = Files.lines(output);
var targets = new HashSet<String>();
var i = stream.iterator();
while (i.hasNext()) {
var t = i.next();
targets.add(t);
}
return targets;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private Set<String> bazelAQuery(
Path bazelWorkspaceRoot, String filterMnemonic, String filterArgument, String... kinds) {
String kindUnion = "";
for (var kind : kinds) {
if (kindUnion.length() > 0) {
kindUnion += " union ";
}
kindUnion += "kind(" + kind + ", ...)";
}
String[] command = {
"bazel",
"aquery",
"--keep_going",
"--output=proto",
"--include_aspects", // required for java_proto_library, see
// https://stackoverflow.com/questions/63430530/bazel-aquery-returns-no-action-information-for-java-proto-library
"--allow_analysis_failures",
"mnemonic(" + filterMnemonic + ", " + kindUnion + ")"
};
var output = fork(bazelWorkspaceRoot, command, true);
if (output == NOT_FOUND) {
return Set.of();
}
return readActionGraph(output, filterArgument);
}
private Set<String> readActionGraph(Path output, String filterArgument) {
try {
var containerV2 = AnalysisProtosV2.ActionGraphContainer.parseFrom(Files.newInputStream(output));
if (containerV2.getArtifactsCount() != 0 && containerV2.getArtifactsList().get(0).getId() != 0) {
return readActionGraphFromV2(containerV2, filterArgument);
}
var containerV1 = AnalysisProtos.ActionGraphContainer.parseFrom(Files.newInputStream(output));
return readActionGraphFromV1(containerV1, filterArgument);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private Set<String> readActionGraphFromV1(AnalysisProtos.ActionGraphContainer container, String filterArgument) {
var argumentPaths = new HashSet<String>();
var outputIds = new HashSet<String>();
for (var action : container.getActionsList()) {
var isFilterArgument = false;
for (var argument : action.getArgumentsList()) {
if (isFilterArgument && argument.startsWith("-")) {
isFilterArgument = false;
continue;
}
if (!isFilterArgument) {
isFilterArgument = argument.equals(filterArgument);
continue;
}
argumentPaths.add(argument);
}
outputIds.addAll(action.getOutputIdsList());
}
var artifactPaths = new HashSet<String>();
for (var artifact : container.getArtifactsList()) {
if (!argumentPaths.contains(artifact.getExecPath())) {
// artifact was not specified by --filterArgument
continue;
}
if (outputIds.contains(artifact.getId()) && !filterArgument.equals("--output")) {
// artifact is the output of another java action
continue;
}
var relative = artifact.getExecPath();
LOG.info("...found bazel dependency " + relative);
artifactPaths.add(relative);
}
return artifactPaths;
}
private Set<String> readActionGraphFromV2(AnalysisProtosV2.ActionGraphContainer container, String filterArgument) {
var argumentPaths = new HashSet<String>();
var outputIds = new HashSet<Integer>();
for (var action : container.getActionsList()) {
var isFilterArgument = false;
for (var argument : action.getArgumentsList()) {
if (isFilterArgument && argument.startsWith("-")) {
isFilterArgument = false;
continue;
}
if (!isFilterArgument) {
isFilterArgument = argument.equals(filterArgument);
continue;
}
argumentPaths.add(argument);
}
outputIds.addAll(action.getOutputIdsList());
}
var artifactPaths = new HashSet<String>();
for (var artifact : container.getArtifactsList()) {
if (outputIds.contains(artifact.getId()) && !filterArgument.equals("--output")) {
// artifact is the output of another java action
continue;
}
var relative = buildPath(container.getPathFragmentsList(), artifact.getPathFragmentId());
if (!argumentPaths.contains(relative)) {
// artifact was not specified by --filterArgument
continue;
}
LOG.info("...found bazel dependency " + relative);
artifactPaths.add(relative);
}
return artifactPaths;
}
private static String buildPath(List<PathFragment> fragments, int id) {
for (PathFragment fragment : fragments) {
if (fragment.getId() == id) {
if (fragment.getParentId() != 0) {
return buildPath(fragments, fragment.getParentId()) + "/" + fragment.getLabel();
}
return fragment.getLabel();
}
}
throw new RuntimeException();
}
private static Path fork(Path workspaceRoot, String[] command, boolean allowNonZeroExit) {
try {
LOG.info("Running " + String.join(" ", command) + " ...");
var output = Files.createTempFile("java-language-server-bazel-output", ".proto");
var process =
new ProcessBuilder()
.command(command)
.directory(workspaceRoot.toFile())
.redirectError(ProcessBuilder.Redirect.INHERIT)
.redirectOutput(output.toFile())
.start();
// Wait for process to exit
var result = process.waitFor();
if (result != 0) {
LOG.severe("`" + String.join(" ", command) + "` returned " + result);
if (!allowNonZeroExit) {
return NOT_FOUND;
}
}
return output;
} catch (InterruptedException | IOException e) {
throw new RuntimeException(e);
}
}
private static final Path NOT_FOUND = Paths.get("");
}