title: How do I load `config_setting()` into my `.bzl` file?
body: <p>My motivation: our codebase is scattered across over at least 20 git repos. I want to consolidate everything into a single git repo with a single build system. Currently we use SBT, but we think the build would take too long, so I am examining the possibility of using Bazel instead.</p>
  
  <p>Most of our codebase uses Scala 2.12, some of our codebase uses Scala 2.11, and the rest needs to build under both Scala 2.11 and Scala 2.12.</p>
  
  <p>I'm trying to use <a href="https://github.com/bazelbuild/rules_scala" rel="nofollow noreferrer">bazelbuild/rules_scala</a>.</p>
  
  <p>With the following call to <code>scala_repositories</code> in my <code>WORKSPACE</code>, I can build using Scala 2.12:</p>
  
  <pre><code>scala_repositories(("2.12.6", {
      "scala_compiler": "3023b07cc02f2b0217b2c04f8e636b396130b3a8544a8dfad498a19c3e57a863",
      "scala_library": "f81d7144f0ce1b8123335b72ba39003c4be2870767aca15dd0888ba3dab65e98",
      "scala_reflect": "ffa70d522fc9f9deec14358aa674e6dd75c9dfa39d4668ef15bb52f002ce99fa"
  }))
  </code></pre>
  
  <p>If I have the following call instead, I can build using Scala 2.11:</p>
  
  <pre><code>scala_repositories(("2.11.12", {
      "scala_compiler": "3e892546b72ab547cb77de4d840bcfd05c853e73390fed7370a8f19acb0735a0",
      "scala_library": "0b3d6fd42958ee98715ba2ec5fe221f4ca1e694d7c981b0ae0cd68e97baf6dce",
      "scala_reflect": "6ba385b450a6311a15c918cf8688b9af9327c6104f0ecbd35933cfcd3095fe04"
  }))
  </code></pre>
  
  <p>However, it is not possible to specify in my <code>BUILD</code> files on a package level which version(s) of Scala to build with. I must specify this globally in my <code>WORKSPACE</code>.</p>
  
  <p>To workaround this, my plan is to set up <a href="https://docs.bazel.build/versions/master/be/common-definitions.html#configurable-attributes" rel="nofollow noreferrer">configurable attributes</a>, so I can specify <code>--define scala=2.11</code> to build with Scala 2.11, and specify <code>--define scala=2.12</code> to build with Scala 2.12.</p>
  
  <p>First I tried by putting this code in my <code>WORKSPACE</code>:</p>
  
  <pre><code>config_setting(
      name = "scala-2.11",
      define_values = {
          "scala": "2.11"
      }
  )
  
  config_setting(
      name = "scala-2.12",
      define_values = {
          "scala": "2.12"
      }
  )
  
  scala_repositories(
      select(
          {
              "scala-2.11": "2.11.12",
              "scala-2.12": "2.12.6"
          }
      ),
      select(
          {
              "scala-2.11": {
                  "scala_compiler": "3e892546b72ab547cb77de4d840bcfd05c853e73390fed7370a8f19acb0735a0",
                  "scala_library": "0b3d6fd42958ee98715ba2ec5fe221f4ca1e694d7c981b0ae0cd68e97baf6dce",
                  "scala_reflect": "6ba385b450a6311a15c918cf8688b9af9327c6104f0ecbd35933cfcd3095fe04",
              },
              "scala-2.12": {
                  "scala_compiler": "3023b07cc02f2b0217b2c04f8e636b396130b3a8544a8dfad498a19c3e57a863",
                  "scala_library": "f81d7144f0ce1b8123335b72ba39003c4be2870767aca15dd0888ba3dab65e98",
                  "scala_reflect": "ffa70d522fc9f9deec14358aa674e6dd75c9dfa39d4668ef15bb52f002ce99fa"
              }
          }
      )
  )
  </code></pre>
  
  <p>But this gave me the error <code>config_setting cannot be in the WORKSPACE file</code>.</p>
  
  <p>So then I tried moving code into a Starlark file.</p>
  
  <p>In <code>tools/build_rules/scala.bzl</code>:</p>
  
  <pre><code>config_setting(
      name = "scala-2.11",
      define_values = {
          "scala": "2.11"
      }
  )
  
  config_setting(
      name = "scala-2.12",
      define_values = {
          "scala": "2.12"
      }
  )
  
  def scala_version():
      return select(
          {
              "scala-2.11": "2.11.12",
              "scala-2.12": "2.12.6"
          }
      )
  
  def scala_machinery():
      return select(
          {
          "scala-2.11": {
              "scala_compiler": "3e892546b72ab547cb77de4d840bcfd05c853e73390fed7370a8f19acb0735a0",
              "scala_library": "0b3d6fd42958ee98715ba2ec5fe221f4ca1e694d7c981b0ae0cd68e97baf6dce",
              "scala_reflect": "6ba385b450a6311a15c918cf8688b9af9327c6104f0ecbd35933cfcd3095fe04",
          },
          "scala-2.12": {
              "scala_compiler": "3023b07cc02f2b0217b2c04f8e636b396130b3a8544a8dfad498a19c3e57a863",
              "scala_library": "f81d7144f0ce1b8123335b72ba39003c4be2870767aca15dd0888ba3dab65e98",
              "scala_reflect": "ffa70d522fc9f9deec14358aa674e6dd75c9dfa39d4668ef15bb52f002ce99fa"
              }
          }
      )
  </code></pre>
  
  <p>And back in my <code>WORKSPACE</code>:</p>
  
  <pre><code>load("//tools/build_rules:scala.bzl", "scala_version", "scala_machinery")
  scala_repositories(scala_version(), scala_machinery())
  </code></pre>
  
  <p>But now I get this error:</p>
  
  <pre><code>tools/build_rules/scala.bzl:1:1: name 'config_setting' is not defined
  </code></pre>
  
  <p>This confuses me, because I thought <code>config_setting()</code> was built in. I can't find where I should load it in from.</p>
  
  <p>So, my questions:</p>
  
  <ul>
  <li>How do I load <code>config_setting()</code> into my <code>.bzl</code> file?</li>
  <li>Or, is there a better way of controlling from the command line which arguments get passed to <code>scala_repositories()</code>?</li>
  <li>Or, is this just not possible?</li>
  </ul>
  
  
  
  <pre><code>$ bazel version
  Build label: 0.17.2-homebrew
  Build target: bazel-out/darwin-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
  Build time: Fri Sep 28 10:42:37 2018 (1538131357)
  Build timestamp: 1538131357
  Build timestamp as int: 1538131357
  </code></pre>