Skip to content

Better support for running scripts. #7851

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Dec 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
0966e4e
Add support for script build caching to cabal run
bacchanalia Dec 1, 2021
fee0dc8
Add support for scripts to cabal build.
bacchanalia Dec 2, 2021
a7d75b2
Add script support to cabal clean.
bacchanalia Dec 2, 2021
0579010
Add script support to cabal repl
bacchanalia Dec 2, 2021
744f258
Added changelog for pr #7851
bacchanalia Dec 2, 2021
b004364
Fix `cabal run script.hs` issue with --builddir
bacchanalia Dec 4, 2021
0884e10
Fixes for `build script` and `repl script`
bacchanalia Dec 7, 2021
36e6386
Bug fixes relating to script support
bacchanalia Dec 7, 2021
d379d22
Add tests for improved script support
bacchanalia Dec 7, 2021
4e03925
Fix clean bug uncovered by 5fad1214
bacchanalia Dec 7, 2021
856134d
Update documentation for better script support
bacchanalia Dec 7, 2021
7df7d9e
Attempt to fix `repl script` on Windows
bacchanalia Dec 8, 2021
12db50a
Attempt to fix remote test failures
bacchanalia Dec 8, 2021
f210db7
Attempt to fix `repl script` on Windows
bacchanalia Dec 8, 2021
3405045
Attempt to fix tests on old ghc versions
bacchanalia Dec 8, 2021
7bbecd7
Feedback: Update docs and formatting
bacchanalia Dec 9, 2021
8f38600
Feedback: code style changes
bacchanalia Dec 9, 2021
119afad
Feedback: make hidden control flow explicit
bacchanalia Dec 9, 2021
ffc5f03
Feedback: add expected fail script run tests
bacchanalia Dec 10, 2021
2faded6
Fix `repl script` when cwd is deeper than cachedir
bacchanalia Dec 10, 2021
3ddcd64
Use script in-place for build or run
bacchanalia Dec 11, 2021
2a6e714
Fix file-locking issue on Windows
bacchanalia Dec 13, 2021
97f6c3c
Fix script recompilation based on cwd
bacchanalia Dec 13, 2021
fbee661
Make `repl script` respect --repl-no-load
bacchanalia Dec 16, 2021
d33e070
Feedback: minor refactor
bacchanalia Dec 17, 2021
6ee85f1
Feedback: refactor and comments for repl options
bacchanalia Dec 18, 2021
cb2bc05
Don't use hs-source-dirs for scripts.
bacchanalia Dec 23, 2021
449a411
Update changelog for PR #7851
bacchanalia Dec 24, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Cabal/src/Distribution/Types/PackageName/Magic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ import Distribution.Types.Version
nonExistentPackageThisIsCabalBug :: PackageName
nonExistentPackageThisIsCabalBug = mkPackageName "nonexistent-package-this-is-a-cabal-bug"

-- | Used by @cabal new-repl@ and @cabal new-run@
-- | Used by @cabal new-repl@, @cabal new-run@ and @cabal new-build@
fakePackageName :: PackageName
fakePackageName = mkPackageName "fake-package"

-- | Used by @cabal new-run@ and @cabal new-build@
fakePackageCabalFileName :: FilePath
fakePackageCabalFileName = "fake-package.cabal"

-- | 'fakePackageName' with 'version0'.
fakePackageId :: PackageId
fakePackageId = PackageIdentifier fakePackageName version0
1 change: 1 addition & 0 deletions cabal-install/cabal-install.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ library
Distribution.Client.Sandbox
Distribution.Client.Sandbox.PackageEnvironment
Distribution.Client.SavedFlags
Distribution.Client.ScriptUtils
Distribution.Client.Security.DNS
Distribution.Client.Security.HTTP
Distribution.Client.Setup
Expand Down
16 changes: 8 additions & 8 deletions cabal-install/src/Distribution/Client/CmdBuild.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import Distribution.Verbosity
( normal )
import Distribution.Simple.Utils
( wrapText, die' )
import Distribution.Client.ScriptUtils
( AcceptNoTargets(..), withContextAndSelectors, updateContextAndWriteProjectFile, TargetContext(..) )

import qualified Data.Map as Map

Expand Down Expand Up @@ -95,19 +97,19 @@ defaultBuildFlags = BuildFlags
-- "Distribution.Client.ProjectOrchestration"
--
buildAction :: NixStyleFlags BuildFlags -> [String] -> GlobalFlags -> IO ()
buildAction flags@NixStyleFlags { extraFlags = buildFlags, ..} targetStrings globalFlags = do
buildAction flags@NixStyleFlags { extraFlags = buildFlags, ..} targetStrings globalFlags
= withContextAndSelectors RejectNoTargets Nothing flags targetStrings globalFlags $ \targetCtx ctx targetSelectors -> do
-- TODO: This flags defaults business is ugly
let onlyConfigure = fromFlag (buildOnlyConfigure defaultBuildFlags
<> buildOnlyConfigure buildFlags)
targetAction
| onlyConfigure = TargetActionConfigure
| otherwise = TargetActionBuild

baseCtx <- establishProjectBaseContext verbosity cliConfig OtherCommand

targetSelectors <-
either (reportTargetSelectorProblems verbosity) return
=<< readTargetSelectors (localPackages baseCtx) Nothing targetStrings
baseCtx <- case targetCtx of
ProjectContext -> return ctx
GlobalContext -> return ctx
ScriptContext path exemeta -> updateContextAndWriteProjectFile ctx path exemeta

buildCtx <-
runProjectPreBuildPhase verbosity baseCtx $ \elaboratedPlan -> do
Expand Down Expand Up @@ -141,8 +143,6 @@ buildAction flags@NixStyleFlags { extraFlags = buildFlags, ..} targetStrings glo
runProjectPostBuildPhase verbosity baseCtx buildCtx buildOutcomes
where
verbosity = fromFlagOrDefault normal (configVerbosity configFlags)
cliConfig = commandLineFlagsToProjectConfig globalFlags flags
mempty -- ClientInstallFlags, not needed here

-- | This defines what a 'TargetSelector' means for the @bench@ command.
-- It selects the 'AvailableTarget's that the 'TargetSelector' refers to,
Expand Down
43 changes: 36 additions & 7 deletions cabal-install/src/Distribution/Client/CmdClean.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import Distribution.Client.DistDirLayout
( DistDirLayout(..), defaultDistDirLayout )
import Distribution.Client.ProjectConfig
( findProjectRoot )
import Distribution.Client.ScriptUtils
( getScriptCacheDirectoryRoot )
import Distribution.Client.Setup
( GlobalFlags )
import Distribution.ReadE ( succeedReadE )
Expand All @@ -22,9 +24,14 @@ import Distribution.Simple.Utils
import Distribution.Verbosity
( normal )

import Control.Monad
( forM, forM_, mapM )
import qualified Data.Set as Set
import System.Directory
( removeDirectoryRecursive, removeFile
, doesDirectoryExist, getDirectoryContents )
, doesDirectoryExist, doesFileExist
, getDirectoryContents, listDirectory
, canonicalizePath )
import System.FilePath
( (</>) )

Expand Down Expand Up @@ -80,16 +87,21 @@ cleanAction CleanFlags{..} extraArgs _ = do
mdistDirectory = flagToMaybe cleanDistDir
mprojectFile = flagToMaybe cleanProjectFile

unless (null extraArgs) $
die' verbosity $ "'clean' doesn't take any extra arguments: "
++ unwords extraArgs
-- TODO interpret extraArgs as targets and clean those targets only (issue #7506)
--
-- For now assume all files passed are the names of scripts
notScripts <- filterM (fmap not . doesFileExist) extraArgs
unless (null notScripts) $
die' verbosity $ "'clean' extra arguments should be script files: "
++ unwords notScripts

projectRoot <- either throwIO return =<< findProjectRoot Nothing mprojectFile

let distLayout = defaultDistDirLayout projectRoot mdistDirectory

if saveConfig
then do
-- Do not clean a project if just running a script in it's directory
when (null extraArgs || isJust mdistDirectory) $ do
if saveConfig then do
let buildRoot = distBuildRootDirectory distLayout

buildRootExists <- doesDirectoryExist buildRoot
Expand All @@ -103,7 +115,24 @@ cleanAction CleanFlags{..} extraArgs _ = do
info verbosity ("Deleting dist-newstyle (" ++ distRoot ++ ")")
handleDoesNotExist () $ removeDirectoryRecursive distRoot

removeEnvFiles (distProjectRootDirectory distLayout)
removeEnvFiles (distProjectRootDirectory distLayout)

-- Clean specified script build caches and orphaned caches.
-- There is currently no good way to specify to only clean orphaned caches.
-- It would be better as part of an explicit gc step (see issue #3333)
toClean <- Set.fromList <$> mapM canonicalizePath extraArgs
cacheDir <- getScriptCacheDirectoryRoot
existsCD <- doesDirectoryExist cacheDir
caches <- if existsCD then listDirectory cacheDir else return []
paths <- fmap concat . forM caches $ \cache -> do
let locFile = cacheDir </> cache </> "scriptlocation"
exists <- doesFileExist locFile
if exists then pure . (,) (cacheDir </> cache) <$> readFile locFile else return []
forM_ paths $ \(cache, script) -> do
exists <- doesFileExist script
when (not exists || script `Set.member` toClean) $ do
info verbosity ("Deleting cache (" ++ cache ++ ") for script (" ++ script ++ ")")
removeDirectoryRecursive cache

removeEnvFiles :: FilePath -> IO ()
removeEnvFiles dir =
Expand Down
Loading