Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Semanticdb clear dir #1580

Merged
merged 3 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ hash2
.metals
.vscode
unformatted-*.backup.scala
.scala-build
.scala-build
test/semanticdb/tempsrc
20 changes: 12 additions & 8 deletions src/java/io/bazel/rulesscala/scalac/ScalacWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<File> jarFiles = extractSourceJars(ops, sources);
List<File> scalaJarFiles = filterFilesByExtension(jarFiles, ".scala");
Expand Down Expand Up @@ -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(
Expand Down
12 changes: 11 additions & 1 deletion test/semanticdb/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ toolchain(

scala_library(
name = "all_lib",
srcs = glob(["**/*.scala"]),
srcs = glob(["*.scala"]),
)

semanticdb_vars_script(
Expand All @@ -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)
],
),
)
45 changes: 43 additions & 2 deletions test/shell/test_semanticdb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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