Skip to content

Commit 1586aaa

Browse files
authored
Don't Glob if Glob Ain't Glob 2: The Globbening (#10518)
* Don't Glob if Glob Ain't Glob 2: The Globbening * make style * Fix tests * Fix output
1 parent 1078916 commit 1586aaa

File tree

3 files changed

+30
-11
lines changed
  • Cabal-tests/tests/UnitTests/Distribution/Simple
  • Cabal/src/Distribution/Simple
  • cabal-testsuite/PackageTests/Check/ConfiguredPackage/Paths/AbsolutePath

3 files changed

+30
-11
lines changed

Cabal-tests/tests/UnitTests/Distribution/Simple/Glob.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,13 @@ testMatchesVersion version pat expected = do
107107
-- check can't identify that kind of match.
108108
expected' = filter (\case GlobMatchesDirectory _ -> False; _ -> True) expected
109109
unless (sort expected' == sort actual) $
110-
assertFailure $ "Unexpected result (pure matcher): " ++ show actual
110+
assertFailure $ "Unexpected result (pure matcher): " ++ show actual ++ "\nExpected: " ++ show expected
111111
checkIO globPat =
112112
withSystemTempDirectory "globstar-sample" $ \tmpdir -> do
113113
makeSampleFiles tmpdir
114114
actual <- runDirFileGlob Verbosity.normal (Just version) tmpdir globPat
115115
unless (isEqual actual expected) $
116-
assertFailure $ "Unexpected result (impure matcher): " ++ show actual
116+
assertFailure $ "Unexpected result (impure matcher): " ++ show actual ++ "\nExpected: " ++ show expected
117117

118118
testFailParseVersion :: CabalSpecVersion -> FilePath -> GlobSyntaxError -> Assertion
119119
testFailParseVersion version pat expected =

Cabal/src/Distribution/Simple/Glob.hs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,6 @@ runDirFileGlob verbosity mspec rawRoot pat = do
370370
"Null dir passed to runDirFileGlob; interpreting it "
371371
++ "as '.'. This is probably an internal error."
372372
let root = if null rawRoot then "." else rawRoot
373-
debug verbosity $ "Expanding glob '" ++ show (pretty pat) ++ "' in directory '" ++ root ++ "'."
374373
-- This function might be called from the project root with dir as
375374
-- ".". Walking the tree starting there involves going into .git/
376375
-- and dist-newstyle/, which is a lot of work for no reward, so
@@ -379,7 +378,7 @@ runDirFileGlob verbosity mspec rawRoot pat = do
379378
-- the whole directory if *, and just the specific file if it's a
380379
-- literal.
381380
let
382-
(prefixSegments, variablePattern) = splitConstantPrefix pat
381+
(prefixSegments, pathOrVariablePattern) = splitConstantPrefix pat
383382
joinedPrefix = joinPath prefixSegments
384383

385384
-- The glob matching function depends on whether we care about the cabal version or not
@@ -431,17 +430,37 @@ runDirFileGlob verbosity mspec rawRoot pat = do
431430
concat <$> traverse (\subdir -> go globPath (dir </> subdir)) subdirs
432431
go GlobDirTrailing dir = return [GlobMatch dir]
433432

434-
directoryExists <- doesDirectoryExist (root </> joinedPrefix)
435-
if directoryExists
436-
then go variablePattern joinedPrefix
437-
else return [GlobMissingDirectory joinedPrefix]
433+
case pathOrVariablePattern of
434+
Left filename -> do
435+
let filepath = joinedPrefix </> filename
436+
debug verbosity $ "Treating glob as filepath literal '" ++ filepath ++ "' in directory '" ++ root ++ "'."
437+
directoryExists <- doesDirectoryExist (root </> filepath)
438+
if directoryExists
439+
then pure [GlobMatchesDirectory filepath]
440+
else do
441+
exist <- doesFileExist (root </> filepath)
442+
pure $
443+
if exist
444+
then [GlobMatch filepath]
445+
else []
446+
Right variablePattern -> do
447+
debug verbosity $ "Expanding glob '" ++ show (pretty pat) ++ "' in directory '" ++ root ++ "'."
448+
directoryExists <- doesDirectoryExist (root </> joinedPrefix)
449+
if directoryExists
450+
then go variablePattern joinedPrefix
451+
else return [GlobMissingDirectory joinedPrefix]
438452
where
439453
-- \| Extract the (possibly null) constant prefix from the pattern.
440454
-- This has the property that, if @(pref, final) = splitConstantPrefix pat@,
441455
-- then @pat === foldr GlobDir final pref@.
442-
splitConstantPrefix :: Glob -> ([FilePath], Glob)
443-
splitConstantPrefix = unfoldr' step
456+
splitConstantPrefix :: Glob -> ([FilePath], Either FilePath Glob)
457+
splitConstantPrefix = fmap literalize . unfoldr' step
444458
where
459+
literalize (GlobFile [Literal filename]) =
460+
Left filename
461+
literalize glob =
462+
Right glob
463+
445464
step (GlobDir [Literal seg] pat') = Right (seg, pat')
446465
step pat' = Left pat'
447466

cabal-testsuite/PackageTests/Check/ConfiguredPackage/Paths/AbsolutePath/cabal.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# cabal check
22
These warnings may cause trouble when distributing the package:
3-
Warning: [glob-missing-dir] In 'extra-source-files': the pattern '/home/user/file' attempts to match files in the directory '/home/user', but there is no directory by that name.
3+
Warning: [no-glob-match] In 'extra-source-files': the pattern '/home/user/file' does not match any files.
44
The following errors will cause portability problems on other environments:
55
Error: [absolute-path] 'extra-source-files: /home/user/file' specifies an absolute path, but the 'extra-source-files' field must use relative paths.
66
Error: [malformed-relative-path] 'extra-source-files: /home/user/file' is not a good relative path: "posix absolute path"

0 commit comments

Comments
 (0)