diff --git a/.gitignore b/.gitignore index bc05c9305..80489aaf3 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ hash2 .metals .vscode unformatted-*.backup.scala -.scala-build \ No newline at end of file +.scala-build +test/semanticdb/tempsrc \ No newline at end of file diff --git a/src/java/io/bazel/rulesscala/scalac/ScalacWorker.java b/src/java/io/bazel/rulesscala/scalac/ScalacWorker.java index 57bc9fb55..6cca6e6b3 100644 --- a/src/java/io/bazel/rulesscala/scalac/ScalacWorker.java +++ b/src/java/io/bazel/rulesscala/scalac/ScalacWorker.java @@ -37,10 +37,14 @@ public static void main(String[] args) throws Exception { public void work(String[] args) throws Exception { CompileOptions ops = new CompileOptions(args); - Path outputJar = Paths.get(ops.outputName); - Path workdir = ensureEmptyWorkDirectory(outputJar, ops.currentTarget); - Path classes = Files.createDirectories(workdir.resolve("classes")); - Path sources = Files.createDirectories(workdir.resolve("sources")); + Path outputJarPath = Paths.get(ops.outputName); + + Path scalacOutPath = clearWorkDirectory(outputJarPath, ops.currentTarget, "_scalac"); + clearWorkDirectory(outputJarPath, ops.currentTarget, "_semanticdb"); + + Files.createDirectories(scalacOutPath); + Path classes = Files.createDirectories(scalacOutPath.resolve("classes")); + Path sources = Files.createDirectories(scalacOutPath.resolve("sources")); List jarFiles = extractSourceJars(ops, sources); List scalaJarFiles = filterFilesByExtension(jarFiles, ".scala"); @@ -80,20 +84,20 @@ public void work(String[] args) throws Exception { /** Now build the output jar */ String[] jarCreatorArgs = { - "-m", ops.manifestPath, "-t", ops.stampLabel, outputJar.toString(), classes.toString() + "-m", ops.manifestPath, "-t", ops.stampLabel, outputJarPath.toString(), classes.toString() }; JarCreator.main(jarCreatorArgs); } - private static Path ensureEmptyWorkDirectory(Path output, String label) throws IOException { + private static Path clearWorkDirectory(Path output, String label, String folderName) throws IOException { String base = label.substring(label.lastIndexOf(':') + 1); - Path dir = output.resolveSibling("_scalac").resolve(base); + Path dir = output.resolveSibling(folderName).resolve(base); if (Files.exists(dir)) { deleteRecursively(dir); } - return Files.createDirectories(dir); + return dir; } private static String[] collectSrcJarSources( diff --git a/test/semanticdb/BUILD b/test/semanticdb/BUILD index 5cec87149..b8db06e57 100644 --- a/test/semanticdb/BUILD +++ b/test/semanticdb/BUILD @@ -32,7 +32,7 @@ toolchain( scala_library( name = "all_lib", - srcs = glob(["**/*.scala"]), + srcs = glob(["*.scala"]), ) semanticdb_vars_script( @@ -49,3 +49,13 @@ semanticdb_vars_script( name = "semantic_provider_vars_empty", dep = "empty_lib", ) + +scala_library( + name = "lib_with_tempsrc", + srcs = glob( + [ + "*.scala", + "tempsrc/*.scala", #Include src files that are dynamically generated by the test_semanticdb.sh (tmpsrc should be in .gitignore so its contents don't get checked in) + ], + ), +) diff --git a/test/shell/test_semanticdb.sh b/test/shell/test_semanticdb.sh index 359c825a8..351162f09 100755 --- a/test/shell/test_semanticdb.sh +++ b/test/shell/test_semanticdb.sh @@ -134,10 +134,51 @@ test_no_semanticdb() { fi } + +test_semanticdb_handles_removed_sourcefiles() { + #Ensure absense of bug where bazel failed with 'access denied' on Windows when a source file was removed. + + #First add the new scala file, build it, then remove the new scala file, and ensure it builds. + set -e + + local toolchainArg=--extra_toolchains=//test/semanticdb:semanticdb_nobundle_toolchain + local newfile_dir=test/semanticdb/tempsrc + local newfilename=D.scala + local newfilepath=$newfile_dir/$newfilename + local rule_label=//test/semanticdb:lib_with_tempsrc + + #add new source file and build it + mkdir -p $newfile_dir && echo "class D{ val a = 1; }" > $newfilepath + + + #make sure D.scala was added to the target (sanity check) + local query_result1=$(bazel query "labels(srcs, $rule_label)") + if [[ $query_result1 != *"$newfilename"* ]] ; then + echo "$newfilename was not properly added as src for target $rule_label" + exit 1 + fi + + bazel build $rule_label $toolchainArg + + #remove the new source file and build it + rm $newfilepath + + #make sure D.scala was properly removed from the target(sanity check) + local query_result2=$(bazel query "labels(srcs, $rule_label)") + if [[ $query_result2 == *"$newfilename"* ]] ; then + echo "$newfilename was not properly removed as src for target $rule_label" + exit 1 + fi + + bazel build $rule_label $toolchainArg + + +} + run_semanticdb_tests() { local bundle=1; local nobundle=0 local scala3=3; local scala2=2 - + $runner test_produces_semanticdb $scala2 $bundle $runner test_produces_semanticdb $scala2 $nobundle @@ -147,7 +188,7 @@ run_semanticdb_tests() { $runner test_produces_semanticdb $scala3 $nobundle $runner test_no_semanticdb - + $runner test_semanticdb_handles_removed_sourcefiles } run_semanticdb_tests