Skip to content

Commit 12b4bd3

Browse files
authoredFeb 12, 2025
Improve .wasp file checks and messages (#2418)
1 parent 57c4013 commit 12b4bd3

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed
 

‎waspc/ChangeLog.md

+2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ Read more about breaking changes in the migration guide: https://wasp.sh/docs/mi
9797

9898
Big thanks to our community members who contributed to this release! @Bojun-Feng @dabrorius @komyg @NathanaelA @vblazenka @genyus
9999

100+
- Improved the error message when the user has a top level *.wasp* file.
101+
100102
## 0.15.2
101103

102104
### 🐞 Bug fixes

‎waspc/src/Wasp/Project/WaspFile.hs

+21-17
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ module Wasp.Project.WaspFile
44
)
55
where
66

7-
import Data.List (find, isSuffixOf)
87
import StrongPath
98
( Abs,
109
Dir,
1110
Path',
1211
castFile,
13-
fromAbsDir,
1412
fromRelFile,
1513
(</>),
1614
)
@@ -25,27 +23,33 @@ import Wasp.Project.WaspFile.TypeScript (analyzeWaspTsFile)
2523
import Wasp.Project.WaspFile.WaspLang (analyzeWaspLangFile)
2624
import qualified Wasp.Psl.Ast.Schema as Psl.Schema
2725
import qualified Wasp.Util.IO as IOUtil
26+
import Wasp.Util.StrongPath (findAllFilesWithSuffix)
2827

2928
findWaspFile :: Path' Abs (Dir WaspProjectDir) -> IO (Either String WaspFilePath)
30-
findWaspFile waspDir = do
31-
files <- fst <$> IOUtil.listDirectory waspDir
32-
return $ case (findWaspTsFile files, findWaspLangFile files) of
33-
(Just _, Just _) -> Left bothFilesFoundMessage
34-
(Nothing, Nothing) -> Left fileNotFoundMessage
35-
(Just waspTsFile, Nothing) -> Right waspTsFile
36-
(Nothing, Just waspLangFile) -> Right waspLangFile
29+
findWaspFile projectDir = do
30+
filesInProjectDir <- fst <$> IOUtil.listDirectory projectDir
31+
let filesEndingWithWasp = findAllFilesWithSuffix ".wasp" filesInProjectDir
32+
filesEndingWithWaspTs = findAllFilesWithSuffix ".wasp.ts" filesInProjectDir
33+
return $ case (filesEndingWithWasp, filesEndingWithWaspTs) of
34+
([], []) -> Left fileNotFoundMessage
35+
(_ : _, _ : _) -> Left bothFilesFoundMessage
36+
([], waspTsFiles) -> case waspTsFiles of
37+
[singleWaspTsFile]
38+
| fromRelFile singleWaspTsFile == ".wasp.ts" -> Left (makeInvalidFileNameMessage ".wasp.ts")
39+
| otherwise -> Right . WaspTs $ castFile (projectDir </> singleWaspTsFile)
40+
multipleWaspTsFiles -> Left (makeMultipleFilesMessage "*.wasp.ts" (map fromRelFile multipleWaspTsFiles))
41+
(waspLangFiles, []) -> case waspLangFiles of
42+
[singleWaspLangFile]
43+
| fromRelFile singleWaspLangFile == ".wasp" -> Left (makeInvalidFileNameMessage ".wasp")
44+
| otherwise -> Right . WaspLang $ castFile (projectDir </> singleWaspLangFile)
45+
multipleWaspFiles -> Left (makeMultipleFilesMessage "*.wasp" (map fromRelFile multipleWaspFiles))
3746
where
38-
findWaspTsFile files = WaspTs <$> findFileThatEndsWith ".wasp.ts" files
39-
findWaspLangFile files = WaspLang <$> findFileThatEndsWith ".wasp" files
40-
findFileThatEndsWith suffix files =
41-
castFile
42-
. (waspDir </>)
43-
<$> find ((suffix `isSuffixOf`) . fromRelFile) files
44-
45-
fileNotFoundMessage = "Couldn't find a *.wasp or a *.wasp.ts file in directory " ++ fromAbsDir waspDir ++ " ."
47+
fileNotFoundMessage = "Couldn't find the *.wasp or a *.wasp.ts file in the project directory."
4648
bothFilesFoundMessage =
4749
"Found both *.wasp and *.wasp.ts files in the project directory. "
4850
++ "You must choose how you want to define your app (using Wasp or TypeScript) and only keep one of them."
51+
makeMultipleFilesMessage suffix files = "Found multiple " ++ suffix ++ " files in the project directory: " ++ show files ++ ". Please keep only one."
52+
makeInvalidFileNameMessage suffix = "Your Wasp file can't be called '" ++ suffix ++ "'. Please rename it to something like [name]" ++ suffix ++ "."
4953

5054
analyzeWaspFile ::
5155
Path' Abs (Dir WaspProjectDir) ->

‎waspc/src/Wasp/Util/StrongPath.hs

+5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ module Wasp.Util.StrongPath
44
stripProperPrefix,
55
splitAbsExtension,
66
splitRelExtension,
7+
findAllFilesWithSuffix,
78
)
89
where
910

1011
import Control.Arrow (first)
1112
import Control.Monad.Catch (MonadThrow)
13+
import Data.List (isSuffixOf)
1214
import qualified Path as P
1315
import qualified StrongPath as SP
1416
import qualified StrongPath.Path as SP
@@ -33,3 +35,6 @@ splitAbsExtension path =
3335
splitRelExtension :: MonadThrow m => SP.Path' (SP.Rel b) (SP.File a) -> m (SP.Path' (SP.Rel b) (SP.File c), String)
3436
splitRelExtension path =
3537
first SP.fromPathRelFile <$> P.splitExtension (SP.toPathRelFile path)
38+
39+
findAllFilesWithSuffix :: String -> [SP.Path p r (SP.File f)] -> [SP.Path p r (SP.File f)]
40+
findAllFilesWithSuffix extension = filter ((extension `isSuffixOf`) . SP.toFilePath)

0 commit comments

Comments
 (0)