Skip to content

Commit d953ca8

Browse files
cushondamienmg
authored andcommitted
Clean VanillaJavaBuilder output directories
to ensure outputs from any previous local builds are discarded. To cherry-pick for bazelbuild#2692. Fixes bazelbuild#2941 PiperOrigin-RevId: 155089391
1 parent 3ad44e3 commit d953ca8

2 files changed

Lines changed: 81 additions & 2 deletions

File tree

src/java_tools/buildjar/java/com/google/devtools/build/buildjar/VanillaJavaBuilder.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,12 @@ private static void setLocations(OptionsParser optionsParser, StandardJavaFileMa
249249
StandardLocation.ANNOTATION_PROCESSOR_PATH, toFiles(optionsParser.getProcessorPath()));
250250
if (optionsParser.getSourceGenDir() != null) {
251251
Path sourceGenDir = Paths.get(optionsParser.getSourceGenDir());
252-
Files.createDirectories(sourceGenDir);
252+
createOutputDirectory(sourceGenDir);
253253
fileManager.setLocation(
254254
StandardLocation.SOURCE_OUTPUT, ImmutableList.of(sourceGenDir.toFile()));
255255
}
256256
Path classDir = Paths.get(optionsParser.getClassDir());
257-
Files.createDirectories(classDir);
257+
createOutputDirectory(classDir);
258258
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, ImmutableList.of(classDir.toFile()));
259259
}
260260

@@ -342,4 +342,32 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
342342
return new String(Files.readAllBytes(path), UTF_8);
343343
}
344344
}
345+
346+
private static void createOutputDirectory(Path dir) throws IOException {
347+
if (Files.exists(dir)) {
348+
try {
349+
// TODO(b/27069912): handle symlinks
350+
Files.walkFileTree(
351+
dir,
352+
new SimpleFileVisitor<Path>() {
353+
@Override
354+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
355+
throws IOException {
356+
Files.delete(file);
357+
return FileVisitResult.CONTINUE;
358+
}
359+
360+
@Override
361+
public FileVisitResult postVisitDirectory(Path dir, IOException exc)
362+
throws IOException {
363+
Files.delete(dir);
364+
return FileVisitResult.CONTINUE;
365+
}
366+
});
367+
} catch (IOException e) {
368+
throw new IOException("Cannot clean output directory '" + dir + "'", e);
369+
}
370+
}
371+
Files.createDirectories(dir);
372+
}
345373
}

src/java_tools/buildjar/javatests/com/google/devtools/build/buildjar/VanillaJavaBuilderTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,55 @@ public void diagnosticWithoutSource() throws Exception {
183183
assertThat(result.ok()).isFalse();
184184
assertThat(Files.exists(output)).isFalse();
185185
}
186+
187+
@Test
188+
public void cleanOutputDirectories() throws Exception {
189+
Path source = temporaryFolder.newFile("Test.java").toPath();
190+
Path output = temporaryFolder.newFile("out.jar").toPath();
191+
Files.write(
192+
source,
193+
ImmutableList.of(
194+
"class A {", //
195+
"}"),
196+
UTF_8);
197+
Path sourceJar = temporaryFolder.newFile("src.srcjar").toPath();
198+
try (OutputStream os = Files.newOutputStream(sourceJar);
199+
JarOutputStream jos = new JarOutputStream(os)) {
200+
jos.putNextEntry(new JarEntry("B.java"));
201+
jos.write("class B {}".getBytes(UTF_8));
202+
}
203+
Path resource = temporaryFolder.newFile("resource.properties").toPath();
204+
Files.write(resource, "hello".getBytes(UTF_8));
205+
206+
Path classDir = temporaryFolder.newFolder().toPath();
207+
Files.write(
208+
classDir.resolve("extra.class"),
209+
new byte[] {(byte) 0xca, (byte) 0xfe, (byte) 0xba, (byte) 0xbe});
210+
211+
VanillaJavaBuilderResult result =
212+
run(
213+
ImmutableList.of(
214+
"--javacopts",
215+
"-Xep:FallThrough:ERROR",
216+
"--sources",
217+
source.toString(),
218+
"--source_jars",
219+
sourceJar.toString(),
220+
"--output",
221+
output.toString(),
222+
"--classpath_resources",
223+
resource.toString(),
224+
"--bootclasspath",
225+
Paths.get(System.getProperty("java.home")).resolve("lib/rt.jar").toString(),
226+
"--classdir",
227+
classDir.toString()));
228+
229+
assertThat(result.output()).isEmpty();
230+
assertThat(result.ok()).isTrue();
231+
232+
ImmutableMap<String, byte[]> outputEntries = readJar(output.toFile());
233+
assertThat(outputEntries.keySet())
234+
.containsExactly(
235+
"META-INF/", "META-INF/MANIFEST.MF", "A.class", "B.class", "resource.properties");
236+
}
186237
}

0 commit comments

Comments
 (0)