-
Notifications
You must be signed in to change notification settings - Fork 696
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
Finish off external commands feature #9412
Merged
mergify
merged 2 commits into
haskell:master
from
mpickering:wip/external-commands-fixes
Nov 24, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -205,7 +205,8 @@ import Distribution.Simple.Command | |
, commandAddAction | ||
, commandFromSpec | ||
, commandShowOptions | ||
, commandsRun | ||
, commandsRunWithFallback | ||
, defaultCommandFallback | ||
, hiddenCommand | ||
) | ||
import Distribution.Simple.Compiler (PackageDBStack) | ||
|
@@ -221,6 +222,8 @@ import Distribution.Simple.PackageDescription (readGenericPackageDescription) | |
import Distribution.Simple.Program | ||
( configureAllKnownPrograms | ||
, defaultProgramDb | ||
, defaultProgramSearchPath | ||
, findProgramOnSearchPath | ||
, getProgramInvocationOutput | ||
, simpleProgramInvocation | ||
) | ||
|
@@ -261,7 +264,7 @@ import System.Directory | |
, getCurrentDirectory | ||
, withCurrentDirectory | ||
) | ||
import System.Environment (getProgName) | ||
import System.Environment (getEnvironment, getExecutablePath, getProgName) | ||
import System.FilePath | ||
( dropExtension | ||
, splitExtension | ||
|
@@ -276,6 +279,7 @@ import System.IO | |
, stderr | ||
, stdout | ||
) | ||
import System.Process (createProcess, env, proc) | ||
|
||
-- | Entry point | ||
-- | ||
|
@@ -334,9 +338,8 @@ warnIfAssertionsAreEnabled = | |
mainWorker :: [String] -> IO () | ||
mainWorker args = do | ||
topHandler $ do | ||
command <- commandsRun (globalCommand commands) commands args | ||
command <- commandsRunWithFallback (globalCommand commands) commands delegateToExternal args | ||
case command of | ||
CommandDelegate -> pure () | ||
CommandHelp help -> printGlobalHelp help | ||
CommandList opts -> printOptionsList opts | ||
CommandErrors errs -> printErrors errs | ||
|
@@ -347,7 +350,6 @@ mainWorker args = do | |
printVersion | ||
| fromFlagOrDefault False (globalNumericVersion globalFlags) -> | ||
printNumericVersion | ||
CommandDelegate -> pure () | ||
CommandHelp help -> printCommandHelp help | ||
CommandList opts -> printOptionsList opts | ||
CommandErrors errs -> do | ||
|
@@ -366,6 +368,27 @@ mainWorker args = do | |
warnIfAssertionsAreEnabled | ||
action globalFlags | ||
where | ||
delegateToExternal | ||
:: [Command Action] | ||
-> String | ||
-> [String] | ||
-> IO (CommandParse Action) | ||
delegateToExternal commands' name cmdArgs = do | ||
mCommand <- findProgramOnSearchPath normal defaultProgramSearchPath ("cabal-" <> name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought this would fix the test failure on windows but it still seems to persist. |
||
case mCommand of | ||
Just (exec, _) -> return (CommandReadyToGo $ \_ -> callExternal exec name cmdArgs) | ||
Nothing -> defaultCommandFallback commands' name cmdArgs | ||
|
||
callExternal :: String -> String -> [String] -> IO () | ||
callExternal exec name cmdArgs = do | ||
cur_env <- getEnvironment | ||
cabal_exe <- getExecutablePath | ||
let new_env = ("CABAL", cabal_exe) : cur_env | ||
result <- try $ createProcess ((proc exec (name : cmdArgs)){env = Just new_env}) | ||
case result of | ||
Left ex -> printErrors ["Error executing external command: " ++ show (ex :: SomeException)] | ||
Right _ -> return () | ||
|
||
printCommandHelp help = do | ||
pname <- getProgName | ||
putStr (help pname) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# cabal v2-build | ||
Resolving dependencies... | ||
Build profile: -w ghc-<GHCVER> -O1 | ||
In order, the following will be built: | ||
- setup-test-0.1.0.0 (exe:cabal-aaaa) (first run) | ||
Configuring executable 'cabal-aaaa' for setup-test-0.1.0.0... | ||
Preprocessing executable 'cabal-aaaa' for setup-test-0.1.0.0... | ||
Building executable 'cabal-aaaa' for setup-test-0.1.0.0... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
packages: setup-test/ |
47 changes: 47 additions & 0 deletions
47
cabal-testsuite/PackageTests/ExternalCommand/cabal.test.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import Test.Cabal.Prelude | ||
import qualified System.Process as Process | ||
import Control.Concurrent (threadDelay) | ||
import System.Directory (removeFile) | ||
import Control.Exception (catch, throwIO) | ||
import System.IO.Error (isDoesNotExistError) | ||
import qualified Data.Time.Clock as Time | ||
import qualified Data.Time.Format as Time | ||
import Data.Maybe | ||
import System.Environment | ||
import System.FilePath | ||
|
||
main = do | ||
cabalTest $ do | ||
res <- cabalWithStdin "v2-build" ["all"] "" | ||
exe_path <- withPlan $ planExePath "setup-test" "cabal-aaaa" | ||
addToPath (takeDirectory exe_path) $ do | ||
-- Test that the thing works at all | ||
res <- cabal_raw_action ["aaaa"] (\h -> () <$ Process.waitForProcess h) | ||
assertOutputContains "aaaa" res | ||
|
||
-- Test that the extra arguments are passed on | ||
res <- cabal_raw_action ["aaaa", "--foobaz"] (\h -> () <$ Process.waitForProcess h) | ||
assertOutputContains "--foobaz" res | ||
|
||
-- Test what happens with "global" flags | ||
res <- cabal_raw_action ["aaaa", "--version"] (\h -> () <$ Process.waitForProcess h) | ||
assertOutputContains "--version" res | ||
|
||
-- Test what happens with "global" flags | ||
res <- cabal_raw_action ["aaaa", "--config-file", "abc"] (\h -> () <$ Process.waitForProcess h) | ||
assertOutputContains "--config-file" res | ||
|
||
|
||
cabal_raw_action :: [String] -> (Process.ProcessHandle -> IO ()) -> TestM Result | ||
cabal_raw_action args action = do | ||
configured_prog <- requireProgramM cabalProgram | ||
env <- getTestEnv | ||
r <- liftIO $ runAction (testVerbosity env) | ||
(Just (testCurrentDir env)) | ||
(testEnvironment env) | ||
(programPath configured_prog) | ||
args | ||
Nothing | ||
action | ||
recordLog r | ||
requireSuccess r |
5 changes: 5 additions & 0 deletions
5
cabal-testsuite/PackageTests/ExternalCommand/setup-test/AAAA.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Main where | ||
|
||
import System.Environment | ||
|
||
main = getArgs >>= print |
5 changes: 5 additions & 0 deletions
5
cabal-testsuite/PackageTests/ExternalCommand/setup-test/CHANGELOG.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Revision history for setup-test | ||
|
||
## 0.1.0.0 -- YYYY-mm-dd | ||
|
||
* First version. Released on an unsuspecting world. |
30 changes: 30 additions & 0 deletions
30
cabal-testsuite/PackageTests/ExternalCommand/setup-test/LICENSE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
Copyright (c) 2023, Matthew Pickering | ||
|
||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
|
||
* Redistributions in binary form must reproduce the above | ||
copyright notice, this list of conditions and the following | ||
disclaimer in the documentation and/or other materials provided | ||
with the distribution. | ||
|
||
* Neither the name of Matthew Pickering nor the names of other | ||
contributors may be used to endorse or promote products derived | ||
from this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
commandRun
is parameterised by a continuation which is used to handle the fallback case in different ways forCabal
andcabal-install
.