From 4019d172e2a5984b33907ff6a6ea436280be71bd Mon Sep 17 00:00:00 2001
From: Matthew Pickering <matthewtpickering@gmail.com>
Date: Tue, 27 Aug 2024 16:41:45 +0100
Subject: [PATCH] Only munge internal dependencies when printing when there is
 no explicit syntax

* In `postProcessInternalDeps` the shadowing logic which existed prior
  to cabal format 3.4 is implemented in a post processing step.

  The algorithm there replaces any references to internal sublibraries
  with an explicit qualifier. For example if you write..

  ```
  library
    build-depends: foo

  library foo
    ...
  ```

  this is reinterpreted as

  ```
  library
    build-depends: mylib:foo

  library foo
    ...
  ```

* In `preProcessInternalDeps` the inverse transformation takes place,
  the goal is to replace `mylib:foo` with just `foo`.

* Things go wrong if you are using version 3.0 for your cabal file
  because
  - In 3.0 the qualifier syntax is introduced so you can be expliciit
    about sublibrary dependencies
  - The shadowing semantics of non-qualified dependencies still exists.

  So the situation is that the user is explicit about the sublibrary

  ```
  library

  library qux
    build-depends: mylib:{mylib, foo}

  library foo
  ```

  1. Post-process leaves this alone, the user is already explicit about
     depending on a sublibrary.
  2. Pre-processing then rewrites `mylib:{mylib, foo}` into two
     dependencies, `mylib` and `foo` (no qualifier).
  3. When parsed these are two separate dependencies rather than treated
     as one dependency, roundtrip test fails.

Solution: Only perform the reverse transformation when the cabal library
version is <= 3.0 and doesn't support the explicit syntax.

Now what happens in these two situations:

1. ```
   library
     build-depends: foo

   library foo
     ...
   ```

  this is reinterpreted as

  ```
  library
    build-depends: mylib:foo

  library foo
    ...
  ```

  then printed and parsed exactly the same way.

2. Explicit syntax is parsed and printed without being munged (when
   supported)

Note: Mixins only supported sublibrary qualifiers from 3.4 whilst
dependencies supported this from 3.0, hence the lack of guard on the
mixins case.

Fixes #10283
---
 .../src/Distribution/PackageDescription/PrettyPrint.hs     | 7 +++++--
 .../tests/ParserTests/regressions/issue-6083-b.format      | 6 +++---
 cabal-validate/src/Main.hs                                 | 2 +-
 3 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/Cabal-syntax/src/Distribution/PackageDescription/PrettyPrint.hs b/Cabal-syntax/src/Distribution/PackageDescription/PrettyPrint.hs
index b03b1b99ad..15c2c15fe0 100644
--- a/Cabal-syntax/src/Distribution/PackageDescription/PrettyPrint.hs
+++ b/Cabal-syntax/src/Distribution/PackageDescription/PrettyPrint.hs
@@ -271,7 +271,7 @@ preProcessInternalDeps specVer gpd
 
     transformD :: Dependency -> [Dependency]
     transformD (Dependency pn vr ln)
-      | pn == thisPn =
+      | pn == thisPn && specVer < CabalSpecV3_0 =
           if LMainLibName `NES.member` ln
             then Dependency thisPn vr mainLibSet : sublibs
             else sublibs
@@ -282,9 +282,12 @@ preProcessInternalDeps specVer gpd
           ]
     transformD d = [d]
 
+    -- Always perform transformation for mixins as syntax was only introduced in 3.4
+    -- This guard is uncessary as no transformations take place when cabalSpec >= CabalSpecV3_4 but
+    -- it more clearly signifies the intent. (See the specVer >= CabalSpecV3_4 line above).
     transformM :: Mixin -> Mixin
     transformM (Mixin pn (LSubLibName uqn) inc)
-      | pn == thisPn =
+      | pn == thisPn && specVer < CabalSpecV3_4 =
           mkMixin (unqualComponentNameToPackageName uqn) LMainLibName inc
     transformM m = m
 
diff --git a/Cabal-tests/tests/ParserTests/regressions/issue-6083-b.format b/Cabal-tests/tests/ParserTests/regressions/issue-6083-b.format
index d209d572be..255c71de99 100644
--- a/Cabal-tests/tests/ParserTests/regressions/issue-6083-b.format
+++ b/Cabal-tests/tests/ParserTests/regressions/issue-6083-b.format
@@ -6,7 +6,7 @@ library
     default-language: Haskell2010
     build-depends:
         base,
-        sublib
+        issue:sublib
 
 library sublib
     default-language: Haskell2010
@@ -15,10 +15,10 @@ executable demo-a
     main-is:       Main.hs
     build-depends:
         issue,
-        sublib
+        issue:sublib
 
 executable demo-b
     main-is:       Main.hs
     build-depends:
         issue,
-        sublib
+        issue:sublib
diff --git a/cabal-validate/src/Main.hs b/cabal-validate/src/Main.hs
index 7164f3f8cc..bf5e30a26c 100644
--- a/cabal-validate/src/Main.hs
+++ b/cabal-validate/src/Main.hs
@@ -290,7 +290,7 @@ runHackageTests opts
 
       let
         -- See #10284 for why this value is pinned.
-        hackageTestsIndexState = "--index-state=2024-08-25"
+        hackageTestsIndexState = "--index-state=2025-01-12"
 
         hackageTest args =
           timedWithCwd
-- 
GitLab