Skip to content

Commit 5af5777

Browse files
committed
Fix known bugs in stack config set snapshot/resolver
1 parent cc1f6df commit 5af5777

File tree

3 files changed

+54
-36
lines changed

3 files changed

+54
-36
lines changed

ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Other enhancements:
2828
Bug fixes:
2929

3030
* `--haddock-for-hackage` does not ignore `--haddock-arguments`.
31+
* The `config set snapshot` and `config set resolver` commands now respect the
32+
presence of a synoymous key.
3133

3234
## v2.15.1 - 2024-02-09
3335

doc/config_command.md

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ Available commands:
4242
install-ghc Configure whether Stack should automatically install
4343
GHC when necessary.
4444
package-index Configure Stack's package index
45-
resolver Change the resolver key of the current project.
45+
resolver Change the snapshot of the current project, using the
46+
resolver key.
4647
snapshot Change the snapshot of the current project.
4748
system-ghc Configure whether Stack should use a system GHC
4849
installation or not.
@@ -82,16 +83,9 @@ file (`config.yaml`).
8283
stack config set resolver SNAPSHOT
8384
~~~
8485

85-
`stack config set resolver <snapshot>` sets the `resolver` key in the
86-
project-level configuration file (`stack.yaml`).
87-
88-
A snapshot of `lts` or `nightly` will be translated into the most recent
89-
available. A snapshot of `lts-22` will be translated into the most recent
90-
available in the `lts-22` sequence.
91-
92-
Known bug:
93-
94-
* The command does not respect the presence of a `snapshot` key.
86+
A command corresponding to the
87+
[`stack config set snapshot` command](#the-stack-config-set-snapshot-command)
88+
but using the `resolver` key instead of the `snapshot` key.
9589

9690
## The `stack config set snapshot` command
9791

@@ -102,15 +96,13 @@ stack config set snapshot SNAPSHOT
10296
~~~
10397

10498
`stack config set snapshot <snapshot>` sets the `snapshot` key in the
105-
project-level configuration file (`stack.yaml`).
99+
project-level configuration file (`stack.yaml`) to the specified snapshot.
106100

107101
A snapshot of `lts` or `nightly` will be translated into the most recent
108102
available. A snapshot of `lts-22` will be translated into the most recent
109103
available in the `lts-22` sequence.
110104

111-
Known bug:
112-
113-
* The command does not respect the presence of a `resolver` key.
105+
If a `resolver` key is present, it will be replaced by a `snapshot` key.
114106

115107
## The `stack config set system-ghc` command
116108

src/Stack/ConfigCmd.hs

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -109,22 +109,44 @@ cfgCmdSet cmd = do
109109
newValue' = T.stripEnd $
110110
decodeUtf8With lenientDecode $ Yaml.encode newValue -- Text
111111
file = toFilePath configFilePath -- String
112-
newYamlLines <- case inConfig config cmdKeys of
113-
Nothing -> do
112+
hits = catMaybes $ NE.toList $ NE.map (inConfig config) cmdKeys
113+
primaryCmdKey = NE.last $ NE.head cmdKeys
114+
newYamlLines <- case hits of
115+
[] -> do
114116
prettyInfoL
115117
[ pretty configFilePath
116118
, flow "has been extended."
117119
]
118-
pure $ writeLines yamlLines "" cmdKeys newValue'
119-
Just oldValue -> if oldValue == newValue
120+
pure $ writeLines yamlLines "" (NE.head cmdKeys) newValue'
121+
[(cmdKey, oldValue)] -> if oldValue == newValue && cmdKey == primaryCmdKey
120122
then do
121123
prettyInfoL
122124
[ pretty configFilePath
123125
, flow "already contained the intended configuration and remains \
124126
\unchanged."
125127
]
126128
pure yamlLines
127-
else switchLine configFilePath (NE.last cmdKeys) newValue' [] yamlLines
129+
else do
130+
when (cmdKey /= primaryCmdKey) $
131+
prettyWarn $
132+
fillSep
133+
[ pretty configFilePath
134+
, flow "contained a synonym for"
135+
, style Target (fromString $ T.unpack primaryCmdKey)
136+
, parens (style Current (fromString $ T.unpack cmdKey))
137+
, flow "which has been replaced."
138+
]
139+
<> line
140+
switchLine configFilePath cmdKey primaryCmdKey newValue' [] yamlLines
141+
_ -> do
142+
-- In practice, this warning should not be encountered because with
143+
-- snapshot and resolver present, Stack will not parse the YAML file.
144+
prettyWarnL
145+
[ pretty configFilePath
146+
, flow "contains more than one possible existing configuration and, \
147+
\consequently, remains unchanged."
148+
]
149+
pure yamlLines
128150
liftIO $ writeFileUtf8 file (T.unlines newYamlLines)
129151
where
130152
-- This assumes that if the key does not exist, the lines that can be
@@ -145,14 +167,15 @@ cfgCmdSet cmd = do
145167

146168
inConfig v cmdKeys = case v of
147169
Yaml.Object obj ->
148-
case KeyMap.lookup (Key.fromText (NE.head cmdKeys)) obj of
149-
Nothing -> Nothing
150-
Just v' -> case nonEmpty $ NE.tail cmdKeys of
151-
Nothing -> Just v'
152-
Just ks -> inConfig v' ks
170+
let cmdKey = NE.head cmdKeys
171+
in case KeyMap.lookup (Key.fromText cmdKey) obj of
172+
Nothing -> Nothing
173+
Just v' -> case nonEmpty $ NE.tail cmdKeys of
174+
Nothing -> Just (cmdKey, v')
175+
Just ks -> inConfig v' ks
153176
_ -> Nothing
154177

155-
switchLine file cmdKey _ searched [] = do
178+
switchLine file cmdKey _ _ searched [] = do
156179
prettyWarnL
157180
[ style Current (fromString $ T.unpack cmdKey)
158181
, flow "not found in YAML file"
@@ -161,11 +184,11 @@ cfgCmdSet cmd = do
161184
\supported."
162185
]
163186
pure $ reverse searched
164-
switchLine file cmdKey newValue searched (oldLine:rest) =
187+
switchLine file cmdKey cmdKey' newValue searched (oldLine:rest) =
165188
case parseOnly (parseLine cmdKey) oldLine of
166-
Left _ -> switchLine file cmdKey newValue (oldLine:searched) rest
189+
Left _ -> switchLine file cmdKey cmdKey' newValue (oldLine:searched) rest
167190
Right (kt, spaces1, spaces2, spaces3, comment) -> do
168-
let newLine = spaces1 <> renderKey cmdKey kt <> spaces2 <>
191+
let newLine = spaces1 <> renderKey cmdKey' kt <> spaces2 <>
169192
":" <> spaces3 <> newValue <> comment
170193
prettyInfoL
171194
[ pretty file
@@ -246,13 +269,13 @@ snapshotValue root snapshot = do
246269
void $ loadSnapshot =<< completeSnapshotLocation concreteSnapshot
247270
pure (Yaml.toJSON concreteSnapshot)
248271

249-
cfgCmdSetKeys :: ConfigCmdSet -> NonEmpty Text
250-
cfgCmdSetKeys (ConfigCmdSetSnapshot _) = ["snapshot"]
251-
cfgCmdSetKeys (ConfigCmdSetResolver _) = ["resolver"]
252-
cfgCmdSetKeys (ConfigCmdSetSystemGhc _ _) = [configMonoidSystemGHCName]
253-
cfgCmdSetKeys (ConfigCmdSetInstallGhc _ _) = [configMonoidInstallGHCName]
272+
cfgCmdSetKeys :: ConfigCmdSet -> NonEmpty (NonEmpty Text)
273+
cfgCmdSetKeys (ConfigCmdSetSnapshot _) = [["snapshot"], ["resolver"]]
274+
cfgCmdSetKeys (ConfigCmdSetResolver _) = [["resolver"], ["snapshot"]]
275+
cfgCmdSetKeys (ConfigCmdSetSystemGhc _ _) = [[configMonoidSystemGHCName]]
276+
cfgCmdSetKeys (ConfigCmdSetInstallGhc _ _) = [[configMonoidInstallGHCName]]
254277
cfgCmdSetKeys (ConfigCmdSetDownloadPrefix _ _) =
255-
["package-index", "download-prefix"]
278+
[["package-index", "download-prefix"]]
256279

257280
cfgCmdName :: String
258281
cfgCmdName = "config"
@@ -284,7 +307,8 @@ configCmdSetParser =
284307
( OA.metavar "SNAPSHOT"
285308
<> OA.help "E.g. \"nightly\" or \"lts-22.8\"" ))
286309
( OA.progDesc
287-
"Change the resolver key of the current project." ))
310+
"Change the snapshot of the current project, using the \
311+
\resolver key." ))
288312
, OA.command (T.unpack configMonoidSystemGHCName)
289313
( OA.info
290314
( ConfigCmdSetSystemGhc

0 commit comments

Comments
 (0)