You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Bazel pushes towards explicit and minimal dependencies to keep BUILD file hygiene and allow for targets to refactor their dependencies without fear of downstream breaking.
163
-
Currently rules_scala does this at the cost of having cryptic `scalac` errors when one mistakenly depends on a transitive dependency or, as more often the case for some, a transitive dependency is needed to [please scalac](https://github.com/scalacenter/advisoryboard/blob/master/proposals/009-improve-direct-dependency-experience.md) itself.
164
-
To learn more about the motivation of strict-deps itself you can visit this Bazel blog [post](https://blog.bazel.build/2017/06/28/sjd-unused_deps.html) on the subject.
161
+
## [Experimental] Dependency options
165
162
166
-
To use it just add `--strict_java_deps=WARN|ERROR` to your `bazel` invocation.
167
-
In both cases of `WARN` or `ERROR` you will get the following text in the event of a violation:
163
+
There are a number of dependency options which can be set in the scala toolchain. These include `dependency_mode`, `strict_deps_mode`, `unused_dependency_checker_mode`, and `dependency_tracking_method`.
164
+
165
+
### [Experimental] Recommended options
166
+
167
+
We recommend one of the following sets of options
168
+
169
+
**Option A**
170
+
Accept the defaults, which might work well enough for you. The defaults are
168
171
```
169
-
...
170
-
Target '//some_package:transitive_dependency' is used but isn't explicitly declared, please add it to the deps.
but you do not need to include this in the toolchain as they are the defaults.
178
+
179
+
**Option B**
180
+
```
181
+
dependency_mode = "plus-one",
182
+
strict_deps_mode = "error",
183
+
unused_dependency_checker_mode = "error",
184
+
dependency_tracking_method = "ast",
173
185
```
174
-
Note that if you have `buildozer` installed you can just run the last line and have it automatically apply the fix for you.
175
186
176
-
**Caveats:**
187
+
Should the first option result in too much effort in handling build files and the like due to confusing dependencies and you becoming confused as to why some specific dependency is needed when the code being compiled never references it, consider this set of options. It will include both dependencies and dependencies of dependencies, which in practice is enough to stop almost all strange missing dependency errors at the cost of somewhat more incremental compile cost in certain cases.
188
+
189
+
With these settings, we also will error on dependencies which are unneeded, and dependencies which should be included in `deps` due to be directly referenced in the code, but are not.
190
+
191
+
The dependency tracking method `ast` is experimental but so far proves to be better than the default for computing the direct dependencies for `plus-one` mode code. In the future we hope to make this the default for `plus-one` mode and remove the option altogether.
192
+
193
+
### [Experimental] Dependency mode
194
+
195
+
There are three dependency modes. The reason for the multiple modes is that often `scalac` depends on jars which seem unnecessary at first glance. Hence, in order to reduce the need to please `scalac`, we provide the following options.
196
+
- `dependency_mode = "direct"` - only include direct dependencies during compiliation; that is, those in the `deps` attribute
197
+
- `dependency_mode = "plus-one"` - only include `deps` and `deps` of `deps` during compiliation.
198
+
- `dependency_mode = "transitive"` - all transitive dependencies are included during compiliation. That is, `deps`, `deps` of `deps`, `deps` of `deps` of `deps`, and so on.
199
+
200
+
Note when a dependency is included, that means its jars are included on the classpath, along with the jars of any targets that it exports.
201
+
202
+
When using `direct` mode, there can be cryptic `scalac` errors when one mistakenly depends on a transitive dependency or, as more often the case for some, a transitive dependency is needed to [please scalac](https://github.com/scalacenter/advisoryboard/blob/master/proposals/009-improve-direct-dependency-experience.md) itself.
203
+
204
+
As one goes down the list, more dependencies are included which helps reduce confusing requirements to add `deps`, at the cost of increased incremental builds due to a greater number of dependencies. In practice, using `plus-one` deps results in almost no confusing `deps` entries required while still being relatively small in terms of the number of total dependencies included.
205
+
206
+
**Caveats for `plus_one` and `transitive`:**
177
207
<ul>
178
-
<li>Extra builds- when strict-deps is on the transitive dependencies are inputs to the compilation action which means you can potentially have more build triggers for changes the cross the ijar boundary </li>
208
+
<li>Extra builds- Extra dependencies are inputs to the compilation action which means you can potentially have more build triggers for changes the cross the ijar boundary </li>
179
209
<li>Label propagation- since label of targets are needed for the clear message and since it's not currently supported by JavaInfo from bazel we manually propagate it. This means that the error messages have a significantly lower grade if you don't use one of the scala rules or scala_import (since they don't propagate these labels)</li>
180
210
<li>javac outputs incorrect targets due to a problem we're tracing down. Practically we've noticed it's pretty trivial to understand the correct target (i.e. it's almost a formatting problem) </li>
181
211
</ul>
182
212
183
-
Note: Currently strict-deps is protected by a feature toggle but we're strongly considering making it the default behavior as `java_*` rules do.
213
+
Note: the last two issues are bugs which will be addressed by [https://github.com/bazelbuild/rules_scala/issues/839].
214
+
215
+
### [Experimental] Strict deps mode
216
+
We have a strict dependency checker which requires that any type referenced in the sources of a scala target should be included in that rule's deps. To learn about the motivation for this you can visit this Bazel blog [post](https://blog.bazel.build/2017/06/28/sjd-unused_deps.html) on the subject.
184
217
185
-
## [Experimental] Unused dependency checking
218
+
The option `strict_deps_mode` can be set to `off`, `warn`, or `error`. We highly recommend setting it to `error`.
219
+
220
+
In both cases of `warn` or `error` you will get the following text in the event of a violation:
221
+
```
222
+
...
223
+
Target '//some_package:transitive_dependency' is used but isn't explicitly declared, please add it to the deps.
Currently unused dependency checking and strict-deps can't be used simultaneously, if both are set only strict-deps will run.
196
-
197
239
Unused dependency checking can either be enabled globally for all targets using a scala toolchain or for individual targets using the
198
-
`unused_dependency_checker_mode`attribute. The feature is still experimental and there can thus be cases where it works incorrectly,
199
-
in these cases you can enable unused dependency checking globally through a toolchain and override individual misbehaving targets
200
-
using the attribute.
240
+
`unused_dependency_checker_mode` attribute.
241
+
242
+
The feature is still experimental and there can thus be cases where it works incorrectly, in these cases you can enable unused dependency checking globally through a toolchain and disable reports of individual misbehaving targets with `unused_dependency_checker_ignored_targets` which is a list of labels.
243
+
244
+
### [Experimental] Dependency tracking method
245
+
246
+
The strict dependency tracker and unused dependency tracker need to track the used dependencies of a scala compilation unit. This toggle allows one to pick which method of tracking to use.
247
+
248
+
- `dependency_tracking_method = "high-level"` - This is the existing tracking method which has false positives and negatives but generally works reasonably well for `direct` dependency mode.
249
+
- `dependency_tracking_method = "ast"` - This is a new tracking method which is being developed for `plus-one` and `transitive` dependency modes. It is still being developed and may have issues which need fixing. If you discover an issue, please submit a small repro of the problem.
250
+
251
+
Note we intend to eventually remove this flag and use `high-level` as the method for `direct` dependency mode, and `ast` as the method for `plus-one` and `transitive` dependency modes.
252
+
253
+
In the meantime, if you are using `plus-one` or `transitive` dependency modes, you can use `ast` dependency tracking mode and see how well it works for you.
254
+
255
+
### [Experimental] Turning on strict_deps_mode/unused_dependency_checker_mode
256
+
257
+
It can be daunting to turn on strict deps checking or unused dependency mode checking on a large codebase. However, it need not be so bad if this is done in phases
258
+
259
+
1. Have a default scala toolchain `A` with the option of interest set to `off` (the starting state)
260
+
2. Create a second scala toolchain `B` with the option of interest set to `warn` or `error`. Those who are working on enabling the flag can run with this toolchain as a command line argument to help identify issues and fix them.
261
+
3. Once all issues are fixed, change `A` to have the option of interest set to `error` and delete `B`.
262
+
263
+
We recommend turning on strict_deps_mode first, as rule `A` might have an entry `B` in its `deps`, and `B` in turn depends on `C`. Meanwhile, the code of `A` only uses `C` but not `B`. Hence, the unused dependency checker, if on, will request that `B` be removed from `A`'s deps. But this will lead to a compile error as `A` can no longer depend on `C`. However, if strict dependency checking was on, then `A`'s deps is guaranteed to have `C` in it.
264
+
265
+
### [Experimental] Migrating from deprecated configurations
266
+
267
+
There are a few deprecated configuration methods which we will be removing in the near future.
268
+
269
+
- `plus_one_deps_mode = "on"` on the scala toolchain. Instead, set `dependency_mode = "plus-one"` on the scala toolchain. `plus_one_deps_mode` will be removed in the future.
270
+
- The command line argument `--strict_java_deps=WARN/ERROR`. Instead, set `dependency_mode = "transitive"` on the scala toolchain, and if only a warning is desired set `strict_deps_mode = "warn"` on the toolchain. In the future, `strict_java_deps` will no longer affect how scala files are compiled. Note that `strict_java_deps` will still control java compilation.
201
271
202
272
## Advanced configurable rules
203
273
To make the ruleset more flexible and configurable, we introduce a phase architecture. By using a phase architecture, where rule implementations are defined as a list of phases that are executed sequentially, functionality can easily be added (or modified) by adding (or swapping) phases.
0 commit comments