5
5
_SemanticDbInfo = "SemanticDbInfo" ,
6
6
)
7
7
8
- def _semanticdb_directory_from_file (file ):
8
+ def _semanticdb_directory_from_output_jar (file ):
9
9
"""
10
- This is janky, but we're limited in what we can do in this function. From the
11
- [documentation](https://bazel.build/rules/lib/builtins/Args#add_all) on `Args#add_all`:
12
-
13
- To avoid unintended retention of large analysis-phase data structures into the execution phase,
14
- the `map_each` function must be declared by a top-level `def` statement; it may not be a
15
- nested function closure by default.
10
+ Using the path of the output JAR to determine the SemanticDB target root is janky, but the
11
+ output directory won't be known at build-time, so we have to use the output JAR path as a proxy.
12
+ It should be built under the same configuration as the SemanticDB files, since both are produced
13
+ by the same compilation action.
16
14
"""
17
15
18
- return file . path [: file .path . find ( "META-INF" ) - 1 ]
16
+ return "{}/semanticdb" . format ( file .dirname )
19
17
20
18
#
21
19
# PHASE: semanticdb
@@ -33,7 +31,13 @@ def phase_semanticdb(ctx, g):
33
31
outputs = []
34
32
35
33
for source in ctx .files .srcs :
36
- if source .extension == "scala" :
34
+ # Generated or external files will have the output directory (beginning with `bazel-out`) in
35
+ # their paths, which we don't want because it isn't guaranteed to be consistent
36
+ if (
37
+ source .extension == "scala" and
38
+ source .is_source and
39
+ source .owner .repo_name == ctx .label .repo_name
40
+ ):
37
41
path = paths .join (
38
42
directory_name ,
39
43
"META-INF" ,
@@ -44,22 +48,21 @@ def phase_semanticdb(ctx, g):
44
48
outputs .append (ctx .actions .declare_file (path ))
45
49
46
50
def add_scalacopts (arguments ):
47
- if len (outputs ) == 0 :
48
- return
51
+ output_jar = g .classpaths .jar
49
52
50
53
if toolchain .scala_configuration .version .startswith ("2" ):
51
54
arguments .add ("--compiler_option=-P:semanticdb:failures:error" )
52
55
arguments .add ("--compiler_option_referencing_path=-P:semanticdb:sourceroot:${workDir}" )
53
56
arguments .add_all (
54
- [outputs [ 0 ] ],
57
+ [output_jar ],
55
58
format_each = "--compiler_option_referencing_path=-P:semanticdb:targetroot:${path} %s" ,
56
- map_each = _semanticdb_directory_from_file ,
59
+ map_each = _semanticdb_directory_from_output_jar ,
57
60
)
58
61
else :
59
62
arguments .add_all (
60
- [outputs [ 0 ] ],
63
+ [output_jar ],
61
64
format_each = "--compiler_option_referencing_path=-semanticdb-target:${path} %s" ,
62
- map_each = _semanticdb_directory_from_file ,
65
+ map_each = _semanticdb_directory_from_output_jar ,
63
66
)
64
67
65
68
arguments .add ("--compiler_option_referencing_path=-sourceroot:${workDir}" )
0 commit comments