Search code examples
shake-build-system

Why Shake dependencies are explicitly `needed`?


I find first example of Shake usage demonstrating a pattern that seems error prone:

    contents <- readFileLines $ out -<.> "txt"
    need contents
    cmd "tar -cf" [out] contents

Why do we need need contents when readFileLines reads them and cmd references them? Is this so we can avoid requiring ApplicativeDo?


Solution

  • I think part of the confusion may be the types/semantics of contents. The file out -<.> "txt" contains a list of filenames, so contents is a list of filenames. When we need contents we are requiring the files themselves be created and depended upon, using the filenames to specify which files. When we pass contents on to cmd we are passing the filenames which tar will use to query the files.

    So the key point is that readFileLines doesn't read the files in question, it only reads the filenames out of another file. We have to use need to make sure that using the files is fine, and then we actually use the files in cmd. Another way of looking at the three lines is:

    1. Which files do we want to operate on?
    2. Make sure those files are ready.
    3. Use those files.

    Does that make sense? There's no relationship with ApplicativeDo - it's presence wouldn't help us at all.