Skip to content
  • Duncan Coutts's avatar
    Refactor the Rebuild monad to keep file root in Reader env · bba5a817
    Duncan Coutts authored
    Fixes #3323 and #3324, ensuring we monitor the project Cabal files.
    Original fix by Edward Z. Yang. The approach in this patch is to fix an
    underlying problem. Subsequent patches use a more consistent approach to
    the monitoring as suggested by Edward.
    
    The motivating example is:
    
      matches <- matchFileGlob dirname glob
    
    where matchFileGlob is defined in the RebuildMonad module as
    
    matchFileGlob root glob = do
        monitorFiles [monitorFileGlob glob]
        liftIO $ Glob.matchFileGlob root glob
    
    This usage is wrong because the root used to match the glob is not the
    same as the root that will be used later when checking the file monitor
    for changes. You can see this is suspicious because the declaration of
    the monitor does not take an root dir paramater but the immediate
    matching does. That's because the root for the monitors is specified
    when we do the rerunIfChanged to check the monitor.
    
    So the only correct usage involves passing in the correct root. This is
    a ripe source of bugs. So this refactoring moves the root into the
    Rebuild monad directly, so the example becomes:
    
      matches <- matchFileGlob glob
    
    The root is implicit, so you can't accidentally pick a different root
    for the immediate match vs the later monitor check. Of course the root
    still matters, but if you get that wrong you'll notice immediately
    because you will not get the match results you were expecting.
    
    So the root is now passed in with runRebuild, not with rerunIfChanged.
    
    Also change the incorrect use of matchFileGlob. This use case now
    relies on the adjusted representation of glob roots, using
    FilePath.splitDrive to obtain the root (if any).
    bba5a817