Skip to content

Add invertRelDir to StrongPath utilities#3923

Merged
FranjoMindek merged 11 commits intomainfrom
franjo/extract-invertRelDir
Mar 18, 2026
Merged

Add invertRelDir to StrongPath utilities#3923
FranjoMindek merged 11 commits intomainfrom
franjo/extract-invertRelDir

Conversation

@FranjoMindek
Copy link
Contributor

@FranjoMindek FranjoMindek commented Mar 16, 2026

Summary

Main change:

  • Adds invertRelDir to Wasp.Util.StrongPath. Given a relative directory path from a to b, returns the inverse path from b back to a. The limitation is that the reverse path can't have any ../ segments.

Side stuff:

  • Refactors various parts of code to use invertRelDir instead of a hardcoded [reldir|../../|] variations
  • Moves resolving "libs path from SDK/server" from WaspLibs.Common to SDK/Server.Common to avoid cyclic dependencies.
  • Removes FilePath.Extra which just existed doing nothing.
  • Removes serverRootDirFromDbRootDir and webAppRootDirFromDbRootDir. Wanted to make them use invertRelDir but found they were unused.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 16, 2026

Open in StackBlitz

@wasp.sh/wasp-cli

npx https://pkg.pr.new/@wasp.sh/wasp-cli@3923

@wasp.sh/wasp-cli-darwin-arm64-unknown

npx https://pkg.pr.new/@wasp.sh/wasp-cli-darwin-arm64-unknown@3923

@wasp.sh/wasp-cli-darwin-x64-unknown

npx https://pkg.pr.new/@wasp.sh/wasp-cli-darwin-x64-unknown@3923

@wasp.sh/wasp-cli-linux-x64-glibc

npx https://pkg.pr.new/@wasp.sh/wasp-cli-linux-x64-glibc@3923

@wasp.sh/wasp-cli-linux-x64-musl

npx https://pkg.pr.new/@wasp.sh/wasp-cli-linux-x64-musl@3923

commit: d463af3

@FranjoMindek FranjoMindek temporarily deployed to railway-deploy-test March 16, 2026 17:03 — with GitHub Actions Inactive
@FranjoMindek FranjoMindek changed the title Claude: Add invertRelDir to StrongPath utilities Add invertRelDir to StrongPath utilities Mar 16, 2026
-- If any of those change significantly (their depth), this path should be adjusted.
waspProjectDirFromProjectRootDir :: Path' (Rel G.Common.ProjectRootDir) (Dir WaspProjectDir)
waspProjectDirFromProjectRootDir = [reldir|../../|]
waspProjectDirFromAppComponentDir = [reldir|../|] </> waspProjectDirFromGeneratedCodeDir
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't have anything to invert for AppComponentRootDir since it is "abstract" path.

Comment on lines -51 to -55
serverRootDirFromDbRootDir :: Path' (Rel DbRootDir) (Dir ServerRootDir)
serverRootDirFromDbRootDir = [reldir|../server|]

webAppRootDirFromDbRootDir :: Path' (Rel DbRootDir) (Dir ServerRootDir)
webAppRootDirFromDbRootDir = [reldir|../web-app|]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Never used

libsSrcDirPathInDataDir = [reldir|Generator/libs|]

libsRootDirFromSdkDir :: Path' Rel' (Dir LibsRootDir)
libsRootDirFromSdkDir = [reldir|../../|] </> libsRootDirInGeneratedCodeDir
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved to respective SDK and Server common files.

@FranjoMindek FranjoMindek marked this pull request as ready for review March 16, 2026 20:51
@FranjoMindek FranjoMindek requested a review from a team as a code owner March 16, 2026 20:51
@FranjoMindek FranjoMindek requested review from cprecioso and removed request for a team March 16, 2026 20:51
Copy link
Member

@cprecioso cprecioso left a comment

Choose a reason for hiding this comment

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

Lgtm, couple of minor suggestions.

-- >>> invertRelDir [reldir|.|] -- "./"
-- >>> invertRelDir [reldir|types|] -- "../"
-- >>> invertRelDir [reldir|.wasp/out|] -- "../../"
invertRelDir :: SP.Path' (SP.Rel a) (SP.Dir b) -> SP.Path' (SP.Rel b) (SP.Dir a)
Copy link
Member

Choose a reason for hiding this comment

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

Should we have a version of this function that returns an Either String Path or Maybe Path in case we want to handle errors? And then a wrapper that errors out on Left so we keep the simple API for internal stuff.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would keep it simple for now, since we only use this helper for strong-path paths we have defined, and we know are valid.

| ".." `elem` pathSegments = error $ "invertRelDir: path contains '..' segment: " ++ SP.fromRelDir relDir
| otherwise = fromJust . SP.parseRelDir $ FP.joinPath $ replicate (length pathSegments) ".."
where
pathSegments = filter (/= ".") $ FP.splitDirectories $ SP.fromRelDir relDir
Copy link
Member

Choose a reason for hiding this comment

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

instead of filter (/= ".") is there a normalize path fn or something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea:

invertRelDir :: SP.Path' (SP.Rel a) (SP.Dir b) -> SP.Path' (SP.Rel b) (SP.Dir a)
invertRelDir relDir
  | ".." `elem` pathSegments = error $ "invertRelDir: path contains '..' segment: " ++ SP.fromRelDir relDir
  | otherwise = case pathSegments of
      ["."] -> [SP.reldir|.|]
      _ -> fromJust . SP.parseRelDir $ FP.joinPath $ replicate (length pathSegments) ".."
  where
    pathSegments = FP.splitDirectories . FP.normalise $ SP.fromRelDir relDir

This comment was marked as resolved.

evaluate (invertRelDir [reldir|../../a|]) `shouldThrow` anyErrorCall

it "returns current dir for current dir input" $ do
invertRelDir [reldir|.|] `shouldBe` [reldir|./|]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We use strong-path on both sides so that tests work on any OS.
Mostly because both work on Path System.

@FranjoMindek FranjoMindek merged commit 9e20913 into main Mar 18, 2026
34 of 36 checks passed
@FranjoMindek FranjoMindek deleted the franjo/extract-invertRelDir branch March 18, 2026 10:31
@FranjoMindek FranjoMindek self-assigned this Mar 18, 2026
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.

3 participants