Skip to content

Semanticdb clear dir #1580

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

Merged
merged 3 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 12 additions & 0 deletions src/java/io/bazel/rulesscala/scalac/ScalacWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ public void work(String[] args) throws Exception {
CompileOptions ops = new CompileOptions(args);

Path outputJar = Paths.get(ops.outputName);

Path workdir = ensureEmptyWorkDirectory(outputJar, ops.currentTarget);
ensureEmptySemanticDBDirectory(outputJar, ops.currentTarget);

Path classes = Files.createDirectories(workdir.resolve("classes"));
Path sources = Files.createDirectories(workdir.resolve("sources"));

Expand Down Expand Up @@ -95,6 +98,15 @@ private static Path ensureEmptyWorkDirectory(Path output, String label) throws I

return Files.createDirectories(dir);
}

private static void ensureEmptySemanticDBDirectory(Path output, String label) throws IOException {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to refactor ensureEmptyWorkDirectory instead of adding this method wich looks almost identical and serves same purpose?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

(But the tests seem flaky... they have failed in different places (unrelated to this change) on last 2 runs).

String base = label.substring(label.lastIndexOf(':') + 1);
Path dir = output.resolveSibling("_semanticdb").resolve(base);

if (Files.exists(dir)) {
deleteRecursively(dir);
}
}

private static String[] collectSrcJarSources(
String[] files, List<File> scalaJarFiles, List<File> javaJarFiles) {
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