The -package flag should select match from right-most package db.
authorEdward Z. Yang <ezyang@cs.stanford.edu>
Sun, 27 Dec 2015 22:57:18 +0000 (23:57 +0100)
committerBen Gamari <ben@smart-cactus.org>
Sun, 27 Dec 2015 22:57:19 +0000 (23:57 +0100)
The shadowing and default behavior (in the absence of
-hide-all-packages) prefers packages that come from "later" package
databases.  So for example if tmp1.d and tmp2.d both expose p-1.0, then

    ghc -package-db tmp1.d -package-db tmp2.d

brings the p-1.0 from tmp2.d into scope (and if they have the same IPID,
tmp2.d shadows tmp1.d).  HOWEVER, -package flags do NOT respect this
behavior.

    ghc -package-db tmp1.d -package-db tmp2.d -package p-1.0

this will force the p-1.0 from tmp1.d to be exposed!  This is
confusing, so this patch makes the behavior of -package flags
consistent.

Signed-off-by: Edward Z. Yang <ezyang@cs.stanford.edu>
Test Plan: validate

Reviewers: austin, bgamari

Reviewed By: bgamari

Subscribers: thomie

Differential Revision: https://phabricator.haskell.org/D1709

17 files changed:
compiler/main/Packages.hs
testsuite/.gitignore
testsuite/tests/cabal/cabal08/Main.hs [new file with mode: 0644]
testsuite/tests/cabal/cabal08/Makefile [new file with mode: 0644]
testsuite/tests/cabal/cabal08/Setup.hs [new file with mode: 0644]
testsuite/tests/cabal/cabal08/all.T [new file with mode: 0644]
testsuite/tests/cabal/cabal08/cabal08.stdout [new file with mode: 0644]
testsuite/tests/cabal/cabal08/p1/ChangeLog.md [new file with mode: 0644]
testsuite/tests/cabal/cabal08/p1/LICENSE [new file with mode: 0644]
testsuite/tests/cabal/cabal08/p1/P.hs [new file with mode: 0644]
testsuite/tests/cabal/cabal08/p1/Setup.hs [new file with mode: 0644]
testsuite/tests/cabal/cabal08/p1/p.cabal [new file with mode: 0644]
testsuite/tests/cabal/cabal08/p2/ChangeLog.md [new file with mode: 0644]
testsuite/tests/cabal/cabal08/p2/LICENSE [new file with mode: 0644]
testsuite/tests/cabal/cabal08/p2/P.hs [new file with mode: 0644]
testsuite/tests/cabal/cabal08/p2/Setup.hs [new file with mode: 0644]
testsuite/tests/cabal/cabal08/p2/p.cabal [new file with mode: 0644]

index 2849b7e..f7bff71 100644 (file)
@@ -593,7 +593,9 @@ selectPackages matches pkgs unusable
   = let (ps,rest) = partition matches pkgs
     in if null ps
         then Left (filter (matches.fst) (Map.elems unusable))
-        else Right (sortByVersion ps, rest)
+        -- NB: packages from later package databases are LATER
+        -- in the list.  We want to prefer the latest package.
+        else Right (sortByVersion (reverse ps), rest)
 
 -- A package named on the command line can either include the
 -- version, or just the name if it is unambiguous.
index 6b94359..e8e4114 100644 (file)
@@ -121,6 +121,9 @@ mk/ghcconfig*_test___spaces_ghc*.exe.mk
 /tests/cabal/cabal05/r-0.1.0.0/
 /tests/cabal/cabal06/inst-*/
 /tests/cabal/cabal06/tmp*
+/tests/cabal/cabal08/Main
+/tests/cabal/cabal08/inst-*
+/tests/cabal/cabal08/tmp*
 /tests/cabal/local01.package.conf/
 /tests/cabal/local03.package.conf/
 /tests/cabal/local04.package.conf/
diff --git a/testsuite/tests/cabal/cabal08/Main.hs b/testsuite/tests/cabal/cabal08/Main.hs
new file mode 100644 (file)
index 0000000..f63ccaf
--- /dev/null
@@ -0,0 +1,2 @@
+import P
+main = putStrLn p
diff --git a/testsuite/tests/cabal/cabal08/Makefile b/testsuite/tests/cabal/cabal08/Makefile
new file mode 100644 (file)
index 0000000..06b9853
--- /dev/null
@@ -0,0 +1,32 @@
+TOP=../../..
+include $(TOP)/mk/boilerplate.mk
+include $(TOP)/mk/test.mk
+
+SETUP=../Setup -v0
+
+# in both cases, p2 should be preferred
+cabal08: clean
+       $(MAKE) clean
+       '$(GHC_PKG)' init tmp1.d
+       '$(GHC_PKG)' init tmp2.d
+       '$(TEST_HC)' -v0 --make Setup
+       cd p1 && $(SETUP) clean
+       cd p1 && $(SETUP) configure $(CABAL_MINIMAL_BUILD) --with-ghc='$(TEST_HC)' --ghc-options='$(TEST_HC_OPTS)' --package-db=../tmp1.d --prefix='$(PWD)/inst-p1'
+       cd p1 && $(SETUP) build
+       cd p1 && $(SETUP) copy
+       cd p1 && $(SETUP) register
+       cd p2 && $(SETUP) clean
+       cd p2 && $(SETUP) configure $(CABAL_MINIMAL_BUILD) --with-ghc='$(TEST_HC)' --ghc-options='$(TEST_HC_OPTS)' --package-db=../tmp2.d --prefix='$(PWD)/inst-p2'
+       cd p2 && $(SETUP) build
+       cd p2 && $(SETUP) copy
+       cd p2 && $(SETUP) register
+       '$(TEST_HC)' $(TEST_HC_OPTS) -package-db tmp1.d -package-db tmp2.d Main.hs
+       ./Main
+       '$(TEST_HC)' $(TEST_HC_OPTS) -package-db tmp1.d -package-db tmp2.d -hide-all-packages -package base -package p Main.hs
+       ./Main
+ifneq "$(CLEANUP)" ""
+       $(MAKE) clean
+endif
+
+clean :
+       $(RM) -r tmp*.d inst-* *.o *.hi */*.o */*.hi */Setup$(exeext) */dist Setup$(exeext)
diff --git a/testsuite/tests/cabal/cabal08/Setup.hs b/testsuite/tests/cabal/cabal08/Setup.hs
new file mode 100644 (file)
index 0000000..9a994af
--- /dev/null
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
diff --git a/testsuite/tests/cabal/cabal08/all.T b/testsuite/tests/cabal/cabal08/all.T
new file mode 100644 (file)
index 0000000..fc4221a
--- /dev/null
@@ -0,0 +1,9 @@
+if default_testopts.cleanup != '':
+   cleanup = 'CLEANUP=1'
+else:
+   cleanup = ''
+
+test('cabal08',
+     normal,
+     run_command,
+     ['$MAKE -s --no-print-directory cabal08 ' + cleanup])
diff --git a/testsuite/tests/cabal/cabal08/cabal08.stdout b/testsuite/tests/cabal/cabal08/cabal08.stdout
new file mode 100644 (file)
index 0000000..8f97cd4
--- /dev/null
@@ -0,0 +1,6 @@
+[1 of 1] Compiling Main             ( Main.hs, Main.o )
+Linking Main ...
+p2
+[1 of 1] Compiling Main             ( Main.hs, Main.o )
+Linking Main ...
+p2
diff --git a/testsuite/tests/cabal/cabal08/p1/ChangeLog.md b/testsuite/tests/cabal/cabal08/p1/ChangeLog.md
new file mode 100644 (file)
index 0000000..b454db5
--- /dev/null
@@ -0,0 +1,5 @@
+# Revision history for p
+
+## 0.1.0.0  -- YYYY-mm-dd
+
+* First version. Released on an unsuspecting world.
diff --git a/testsuite/tests/cabal/cabal08/p1/LICENSE b/testsuite/tests/cabal/cabal08/p1/LICENSE
new file mode 100644 (file)
index 0000000..c8cc6ab
--- /dev/null
@@ -0,0 +1,30 @@
+Copyright (c) 2015, Edward Z. Yang
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+
+    * Redistributions in binary form must reproduce the above
+      copyright notice, this list of conditions and the following
+      disclaimer in the documentation and/or other materials provided
+      with the distribution.
+
+    * Neither the name of Edward Z. Yang nor the names of other
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/testsuite/tests/cabal/cabal08/p1/P.hs b/testsuite/tests/cabal/cabal08/p1/P.hs
new file mode 100644 (file)
index 0000000..e3109b2
--- /dev/null
@@ -0,0 +1,2 @@
+module P where
+p = "p1"
diff --git a/testsuite/tests/cabal/cabal08/p1/Setup.hs b/testsuite/tests/cabal/cabal08/p1/Setup.hs
new file mode 100644 (file)
index 0000000..9a994af
--- /dev/null
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
diff --git a/testsuite/tests/cabal/cabal08/p1/p.cabal b/testsuite/tests/cabal/cabal08/p1/p.cabal
new file mode 100644 (file)
index 0000000..8624a04
--- /dev/null
@@ -0,0 +1,14 @@
+name:                p
+version:             0.1.0.0
+license:             BSD3
+license-file:        LICENSE
+author:              Edward Z. Yang
+maintainer:          ezyang@cs.stanford.edu
+build-type:          Simple
+extra-source-files:  ChangeLog.md
+cabal-version:       >=1.10
+
+library
+  exposed-modules:     P
+  build-depends:       base, containers
+  default-language:    Haskell2010
diff --git a/testsuite/tests/cabal/cabal08/p2/ChangeLog.md b/testsuite/tests/cabal/cabal08/p2/ChangeLog.md
new file mode 100644 (file)
index 0000000..b454db5
--- /dev/null
@@ -0,0 +1,5 @@
+# Revision history for p
+
+## 0.1.0.0  -- YYYY-mm-dd
+
+* First version. Released on an unsuspecting world.
diff --git a/testsuite/tests/cabal/cabal08/p2/LICENSE b/testsuite/tests/cabal/cabal08/p2/LICENSE
new file mode 100644 (file)
index 0000000..c8cc6ab
--- /dev/null
@@ -0,0 +1,30 @@
+Copyright (c) 2015, Edward Z. Yang
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+
+    * Redistributions in binary form must reproduce the above
+      copyright notice, this list of conditions and the following
+      disclaimer in the documentation and/or other materials provided
+      with the distribution.
+
+    * Neither the name of Edward Z. Yang nor the names of other
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/testsuite/tests/cabal/cabal08/p2/P.hs b/testsuite/tests/cabal/cabal08/p2/P.hs
new file mode 100644 (file)
index 0000000..2f8fe77
--- /dev/null
@@ -0,0 +1,2 @@
+module P where
+p = "p2"
diff --git a/testsuite/tests/cabal/cabal08/p2/Setup.hs b/testsuite/tests/cabal/cabal08/p2/Setup.hs
new file mode 100644 (file)
index 0000000..9a994af
--- /dev/null
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
diff --git a/testsuite/tests/cabal/cabal08/p2/p.cabal b/testsuite/tests/cabal/cabal08/p2/p.cabal
new file mode 100644 (file)
index 0000000..dc88c82
--- /dev/null
@@ -0,0 +1,14 @@
+name:                p
+version:             0.1.0.0
+license:             BSD3
+license-file:        LICENSE
+author:              Edward Z. Yang
+maintainer:          ezyang@cs.stanford.edu
+build-type:          Simple
+extra-source-files:  ChangeLog.md
+cabal-version:       >=1.10
+
+library
+  exposed-modules:     P
+  build-depends:       base
+  default-language:    Haskell2010