Skip to content

Commit 69aec43

Browse files
authored
Semanticdb clear dir (#1580)
* Delete _semanticdb folder before compiling * lint fix * Consolidated some functions in Scalacworker.
1 parent e2fe29c commit 69aec43

File tree

4 files changed

+68
-12
lines changed

4 files changed

+68
-12
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ hash2
99
.metals
1010
.vscode
1111
unformatted-*.backup.scala
12-
.scala-build
12+
.scala-build
13+
test/semanticdb/tempsrc

src/java/io/bazel/rulesscala/scalac/ScalacWorker.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,14 @@ public static void main(String[] args) throws Exception {
3737
public void work(String[] args) throws Exception {
3838
CompileOptions ops = new CompileOptions(args);
3939

40-
Path outputJar = Paths.get(ops.outputName);
41-
Path workdir = ensureEmptyWorkDirectory(outputJar, ops.currentTarget);
42-
Path classes = Files.createDirectories(workdir.resolve("classes"));
43-
Path sources = Files.createDirectories(workdir.resolve("sources"));
40+
Path outputJarPath = Paths.get(ops.outputName);
41+
42+
Path scalacOutPath = clearWorkDirectory(outputJarPath, ops.currentTarget, "_scalac");
43+
clearWorkDirectory(outputJarPath, ops.currentTarget, "_semanticdb");
44+
45+
Files.createDirectories(scalacOutPath);
46+
Path classes = Files.createDirectories(scalacOutPath.resolve("classes"));
47+
Path sources = Files.createDirectories(scalacOutPath.resolve("sources"));
4448

4549
List<File> jarFiles = extractSourceJars(ops, sources);
4650
List<File> scalaJarFiles = filterFilesByExtension(jarFiles, ".scala");
@@ -80,20 +84,20 @@ public void work(String[] args) throws Exception {
8084

8185
/** Now build the output jar */
8286
String[] jarCreatorArgs = {
83-
"-m", ops.manifestPath, "-t", ops.stampLabel, outputJar.toString(), classes.toString()
87+
"-m", ops.manifestPath, "-t", ops.stampLabel, outputJarPath.toString(), classes.toString()
8488
};
8589
JarCreator.main(jarCreatorArgs);
8690
}
8791

88-
private static Path ensureEmptyWorkDirectory(Path output, String label) throws IOException {
92+
private static Path clearWorkDirectory(Path output, String label, String folderName) throws IOException {
8993
String base = label.substring(label.lastIndexOf(':') + 1);
90-
Path dir = output.resolveSibling("_scalac").resolve(base);
94+
Path dir = output.resolveSibling(folderName).resolve(base);
9195

9296
if (Files.exists(dir)) {
9397
deleteRecursively(dir);
9498
}
9599

96-
return Files.createDirectories(dir);
100+
return dir;
97101
}
98102

99103
private static String[] collectSrcJarSources(

test/semanticdb/BUILD

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ toolchain(
3232

3333
scala_library(
3434
name = "all_lib",
35-
srcs = glob(["**/*.scala"]),
35+
srcs = glob(["*.scala"]),
3636
)
3737

3838
semanticdb_vars_script(
@@ -49,3 +49,13 @@ semanticdb_vars_script(
4949
name = "semantic_provider_vars_empty",
5050
dep = "empty_lib",
5151
)
52+
53+
scala_library(
54+
name = "lib_with_tempsrc",
55+
srcs = glob(
56+
[
57+
"*.scala",
58+
"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)
59+
],
60+
),
61+
)

test/shell/test_semanticdb.sh

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,51 @@ test_no_semanticdb() {
134134
fi
135135
}
136136

137+
138+
test_semanticdb_handles_removed_sourcefiles() {
139+
#Ensure absense of bug where bazel failed with 'access denied' on Windows when a source file was removed.
140+
141+
#First add the new scala file, build it, then remove the new scala file, and ensure it builds.
142+
set -e
143+
144+
local toolchainArg=--extra_toolchains=//test/semanticdb:semanticdb_nobundle_toolchain
145+
local newfile_dir=test/semanticdb/tempsrc
146+
local newfilename=D.scala
147+
local newfilepath=$newfile_dir/$newfilename
148+
local rule_label=//test/semanticdb:lib_with_tempsrc
149+
150+
#add new source file and build it
151+
mkdir -p $newfile_dir && echo "class D{ val a = 1; }" > $newfilepath
152+
153+
154+
#make sure D.scala was added to the target (sanity check)
155+
local query_result1=$(bazel query "labels(srcs, $rule_label)")
156+
if [[ $query_result1 != *"$newfilename"* ]] ; then
157+
echo "$newfilename was not properly added as src for target $rule_label"
158+
exit 1
159+
fi
160+
161+
bazel build $rule_label $toolchainArg
162+
163+
#remove the new source file and build it
164+
rm $newfilepath
165+
166+
#make sure D.scala was properly removed from the target(sanity check)
167+
local query_result2=$(bazel query "labels(srcs, $rule_label)")
168+
if [[ $query_result2 == *"$newfilename"* ]] ; then
169+
echo "$newfilename was not properly removed as src for target $rule_label"
170+
exit 1
171+
fi
172+
173+
bazel build $rule_label $toolchainArg
174+
175+
176+
}
177+
137178
run_semanticdb_tests() {
138179
local bundle=1; local nobundle=0
139180
local scala3=3; local scala2=2
140-
181+
141182
$runner test_produces_semanticdb $scala2 $bundle
142183
$runner test_produces_semanticdb $scala2 $nobundle
143184

@@ -147,7 +188,7 @@ run_semanticdb_tests() {
147188
$runner test_produces_semanticdb $scala3 $nobundle
148189

149190
$runner test_no_semanticdb
150-
191+
$runner test_semanticdb_handles_removed_sourcefiles
151192
}
152193

153194
run_semanticdb_tests

0 commit comments

Comments
 (0)