Skip to content

Commit 9dfe91a

Browse files
committed
Warn if untrimmed URI is detected
- Error if an untrimmed URI is detected - Add a changelog - Add UntrimmedImport tests - Soften the error down to a warning - Use with-ghc.config trick - Add fix-whitespace exceptions for test projects - Include W3C quote is changelog - Remove unused LANGUAGE pragma - Rerun test now that URL imports are sorted last - Move test underneath ProjectImport parent dir
1 parent 11f91fd commit 9dfe91a

File tree

10 files changed

+100
-0
lines changed

10 files changed

+100
-0
lines changed

cabal-install-solver/src/Distribution/Solver/Types/ProjectConfigPath.hs

+15
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ module Distribution.Solver.Types.ProjectConfigPath
1414
, docProjectConfigPath
1515
, docProjectConfigFiles
1616
, cyclicalImportMsg
17+
, untrimmedUriImportMsg
1718
, docProjectConfigPathFailReason
1819

1920
-- * Checks and Normalization
2021
, isCyclicConfigPath
2122
, isTopLevelConfigPath
23+
, isUntrimmedUriConfigPath
2224
, canonicalizeConfigPath
2325
) where
2426

@@ -155,6 +157,14 @@ cyclicalImportMsg path@(ProjectConfigPath (duplicate :| _)) =
155157
, nest 2 (docProjectConfigPath path)
156158
]
157159

160+
-- | A message for an import that has leading or trailing spaces.
161+
untrimmedUriImportMsg :: Doc -> ProjectConfigPath -> Doc
162+
untrimmedUriImportMsg intro path =
163+
vcat
164+
[ intro <+> text "import has leading or trailing whitespace" <> semi
165+
, nest 2 (docProjectConfigPath path)
166+
]
167+
158168
docProjectConfigPathFailReason :: VR -> ProjectConfigPath -> Doc
159169
docProjectConfigPathFailReason vr pcp
160170
| ProjectConfigPath (p :| []) <- pcp =
@@ -183,6 +193,11 @@ nullProjectConfigPath = ProjectConfigPath $ "unused" :| []
183193
isCyclicConfigPath :: ProjectConfigPath -> Bool
184194
isCyclicConfigPath (ProjectConfigPath p) = length p /= length (NE.nub p)
185195

196+
-- | Check if the last segment of the path (root or importee) is a URI that has
197+
-- leading or trailing spaces.
198+
isUntrimmedUriConfigPath :: ProjectConfigPath -> Bool
199+
isUntrimmedUriConfigPath (ProjectConfigPath (p :| _)) = let p' = trim p in p' /= p && isURI p'
200+
186201
-- | Check if the project config path is top-level, meaning it was not included by
187202
-- some other project config.
188203
isTopLevelConfigPath :: ProjectConfigPath -> Bool

cabal-install/src/Distribution/Client/ProjectConfig/Legacy.hs

+4
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ import Distribution.Simple.Setup
126126
import Distribution.Simple.Utils
127127
( debug
128128
, lowercase
129+
, noticeDoc
129130
)
130131
import Distribution.Types.CondTree
131132
( CondBranch (..)
@@ -275,6 +276,9 @@ parseProjectSkeleton cacheDir httpTransport verbosity projectDir source (Project
275276
if isCyclicConfigPath normLocPath
276277
then pure . parseFail $ ParseUtils.FromString (render $ cyclicalImportMsg normLocPath) Nothing
277278
else do
279+
when
280+
(isUntrimmedUriConfigPath importLocPath)
281+
(noticeDoc verbosity $ untrimmedUriImportMsg (Disp.text "Warning:") importLocPath)
278282
normSource <- canonicalizeConfigPath projectDir source
279283
let fs = (\z -> CondNode z [normLocPath] mempty) <$> fieldsToConfig normSource (reverse acc)
280284
res <- parseProjectSkeleton cacheDir httpTransport verbosity projectDir importLocPath . ProjectConfigToParse =<< fetchImportConfig normLocPath
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# checking project import with trailing space
2+
# cabal v2-build
3+
Warning: import has leading or trailing whitespace;
4+
'https://www.stackage.org/nightly-2024-12-05/cabal.config '
5+
imported by: trailing-space.project
6+
Configuration is affected by the following files:
7+
- trailing-space.project
8+
- with-ghc.config
9+
imported by: trailing-space.project
10+
- https://www.stackage.org/nightly-2024-12-05/cabal.config
11+
imported by: trailing-space.project
12+
Resolving dependencies...
13+
Build profile: -w ghc-<GHCVER> -O1
14+
In order, the following would be built:
15+
- my-0.1 (lib:my) (first run)
16+
# checking project import with tabs and spaces
17+
# cabal v2-build
18+
Warning: import has leading or trailing whitespace;
19+
'https://www.stackage.org/nightly-2024-12-05/cabal.config '
20+
imported by: tabs-and-spaces.project
21+
Configuration is affected by the following files:
22+
- tabs-and-spaces.project
23+
- with-ghc.config
24+
imported by: tabs-and-spaces.project
25+
- https://www.stackage.org/nightly-2024-12-05/cabal.config
26+
imported by: tabs-and-spaces.project
27+
Resolving dependencies...
28+
Build profile: -w ghc-<GHCVER> -O1
29+
In order, the following would be built:
30+
- my-0.1 (lib:my) (first run)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Test.Cabal.Prelude
2+
3+
main = cabalTest . recordMode RecordMarked $ do
4+
let log = recordHeader . pure
5+
6+
log "checking project import with trailing space"
7+
trailing <- cabal' "v2-build" [ "--dry-run", "--project-file=trailing-space.project" ]
8+
assertOutputContains "import has leading or trailing whitespace" trailing
9+
assertOutputContains "'https://www.stackage.org/nightly-2024-12-05/cabal.config '" trailing
10+
11+
log "checking project import with tabs and spaces"
12+
cabal "v2-build" [ "--dry-run", "--project-file=tabs-and-spaces.project" ]
13+
14+
return ()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: my
2+
version: 0.1
3+
license: BSD3
4+
cabal-version: >= 1.2
5+
build-type: Simple
6+
7+
library
8+
exposed-modules: Foo
9+
build-depends: base
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
packages: .
2+
import: https://www.stackage.org/nightly-2024-12-05/cabal.config
3+
import: with-ghc.config
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
packages: .
2+
import: https://www.stackage.org/nightly-2024-12-05/cabal.config
3+
import: with-ghc.config
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- WARNING: Override the `with-compiler: ghc-x.y.z` of the stackage import, of
2+
-- https://www.stackage.org/nightly-yyyy-mm-dd/cabal.config. Otherwise tests
3+
-- will fail with:
4+
-- -Error: [Cabal-5490]
5+
-- -Cannot find the program 'ghc'. User-specified path 'ghc-x.y.z' does not
6+
-- refer to an executable and the program is not on the system path.
7+
with-compiler: ghc

changelog.d/pr-10629

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
synopsis: "Report trailing spaces in project import URIs"
3+
packages: [cabal-install, cabal-install-solver]
4+
prs: 10629
5+
issues: 10622
6+
---
7+
8+
> A string is a valid URL potentially surrounded by spaces if, after stripping
9+
> leading and trailing whitespace from it, it is a valid URL."
10+
> SOURCE: [W3C/HTML5/URLs](https://www.w3.org/TR/2010/WD-html5-20100624/urls.html)
11+
12+
Fixes a problem of mistaking a URI for a file path when it has trailing spaces
13+
and warn about such trailing spaces.

fix-whitespace.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ excluded-files:
9595
- Cabal-tests/tests/ParserTests/warnings/tab.cabal
9696
- Cabal-tests/tests/ParserTests/warnings/utf8.cabal
9797
- cabal-testsuite/PackageTests/Regression/T8507/pkg.cabal
98+
- cabal-testsuite/PackageTests/ProjectImport/UntrimmedImport/trailing-space.project
99+
- cabal-testsuite/PackageTests/ProjectImport/UntrimmedImport/tabs-and-spaces.project
98100

99101
# These also contain tabs that affect the golden value:
100102
# Could be removed from exceptions, but then the tab warning

0 commit comments

Comments
 (0)