on master help sign in
Merge pull request #653 from obsidiansystems/eac@warn-ambiguous-package

Warn when being forced to disambiguate packages in a session

commitfb0c347e488cf53b7c37f37c93927931095d17bd authorElliot Cameron <3noch@users.noreply.github.com> dateWed 12 Feb 2020, 10:57 (7 years ago) parentcb654e43ebde24
3 files changed+25−15
ChangeLog.md+2 lib/command/src/Obelisk/Command/Project.hs+1−1 lib/command/src/Obelisk/Command/Run.hs+22−14
ChangeLog.md+2
@@ -5,6 +5,8 @@ This project's release branch is `master`. This log is written from the perspect
55## Unreleased
66
77* Fix a bug in `Obelisk.Route.Frontend` where `routeLink`, `routeLinkDynAttr`, and `dynRouteLink` would not behave exactly like `<a href="...">` when run by JavaScript. These functions now scroll to the top of the page when the link is clicked. ([#540](https://github.com/obsidiansystems/obelisk/pull/540))
8+* Fix a bug in `ob run`/`ob watch`/`ob repl` where nested Obelisk projects would also be loaded into the session. These are now ignored. ([#652](https://github.com/obsidiansystems/obelisk/pull/652))
9+* Improve behavior of `ob run`/`ob watch`/`ob repl` when multiple packages with the same name are encountered. Obelisk now issues a warning and tells you which one it will use. ([#653](https://github.com/obsidiansystems/obelisk/pull/653))
810
911## v0.5.0.0 - 2020-02-07
1012
lib/command/src/Obelisk/Command/Project.hs+1−1
@@ -186,7 +186,7 @@ findProjectRoot target = do
186186 targetStat <- liftIO $ getFileStatus target
187187 umask <- liftIO getUmask
188188 (result, _) <- liftIO $ runStateT (walkToProjectRoot target targetStat umask myUid) []
189− return result
189+ return $ makeRelative "." <$> result
190190
191191withProjectRoot :: MonadObelisk m => FilePath -> (FilePath -> m a) -> m a
192192withProjectRoot target f = findProjectRoot target >>= \case
lib/command/src/Obelisk/Command/Run.hs+22−14
@@ -8,6 +8,7 @@ module Obelisk.Command.Run where
88
99import Control.Arrow ((&&&))
1010import Control.Exception (Exception, bracket)
11+import Control.Lens (ifor)
1112import Control.Monad (filterM, unless)
1213import Control.Monad.Except (runExceptT, throwError)
1314import Control.Monad.IO.Class (liftIO)
@@ -46,8 +47,8 @@ import qualified System.Info
4647import System.IO.Temp (withSystemTempDirectory)
4748
4849import Obelisk.App (MonadObelisk)
49−import Obelisk.CliApp (Severity (..) , failWith, putLog, proc, readCreateProcessWithExitCode, readProcessAndLogStderr)
50−import Obelisk.Command.Project (withProjectRoot, nixShellWithPkgs, toNixPath)
50+import Obelisk.CliApp (Severity (..) , failWith, putLog, proc, readCreateProcessWithExitCode, readProcessAndLogStderr, setCwd)
51+import Obelisk.Command.Project (withProjectRoot, nixShellWithPkgs)
5152import Obelisk.Command.Utils (findExePath, ghcidExePath, nixBuildExePath, nixExePath)
5253
5354data CabalPackageInfo = CabalPackageInfo
@@ -73,29 +74,26 @@ run :: MonadObelisk m => m ()
7374run = withProjectRoot "." $ \root -> do
7475 pkgs <- fmap toList . parsePackagesOrFail =<< getLocalPkgs root
7576 withGhciScript pkgs root $ \dotGhciPath -> do
76− freePort <- getFreePort
7777 assets <- do
78− let importableRoot = toNixPath root
79− isDerivation <- readProcessAndLogStderr Debug $
78+ isDerivation <- readProcessAndLogStderr Debug $ setCwd (Just root) $
8079 proc nixExePath
8180 [ "eval"
82− , "-f"
83− , root
84− , "(let a = import " <> importableRoot <> " {}; in toString (a.reflex.nixpkgs.lib.isDerivation a.passthru.staticFilesImpure))"
81+ , "(let a = import ./. {}; in toString (a.reflex.nixpkgs.lib.isDerivation a.passthru.staticFilesImpure))"
8582 , "--raw"
8683 -- `--raw` is not available with old nix-instantiate. It drops quotation
8784 -- marks and trailing newline, so is very convenient for shelling out.
8885 ]
8986 -- Check whether the impure static files are a derivation (and so must be built)
9087 if isDerivation == "1"
91− then fmap T.strip $ readProcessAndLogStderr Debug $ -- Strip whitespace here because nix-build has no --raw option
88+ then fmap T.strip $ readProcessAndLogStderr Debug $ setCwd (Just root) $ -- Strip whitespace here because nix-build has no --raw option
9289 proc nixBuildExePath
9390 [ "--no-out-link"
94− , "-E", "(import " <> importableRoot <> "{}).passthru.staticFilesImpure"
91+ , "-E", "(import ./. {}).passthru.staticFilesImpure"
9592 ]
96− else readProcessAndLogStderr Debug $
97− proc nixExePath ["eval", "-f", root, "passthru.staticFilesImpure", "--raw"]
93+ else readProcessAndLogStderr Debug $ setCwd (Just root) $
94+ proc nixExePath ["eval", "-f", ".", "passthru.staticFilesImpure", "--raw"]
9895 putLog Debug $ "Assets impurely loaded from: " <> assets
96+ freePort <- getFreePort
9997 runGhcid root True dotGhciPath pkgs $ Just $ unwords
10098 [ "Obelisk.Run.run"
10199 , show freePort
@@ -128,7 +126,7 @@ getLocalPkgs root = do
128126 -- We do not want to find packages that are embedded inside other obelisk projects, unless that
129127 -- obelisk project is our own.
130128 let exclusions = filter (/= root) $ map takeDirectory obeliskPaths
131− runFind $
129+ fmap (map (makeRelative ".")) $ runFind $
132130 ["-L", root, "(", "-name", "*.cabal", "-o", "-name", Hpack.packageConfig, ")", "-a", "-type", "f"]
133131 <> concat [["-not", "-path", p </> "*"] | p <- exclusions]
134132 where
@@ -245,7 +243,17 @@ parsePackagesOrFail dirs = do
245243 | _cabalPackageInfo_buildable packageInfo -> Right packageInfo
246244 _ -> Left dir
247245
248− packageInfos <- case NE.nonEmpty packageInfos' of
246+ let packagesByName = Map.fromListWith (<>) [(_cabalPackageInfo_packageName p, p NE.:| []) | p <- packageInfos']
247+ unambiguous <- ifor packagesByName $ \packageName ps -> case ps of
248+ p NE.:| [] -> pure p -- No ambiguity here
249+ p NE.:| _ -> do
250+ putLog Warning $ T.pack $
251+ "Packages named '" <> T.unpack packageName <> "' appear in " <> show (length ps) <> " different locations: "
252+ <> intercalate ", " (map _cabalPackageInfo_packageFile $ toList ps)
253+ <> "; Picking " <> _cabalPackageInfo_packageFile p
254+ pure p
255+
256+ packageInfos <- case NE.nonEmpty $ toList unambiguous of
249257 Nothing -> failWith $ T.pack $ "No valid, buildable packages found in " <> intercalate ", " dirs
250258 Just xs -> pure xs
251259