Skip to content

Commit

Permalink
Fix #5467 Report error if package identifier does not parse
Browse files Browse the repository at this point in the history
  • Loading branch information
mpilgrem committed Mar 30, 2024
1 parent 967e86c commit d7fccc5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
5 changes: 5 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ Behaviour changes:
* The `haddock --haddock-for-hackage` command only seeks to create an archive of
the `<package_version>-docs` directory for build targets and if flags
excluding the building of project packages are not set.
* If a build target is a package identifier, and the package version is not in
the snapshot or the package index, Stack will report an error when the target
is parsed. Previously, if another version of the package was in the snapshot,
Stack would construct the build plan with that other version or, if it was
not, Stack would defer an error to the construction of the build plan.

Other enhancements:

Expand Down
10 changes: 4 additions & 6 deletions doc/build_command.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,13 @@ Example: `stack build foobar-1.2.3`
If the package name is that of a project package, then Stack fails with an
error.

If the package version is an extra-dep or in the snapshot, then Stack will use
that version.

If the package version is in the package index (e.g. Hackage) then Stack will
use the latest revision of that version from the package index.

If the package is an extra-dep or in the snapshot, Stack will behave as if only
the package name had been specified as the target (that is, ignoring the
specified version).

Otherwise, Stack will fail with an error, reporting that the package name is
unknown.
Otherwise, Stack will fail with an error.

### Target: project package *component*

Expand Down
44 changes: 30 additions & 14 deletions src/Stack/Build/Target.hs
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,13 @@ resolveRawTarget sma allLocs (rawInput, rt) =
go (RTPackageIdentifier ident@(PackageIdentifier name version))
| Map.member name locals = pure $ Left $
fillSep
[ style Target (fromPackageName name)
, flow "target has a specific version number, but it is a project \
\package. To avoid confusion, we will not install the \
\specified version or build the project package. To build the \
\project package, specify the target without an explicit \
\version."
[ style Target (fromPackageId ident)
, flow "is a specific package version, but"
, style Target (fromPackageName name)
, flow "is the name of a project package. To avoid confusion, Stack \
\will not try to build the specified version or the project \
\package. To build the project package, specify only"
, style Current (fromPackageName name) <> "."
]
| otherwise =
case Map.lookup name allLocs of
Expand All @@ -412,15 +413,13 @@ resolveRawTarget sma allLocs (rawInput, rt) =
) ->
if version == versionLoc
then pure $ deferToConstructPlan name
else hackageLatestRevision name version
else hackageLatestRevision name version versionLoc
-- The package was coming from something besides the index, so refuse
-- to do the override
Just loc' -> pure $ Left $
fillSep
[ flow "Package with identifier was targeted on the command \
\line:"
, style Target (fromPackageId ident) <> ","
, flow "but it was specified from a non-index location:"
[ style Target (fromPackageId ident)
, flow "was specified from a non-index location, namely:"
, flow $ T.unpack $ textDisplay loc' <> "."
, flow "Recommendation: add the correctly desired version to \
\extra-deps."
Expand All @@ -429,7 +428,14 @@ resolveRawTarget sma allLocs (rawInput, rt) =
Nothing -> do
mrev <- getLatestHackageRevision YesRequireHackageIndex name version
pure $ case mrev of
Nothing -> deferToConstructPlan name
Nothing -> Left $
fillSep
[ flow "Stack did not know the location of a package named"
, style Target (fromPackageName name)
, "and could not find"
, style Target (fromPackageId ident)
, flow "in the package index."
]
Just (_rev, cfKey, treeKey) -> Right ResolveResult
{ name
, rawInput
Expand All @@ -453,10 +459,20 @@ resolveRawTarget sma allLocs (rawInput, rt) =
, packageType = PTDependency
}

hackageLatestRevision name version = do
hackageLatestRevision name version versionLoc = do
mrev <- getLatestHackageRevision YesRequireHackageIndex name version
pure $ case mrev of
Nothing -> deferToConstructPlan name
Nothing -> Left $
fillSep
[ flow "Stack knows the location of"
, style Current (fromPackageId pkgId')
, flow "but did not know the location of"
, style Target (fromPackageId pkgId) <>","
, flow "and did not find it in the package index."
]
where
pkgId = PackageIdentifier name version
pkgId' = PackageIdentifier name versionLoc
Just (_rev, cfKey, treeKey) -> Right ResolveResult
{ name
, rawInput
Expand Down

0 comments on commit d7fccc5

Please sign in to comment.