title: Optional file dependencies in Bazel?
body: <p>Is there a way to specify optional dependencies in Bazel?</p>
  
  <p>I'd like to make a rule to somewhat mirror Kitware's <a href="https://cmake.org/cmake/help/latest/module/ExternalData.html" rel="nofollow noreferrer">ExternalData</a>, but I would like to see if I can enable workflows where the developer edits the file in-tree, ideally without needing to modify the <code>BUILD</code> file.</p>
  
  <h2>Ideal Workflow</h2>
  
  <ul>
  <li>Define a rule, <code>external_data</code>, which can fetch a file from a given server given its SHA-512.</li>
  <li>If the file already exists, check it's SHA-512.
  
  <ul>
  <li>If that is what is requested, symlink / copy this file (ensuring that no tests can modify the original file).</li>
  <li>If it is different, print a <em>warning</em>, but proceed as normal, to allow for developers to quickly modify the large files as they need.</li>
  </ul></li>
  </ul>
  
  <p>I would like to do this such that Bazel can switch between the file being present and not, and be robust to false-positives on caching. An example scenario that I would like to avoid, if I were to not include it as an optional dependency:</p>
  
  <ol>
  <li>In a prior run, the file was in the workspace, Bazel built the target, everything's fine and dandy.</li>
  <li>Developer removes the file from the workspace after uploading, satisfied with their changes and wanting to test the download process.</li>
  <li>When running the downstream target, Bazel doesn't care about the change in the workspace since it's not an explicit dependency, and the symlink is invalidated, and the test crashes and burns.</li>
  </ol>
  
  <p>To me, it seems like I'd run into this if I tried to implement a <s><code>repository_rule</code></s> <code>rule</code> which manually checks for the file existence, and conditionally executes (I'm not sure if analysis would retrigger this rule being "evaluated" if Step 2 happens.).</p>
  
  <h2>Workaround</h2>
  
  <p>My current thought for an alternative workflow is to have an explicit option for <code>external_data</code>, <code>use_workspace</code>: if <code>False</code>, it will download the file; if <code>True</code>, it will just mirror <code>exports_files([])</code>. The developer can then set this when modifying files.</p>
  
  <p>(Ideally, I'd like to <em>optionally</em> include a file which indicates the SHA (<code>${file}.sha512</code>), but this seems to go back to the original ask.)</p>