Search code examples
shake-build-system

Is it possible for Shake to change a source file?


When running tools such as formatters and linting tools with "auto-correction" options, it can be that the input and output for a Rule are the same file; for example:

  "//*.hs" %> \out ->
    cmd_ "ormolu" "-m" "inplace" out

-- OR

  batch 10 ("//*.hs" %>)
    ( \out -> do
      cmd_ "ormolu" "-m" "inplace" out
      pure out
    )
    (cmd_ "hlint")

This seems to work "correctly" (the rule is re-run if the source file is needed and has changed), but we're unsure if this is a happy coincidence or shake working as designed - especially when we start thinking about cached results from shakeShare or in the future Cloud Shake. Is this the best way to handle this type of rule, or is there something better?


Solution

  • There is no principled way to generate a rule that replaces a source file in Shake. Given a source code formatter, anything else isn't very usfeul. Shake makes the assumption that inputs don't change while the compilation is ongoing. It's likely that passing --lint will lead to a lint error and that it would be incompatible with Cloud Shake. The official advice would be to make such changes in a separate non-Shake pass before you call shake.

    However, if it works for you, and is useful, I wouldn't overly worry. The pattern has tests in Shake, it's something plenty of people do. You can turn off Cloud caching on a per file basis with historyDisable.