diff --git a/ChangeLog.md b/ChangeLog.md index 19acc3d0fe..0727afc837 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -13,6 +13,10 @@ Major changes: Behavior changes: +* File watching now takes into account specified targets, old behavior could + be restored using the new flag `--watch-all` + [#5310](https://github.com/commercialhaskell/stack/issues/5310) + Other enhancements: * `stack ls dependencies json` now includes fields `sha256` and `size` for diff --git a/doc/build_command.md b/doc/build_command.md index 295bcffcea..c8e5765d03 100644 --- a/doc/build_command.md +++ b/doc/build_command.md @@ -157,7 +157,10 @@ all of them, please use `stack build --help`. Some particularly convenient ones worth mentioning here since they compose well with the rest of the build system as described: -* `--file-watch` will rebuild your project every time a file changes +* `--file-watch` will rebuild your project every time a file changes, by default + it will take into account all files belonging to the targets you specify, + alternatively one could specify `--watch-all` which will make Stack watch + any local files (from project packages or from local dependencies) * `--exec "cmd [args]"` will run a command after a successful build To come back to the composable approach described above, consider this final diff --git a/src/Stack/Build.hs b/src/Stack/Build.hs index c918290ef9..d4f05de350 100644 --- a/src/Stack/Build.hs +++ b/src/Stack/Build.hs @@ -68,11 +68,22 @@ build msetLocalFiles = do checkSubLibraryDependencies (Map.elems $ smProject sourceMap) + boptsCli <- view $ envConfigL.to envConfigBuildOptsCLI -- Set local files, necessary for file watching stackYaml <- view stackYamlL for_ msetLocalFiles $ \setLocalFiles -> do - files <- sequence - [lpFiles lp | lp <- allLocals] + files <- + if boptsCLIWatchAll boptsCli + then sequence [lpFiles lp | lp <- allLocals] + else forM allLocals $ \lp -> do + let pn = packageName (lpPackage lp) + case Map.lookup pn (smtTargets $ smTargets sourceMap) of + Nothing -> + pure Set.empty + Just (TargetAll _) -> + lpFiles lp + Just (TargetComps components) -> + lpFilesForComponents components lp liftIO $ setLocalFiles $ Set.insert stackYaml $ Set.unions files checkComponentsBuildable allLocals @@ -81,7 +92,6 @@ build msetLocalFiles = do (installedMap, globalDumpPkgs, snapshotDumpPkgs, localDumpPkgs) <- getInstalled installMap - boptsCli <- view $ envConfigL.to envConfigBuildOptsCLI baseConfigOpts <- mkBaseConfigOpts boptsCli plan <- constructPlan baseConfigOpts localDumpPkgs loadPackage sourceMap installedMap (boptsCLIInitialBuildSteps boptsCli) diff --git a/src/Stack/Options/BuildParser.hs b/src/Stack/Options/BuildParser.hs index a81f49d189..2d87cd3ee6 100644 --- a/src/Stack/Options/BuildParser.hs +++ b/src/Stack/Options/BuildParser.hs @@ -71,6 +71,9 @@ buildOptsParser cmd = help "Like --file-watch, but polling the filesystem instead of using events") <|> pure NoFileWatch) <*> + switch + (long "watch-all" <> + help "Watch all local files not taking targets into account") <*> many (cmdOption (long "exec" <> metavar "CMD [ARGS]" <> diff --git a/src/Stack/Types/Config/Build.hs b/src/Stack/Types/Config/Build.hs index c22bf4ac29..c329293793 100644 --- a/src/Stack/Types/Config/Build.hs +++ b/src/Stack/Types/Config/Build.hs @@ -133,6 +133,7 @@ defaultBuildOptsCLI = BuildOptsCLI , boptsCLIGhcOptions = [] , boptsCLIBuildSubset = BSAll , boptsCLIFileWatch = NoFileWatch + , boptsCLIWatchAll = False , boptsCLIExec = [] , boptsCLIOnlyConfigure = False , boptsCLICommand = Build @@ -166,6 +167,7 @@ data BuildOptsCLI = BuildOptsCLI , boptsCLIFlags :: !(Map ApplyCLIFlag (Map FlagName Bool)) , boptsCLIBuildSubset :: !BuildSubset , boptsCLIFileWatch :: !FileWatchOpts + , boptsCLIWatchAll :: !Bool , boptsCLIExec :: ![(String, [String])] , boptsCLIOnlyConfigure :: !Bool , boptsCLICommand :: !BuildCommand diff --git a/src/Stack/Types/Package.hs b/src/Stack/Types/Package.hs index e3a580bbb6..a177e252a0 100644 --- a/src/Stack/Types/Package.hs +++ b/src/Stack/Types/Package.hs @@ -320,6 +320,14 @@ instance Show (MemoizedWith env a) where lpFiles :: HasEnvConfig env => LocalPackage -> RIO env (Set.Set (Path Abs File)) lpFiles = runMemoizedWith . fmap (Set.unions . M.elems) . lpComponentFiles +lpFilesForComponents :: HasEnvConfig env + => Set NamedComponent + -> LocalPackage + -> RIO env (Set.Set (Path Abs File)) +lpFilesForComponents components lp = runMemoizedWith $ do + componentFiles <- lpComponentFiles lp + pure $ mconcat (M.elems (M.restrictKeys componentFiles components)) + -- | A location to install a package into, either snapshot or local data InstallLocation = Snap | Local deriving (Show, Eq)