Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure users don't create a .wasp file #2418

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

komyg
Copy link
Contributor

@komyg komyg commented Dec 14, 2024

Description

Fixes: #2406

Select what type of change this PR introduces:

  1. Just code/docs improvement (no functional change).
  2. Bug fix (non-breaking change which fixes an issue).
  3. New feature (non-breaking change which adds functionality).
  4. Breaking change (fix or feature that would cause existing functionality to not work as expected).

Update Waspc ChangeLog and version if needed

If you did a bug fix, new feature, or breaking change, that affects waspc, make sure you satisfy the following:

  1. I updated ChangeLog.md with description of the change this PR introduces.
  2. I bumped waspc version in waspc.cabal to reflect changes I introduced, with regards to the version of the latest wasp release, if the bump was needed.

Update example apps if needed

If you did code changes and added a new feature or modified an existing feature, make sure you satisfy the following:

  1. I updated waspc/examples/todoApp as needed (updated modified feature or added new feature) and manually checked it works correctly.
  2. I updated waspc/headless-test/examples/todoApp and its e2e tests as needed (updated modified feature and its tests or added new feature and new tests for it).

@komyg komyg force-pushed the fix/2406/check-for-dot-wasp-file branch from acd4f7c to 11862bc Compare December 19, 2024 18:35
@Martinsos Martinsos requested a review from sodic December 19, 2024 20:32
Copy link
Contributor

@sodic sodic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @komyg, thanks for the effort!

You have the right idea, and I see you know how to use both Haskell and our codebase (props for using relfile and IOUtil.doesFileExist), so I have no doubt we'll be merging this soon.

There are some improvements we can do, I left three requests (the biggest of which is moving the logic into findWaspFile).

Let me know if you have any questions :)

Comment on lines 253 to 254
waspDirExists :: Path' Abs (Dir WaspProjectDir) -> IO (Either String (Path' Abs (Dir WaspProjectDir)))
waspDirExists waspDir = do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is called waspDirExists, implying that it checks whether a .wasp directory exists and returns either an error or a path to the directory.

But, in reality, the function does something else:

  • If a .wasp directory does exist, it returns a path to the existing .wasp directory (so far so good)
  • If the .wasp directory doesn't exist, it returns a path to the non-existing .wasp directory (this is the first weird part)
  • If a .wasp file exists, it returns an error saying that .wasp is a file (but the function's name implies it should only care about whether the .wasp directory exists and report on that).

Imagine what happens when someone else tries to use this function in a different context (e.g., to really check whether the .wasp directory exists). I think they'd end up very confused :)

Anyway, for this issue, we don't care whether the .wasp directory exists or not. It can exist, it doesn't need to exist - it's all the same to us.

We only care about whether a .wasp file exists, so we should write a function to check for that, and name it accordingly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right.

My initial idea here, was to make this function return the waspDir as a right value (in case there was no .wasp file), or an error as a left value.

Then I would send the right result to the next function call: waspFilePathOrError <- left (: []) <$> findWaspFile waspDir, because that function takes the waspDir as an argument.

However, I didn't end up doing this, and in the end I should just have returned a boolean or empty right value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My original plan for this implementation was to create a sort of pipeline in which the result of the previous function would go into the next function, as it is usually done in railway oriented programming.

This way, I would be able to avoid having two case statements in the analyzeWaspProject, because the next waspFilePathOrError would only be called if its input was a right value, otherwise it will pass through the left value unchanged.

Unfortunately, I was unable to do that, and I forgot to change the return type of this function to boolean or an empty right value.

let waspDotWaspPath = waspDir </> [relfile|.wasp|]
isFile <- IOUtil.doesFileExist waspDotWaspPath
if isFile
then return $ Left "The path to the Wasp project is a file, but it should be a directory."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "Wasp project" is the user's project, not the stuff in the .wasp directory.

Also, users don't really care about the .wasp directory, so this message gives out unnecessary information. If I read it, I'd think Wasp expects me to create a .wasp directory (which isn't true).

We should tell users something that conveys the message "Hey, your .wasp file can't be called .wasp, it needs to have a proper name," no need to mention the .wasp directory at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I've re-phrased the message as: Invalid file name for the .wasp file. Please rename it to [something].wasp.. What do you think?

Comment on lines 81 to 85
dirResult <- waspDirExists waspDir
case dirResult of
Left err -> return (Left [err], [])
Right _ -> do
waspFilePathOrError <- left (: []) <$> findWaspFile waspDir
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better place to handle this check is the function findWaspFile.

After all, we don't care about the directory at all. All we care about is:

  • Do the users have a *.wasp file?
  • If they do, is it called something other than just .wasp?
  • If not, tell them to name it properly.

That's on me, the issue could have been explained better. Sorry for the troubles :)

So, to summarize one last time:

While looking for a *.wasp file, we should also ensure that the found file isn't called .wasp

While you're at it, you might also want to check whether there are multiple .wasp files and report that.

Because, in theory, Wasp will only find one of *.wasp files. Let's say we have two of them: something.wasp and .wasp. findWaspFile could return the something.wasp, letting .wasp slip under the radar and cause problems later.

And even if it didn't, allowing multiple .wasp files can be confusing.

@komyg komyg force-pushed the fix/2406/check-for-dot-wasp-file branch from 9e3bada to 9830797 Compare December 23, 2024 13:06
@komyg komyg requested a review from sodic December 24, 2024 00:08
@komyg
Copy link
Contributor Author

komyg commented Dec 24, 2024

Hey @sodic, thank you for your review, it was very through!

I believe I addressed most of your comments. Could you review again, please?

The only thing left is making sure we don't have more than one .wasp file. I am currently working on that, but it is not done yet. If I can get it done before your next review, then I can be a part of this PR, otherwise, I can make it into another PR. What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ensure users don't create a .wasp file
2 participants