Skip to content

Commit d127ba5

Browse files
committed
WIP optparse
Change-Id: Iea0d9564cac03be98281b3eb2c79409cd88dcea2
1 parent 6804c5a commit d127ba5

13 files changed

Lines changed: 220 additions & 175 deletions

File tree

buck-proxy/lib/BuckProxy/Orchestration.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import System.Directory (createDirectoryIfMissing)
3737
import System.Exit (exitFailure)
3838
import System.Process (ProcessHandle, getProcessExitCode, spawnProcess)
3939
import Types.Args (TargetId)
40-
import Types.BuckArgs (BuckArgs (workerTargetId), parseBuckArgs)
40+
import Types.BuckArgs (BuckArgs (workerTargetId), parseBuckArgsCli)
4141
import Types.Grpc (CommandEnv (..), RequestArgs (..))
4242
import Types.Orchestration (
4343
PrimarySocketName (..),
@@ -87,7 +87,7 @@ proxyHandler workerMap command socketDefault socketOverride req = do
8787
-- from the gRPC socket path if the key is absent from the env.
8888
-- If an override was specified on the command line with @--socket-name@, it has precedence over both.
8989
socketId = fromMaybe socketDefault (socketOverride <|> coerce (cmdEnv.values !? "BUCK_BUILD_ID"))
90-
buckArgs <- either (throwIO . userError) pure (parseBuckArgs cmdEnv (RequestArgs argv))
90+
buckArgs <- either (throwIO . userError) pure (parseBuckArgsCli cmdEnv (RequestArgs argv))
9191
case buckArgs.workerTargetId of
9292
Nothing -> throwIO (userError "No --worker-target-id passed")
9393
Just targetId -> do

ghc-proxy/app/ghc-proxy/Main.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import System.Environment (getArgs, lookupEnv)
1111
import System.Exit (exitFailure)
1212
import System.IO (BufferMode (..), hPutStrLn, hSetBuffering, stderr, stdout)
1313
import Types.Args (Args (..))
14-
import Types.BuckArgs (parseBuckArgs, toGhcArgs)
14+
import Types.BuckArgs (parseBuckArgsCli, toGhcArgs)
1515
import Types.Env (Env (..))
1616
import Types.Grpc (CommandEnv (..), RequestArgs (..))
1717
import Types.Log (Log, Logger (..), TraceId (..), newLog)
1818

1919
envFromArgs :: [String] -> IO (Env, MVar Log)
2020
envFromArgs argv = do
21-
buckArgs <- either parseError pure (parseBuckArgs (CommandEnv []) (RequestArgs argv))
21+
buckArgs <- either parseError pure (parseBuckArgsCli (CommandEnv []) (RequestArgs argv))
2222
args <- toGhcArgs buckArgs Nothing
2323
actionMetadata <- lookupEnv "ACTION_METADATA"
2424
let args' = args {actionMetadata}

ghc-server/lib/GhcClient/Run.hs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@ import System.IO (BufferMode (..), hPutStrLn, hSetBuffering, stderr, stdout)
4040
import System.OsPath (encodeUtf)
4141

4242
-- | CLI argument parser for the client.
43+
--
44+
-- Parses only client-local options (@--wait@) and the project root.
45+
-- All remaining arguments (after @--@) are passed verbatim to the server.
4346
clientConfigParser :: Parser ClientConfig
4447
clientConfigParser = do
4548
projectRoot <- argument readOsPath (metavar "PROJECT_ROOT" <> help "Path to the project root directory")
4649
wait <- switch (long "wait" <> short 'w' <> help "Wait for the build to complete before returning")
47-
recompile <- switch (long "recompile" <> help "Recompile modules even when cached artifacts exist")
48-
rebuild <- switch (long "rebuild" <> help "Recompute metadata and recompile even when cached")
49-
targets <- many (strArgument (metavar "TARGETS..." <> help "Schedule targets (e.g. unit1 unit2:metadata unit2:Module)"))
50-
pure ClientConfig {..}
50+
targets <- many (strArgument (metavar "ARGS..." <> help "Arguments passed verbatim to the server (use -- to pass flags)"))
51+
pure ClientConfig {projectRoot, wait, targets}
5152
where
5253
readOsPath =
5354
eitherReader (first show <$> encodeUtf)
@@ -78,8 +79,6 @@ client config = do
7879

7980
flagArgs =
8081
["--wait" | config.wait]
81-
++ ["--recompile" | config.recompile]
82-
++ ["--rebuild" | config.rebuild]
8382

8483
-- | Parse CLI args and run the client command.
8584
runClient :: IO ()

ghc-server/lib/GhcServer/Data/Config.hs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,9 @@ data ClientConfig =
2525
ClientConfig {
2626
-- | Absolute path to the project root directory.
2727
projectRoot :: OsPath,
28-
-- | Raw schedule arguments to send.
28+
-- | Raw arguments to send to the server, including schedule targets and flags like @--recompile@/@--rebuild@.
2929
targets :: [String],
3030
-- | Whether to wait for the build to complete before returning.
31-
wait :: Bool,
32-
-- | Force recompilation of modules even when cached artifacts exist.
33-
recompile :: Bool,
34-
-- | Recompute metadata (and recompile) even when cached.
35-
rebuild :: Bool
31+
wait :: Bool
3632
}
3733
deriving stock (Show)

ghc-worker/ghc-worker.cabal

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,16 @@ executable ghc-worker
8686
else
8787
ghc-options: -O2 -threaded "-with-rtsopts=-K512M -I5 -A128M -T -N"
8888
build-depends:
89-
ghc-worker
89+
ghc-worker,
90+
optparse-applicative
9091

9192
test-suite ghc-worker-test
9293
import: all
9394
type: exitcode-stdio-1.0
9495
main-is: Main.hs
9596
hs-source-dirs: test
9697
other-modules:
98+
BuckArgsTest,
9799
BuildPlanTest,
98100
FlagParserTest,
99101
IncrementalMetadataTest,

ghc-worker/lib/GhcWorker/GhcHandler.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import Internal.Session (withGhcMakeModule, withGhcMakeSource)
2828
import Prelude hiding (log)
2929
import Types.Args (Args (..))
3030
import qualified Types.BuckArgs
31-
import Types.BuckArgs (BuckArgs, IsInterpreted (..), Mode (..), parseBuckArgs, toGhcArgs)
31+
import Types.BuckArgs (BuckArgs, IsInterpreted (..), Mode (..), parseBuckArgsCli, toGhcArgs)
3232
import Types.Env (Env (..))
3333
import Types.FeatureFlags (FeatureFlags (..))
3434
import Types.Grpc (RequestArgs (..))
@@ -151,7 +151,7 @@ ghcHandler state featureFlags instrument traceId =
151151
InstrumentedHandler \ hooks -> GrpcHandler \ commandEnv argv -> do
152152
log <- newLogger <$> newLog traceId
153153
result <- try do
154-
buckArgs <- either parseError pure (parseBuckArgs commandEnv argv)
154+
buckArgs <- either parseError pure (parseBuckArgsCli commandEnv argv)
155155
args <- toGhcArgs buckArgs (Just featureFlags)
156156
log.debug (unlines (coerce argv))
157157
let env = Env {log, state, args = args}

ghc-worker/test/BuckArgsTest.hs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
module BuckArgsTest where
2+
3+
import Data.List.NonEmpty (NonEmpty (..))
4+
import Data.Map.Strict qualified as Map
5+
import Hedgehog (TestT, evalEither, (===))
6+
import Test.Run (assertJust, unitTest)
7+
import Test.Tasty (TestTree, testGroup)
8+
import Types.Args (TargetId (..))
9+
import Types.BuckArgs (BuckArgs (..), Mode (..), parseBuckArgsCli)
10+
import Types.Grpc (CommandEnv (..), RequestArgs (..))
11+
12+
-- | Typical metadata command sent by Buck.
13+
metadataCommand :: [String]
14+
metadataCommand =
15+
[
16+
"-M",
17+
"--ghc-dir", "buck/ghc_dir",
18+
"--worker-target-id", "singleton",
19+
"--build-plan", "buck/lib1.depends.json",
20+
"--fields", "exposed_modules,module_graph,package_deps,th_modules,cache",
21+
"--dep-units", "buck/lib1.json",
22+
"--unit", "lib1",
23+
"--ghc-args", "buck/lib1.args"
24+
]
25+
26+
-- | Compile-mode command with GHC passthrough flags.
27+
compileCommand :: [String]
28+
compileCommand =
29+
[
30+
"-c",
31+
"--unit", "lib2",
32+
"--module", "Lib.Module",
33+
"-O2",
34+
"-package-db", "buck/db"
35+
]
36+
37+
runParser :: [String] -> Either String BuckArgs
38+
runParser cmd =
39+
parseBuckArgsCli (CommandEnv Map.empty) (RequestArgs cmd)
40+
41+
test_parseMetadata :: TestT IO ()
42+
test_parseMetadata = do
43+
BuckArgs {..}
44+
<- evalEither (runParser metadataCommand)
45+
assertJust ModeMetadata mode
46+
assertJust "lib1" unit
47+
assertJust (TargetId "singleton") workerTargetId
48+
assertJust "buck/ghc_dir" ghcDirFile
49+
assertJust "buck/lib1.depends.json" buildPlan
50+
assertJust ("exposed_modules" :| ["module_graph", "package_deps", "th_modules", "cache"]) fields
51+
assertJust "buck/lib1.json" depUnits
52+
assertJust "buck/lib1.args" ghcArgsFile
53+
[] === ghcOptions
54+
55+
test_parseCompile :: TestT IO ()
56+
test_parseCompile = do
57+
BuckArgs {mode, unit, moduleName, ghcOptions} <- evalEither (runParser compileCommand)
58+
assertJust ModeCompile mode
59+
assertJust "lib2" unit
60+
assertJust "Lib.Module" moduleName
61+
["-O2", "-package-db", "buck/db"] === ghcOptions
62+
63+
test_parseBuckArgs :: TestTree
64+
test_parseBuckArgs =
65+
testGroup "parseBuckArgsCli" [
66+
unitTest "metadata command" test_parseMetadata,
67+
unitTest "compile command" test_parseCompile
68+
]

ghc-worker/test/Main.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{-# LANGUAGE CPP #-}
22

33
module Main where
4-
4+
5+
import BuckArgsTest (test_parseBuckArgs)
56
import BuildPlanTest (test_buildPlan)
67
import IncrementalMetadataTest (test_incremental)
78
import ProfileTest (test_profiling)
@@ -36,6 +37,7 @@ fullTest = False
3637
testsGeneral :: [TestTree]
3738
testsGeneral =
3839
[
40+
test_parseBuckArgs,
3941
test_sortScheduleOrder,
4042
test_projectBuild,
4143
test_incremental

internal/src/Internal/Cache/Metadata.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import Internal.State (updateMakeState)
3434
import qualified Internal.State.Make as Make
3535
import Internal.State.Make (insertUnitEnv, storeModuleGraph)
3636
import Internal.UnitEnv (emptyHomePackageTable)
37-
import Types.BuckArgs (CachedBuckArgs (..), parseCachedBuckArgs)
37+
import Types.BuckArgs (CachedBuckArgs (..), parseCachedBuckArgsCli)
3838
import Types.CachedDeps (
3939
CachedBuildPlan (..),
4040
CachedBuildPlans (..),
@@ -205,7 +205,7 @@ loadCachedArgs ::
205205
StateT WorkerState IO ()
206206
loadCachedArgs path = do
207207
cachedArgs <- liftIO $ readFile path
208-
case parseCachedBuckArgs (lines cachedArgs) of
208+
case parseCachedBuckArgsCli (lines cachedArgs) of
209209
Right args -> modifyM (setupPath args.cachedBinPath)
210210
Left err -> liftIO $ throwIO (userError err)
211211

ops/packages.nix

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,15 @@
112112
"containers"
113113
"directory"
114114
"grapesy"
115+
"optparse-applicative"
115116
"process"
116117
"text"
117118
];
118119
};
119120
executables.buck-proxy = {
120121
dependencies = [
121122
"buck-worker-types"
123+
"optparse-applicative"
122124
"unix"
123125
];
124126
ghc-options-exe = [
@@ -222,6 +224,7 @@
222224
"filepath"
223225
"ghc"
224226
"ghc-paths"
227+
"optparse-applicative"
225228
"split"
226229
"text"
227230
];
@@ -329,9 +332,6 @@
329332

330333
executables.gen-project = {
331334
source-dirs = "app/gen-project";
332-
dependencies = [
333-
"directory"
334-
];
335335
};
336336

337337
tests.ghc-server-test = {

0 commit comments

Comments
 (0)