Skip to content

Expose cabal.project.freeze parsing in the Cabal library #7224

Closed

Description

@Fuuzetsu

Currently it seems impossible to get at contents of cabal.project.freeze file without re-implementing a bunch of the Distribution.Client.Config that's only in the cabal-install binary and not available to be used.

Our specific use-case is that we generate stack.yaml file as a shim from our cabal files and the freeze file contains the set of flags that we should be using.

In general, it seems that parsing this file should be inside Cabal.

Activity

  1. phadej commented on Jan 5, 2021

    @phadej
    Collaborator

    Asking to add cabal.project.freeze parsing to Cabal library is the same as adding parsing support for stack.yaml there.

    In the current state of configuration code, splitting out the parser of cabal-install files (cabal.config, cabal.project), and having even some degree of compatibility is unfortunately impossible.

  2. Fuuzetsu commented on Jan 5, 2021

    @Fuuzetsu
    MemberAuthor

    Asking to add cabal.project.freeze parsing to Cabal library is the same as adding parsing support for stack.yaml there.

    I'm not sure how this would be. We already do the parsing in cabal-install using the Cabal package, it seems like some of the code should just migrate back into Cabal...

    In the current state of configuration code, splitting out the parser of cabal-install files (cabal.config, cabal.project), and having even some degree of compatibility is unfortunately impossible.

    We already parse .cabal files, isn't it exactly the same issue? Is Cabal only meant to parse the .cabal file and offer tools to parse other things in the cabal format, but nothing specific to cabal-install? Hence the stack comment. If yes, feel free to close. It is very unfortunate though. Maybe cabal-install should expose modules so we could just invoke an already written piece of code from there...


    In the meantime, if anyone is interested, I am parsing out the bits I'm interested in (package flags in constraints) with following code:

    parseCabalFlags :: Action CabalFlags
    parseCabalFlags = do
        need [cabalProjectFreezeFile]
        fields <- do
            freezeFileBs <- liftIO $ BS.readFile cabalProjectFreezeFile
            case Cabal.readFields freezeFileBs of
                Left e -> fail $ show e
                Right fields -> pure fields
        constraints <- case listToMaybe $ mapMaybe getConstraints fields of
            Nothing -> fail "No constraints section in cabal freeze file."
            Just constraints -> pure constraints
    
        let cabalFlags =
                let f m t = case Cabal.simpleParse (T.unpack t) of
                        Nothing -> m
                        Just (CabalPkgFlags pkg flags) ->
                            HashMap.insertWith (<>) pkg flags m
                in foldl' f mempty constraints
        pure $! CabalFlags cabalFlags
        where
            getConstraints (Cabal.Field (Cabal.Name _ n) ls)
                | n == fromString "constraints" = pure $ ls <&> \case
                    Cabal.FieldLine _ t -> T.decodeUtf8 t
            getConstraints _ = Nothing
    
    data CabalPkgFlags = CabalPkgFlags !T.Text !Cabal.FlagAssignment
    instance Cabal.Parsec CabalPkgFlags where
        parsec = do
            -- We don't check the package name: if there are no flags on this
            -- line, parsecFlagAssignment will fail or be empty and we won't
            -- include this anyway.
            pkg <- Cabal.munch1 (not . isSpace)
            () <- Cabal.skipSpaces1
            flags <- Cabal.parsecFlagAssignment
            guard . not $ Cabal.nullFlagAssignment flags
            -- Consume any trailing spaces and trailing comma if it's not the last
            -- item.
            Cabal.spaces <* Cabal.optional (Cabal.char ',')
            pure $! CabalPkgFlags (fromString pkg) flags
  3. phadej commented on May 3, 2021

    @phadej
    Collaborator

    Honestly, i don't think #7358 really closes this. Figuring out how to parse cabal.project(.freeze) using current cabal-install is beyond human. It's in theory possible, in practice it isn't.

  4. reopened this on May 3, 2021
  5. emilypi commented on May 3, 2021

    @emilypi
    Member

    Was this closed by linking? I didn't physically close this issue. Thanks for reopening @phadej

  6. fgaz commented on May 3, 2021

    @fgaz
    Member

    Yes, I thought I'd do good by linking the merge of that pr with the closure of the issues listed in "fixes", my bad

  7. emilypi commented on May 3, 2021

    @emilypi
    Member

    No worries. I went through and this is the only one that is partially fixed but not fully.

  8. andreabedini commented on Jan 12, 2023

    @andreabedini
    Collaborator

    This is now easy with the cabal-install library. We have readProjectConfig and even a readProjectLocalFreezeConfig

    readProjectConfig :: Verbosity -> HttpTransport -> Flag Bool -> Flag FilePath -> DistDirLayout -> Rebuild ProjectConfigSkeleton
    readProjectLocalFreezeConfig :: Verbosity -> HttpTransport -> DistDirLayout -> Rebuild ProjectConfigSkeleton 
    

    API could be improved but these functions do exactly what the OP asked.

  9. Fuuzetsu commented on Jan 13, 2023

    @Fuuzetsu
    MemberAuthor

    Fine with me. FWIW we no longer have need for this so feel free to close at own discretion.

  10. Mikolaj commented on Jan 13, 2023

    @Mikolaj
    Member

    Thank you, everybody. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions