purescript-contrib / purescript-pathy

A type-safe abstraction for platform-independent file system paths.
Apache License 2.0
33 stars 17 forks source link

all sandbox methods should return Abs Paths? #53

Open srghma opened 1 month ago

srghma commented 1 month ago

Describe the bug

I write https://github.com/srghma/purescript-pathy-node/blob/56c0cbce481ac50b11f5b66690967701a5281d65/test/Test/Main.purs#L109-L110

    -- outerTmpDir :: SandboxedPath Rel Dir
    -- outerTmpDir = sandboxAny (currentDir </> dir (Proxy :: _ "tmp") </> dir (Proxy :: _ "dir-entries-test"))

and it gives "/tmp/dir-entries-test/"

and I'm like "WHAAT, should be rel"

To Reproduce

add this to tests

  test "sandbox - Rel path, but should be abosolute?"
    ( let (relDir :: Maybe (SandboxedPath Rel Dir)) = sandbox rootDir (currentDir </> dirBar)
      in printPath posixPrinter <$> relDir)
    (Just "/bar/")

  test "sandbox - Rel path, but should be abosolute?"
    ( let (relDir :: Maybe (SandboxedPath Rel Dir)) = sandbox (rootDir </> dirBar) (currentDir </> dirBar)
      in printPath posixPrinter <$> relDir)
    (Just "/bar/bar/")

Expected behavior

maybe

data SandboxedPath a b = SandboxedPath (Path Abs Dir) (Path a b)

should be

data SandboxedPath dirOrFile = SandboxedPath (Path Abs Dir) (Path Abs dirOrFile)

srghma commented 1 month ago

or maybe this is ok, since I can write

sandboxOrThrow
  :: forall a b m
   . IsRelOrAbs a
  => IsDirOrFile b
  => MonadThrow Error m
  => Path Abs Dir
  -> Path a b
  -> m (SandboxedPath a b)
sandboxOrThrow root path = sandbox root path # maybe (throwError $ error $ "cannot sandbox path: root = " <> show root <> ", path = " <> show path) pure

(outerTmpDir :: SandboxedPath Rel Dir) <- sandboxOrThrow cwd (currentDir </> dir (Proxy :: _ "tmp") </> dir (Proxy :: _ "dir-entries-test"))

but on other hand - what is the use of currentDir inside of a sandbox method, maybe currentDir makes everything harder to understand

srghma commented 1 month ago

well, someone could write getFirstFile whose output form depends on the input

-- ./mydir/ -> ./mydir/myfile.txt
-- /myabspath/mydir/ -> /myabspath/mydir/myfile.txt
getFirstFile :: FilePath -> Aff FilePath
getFirstFile = ...

getFirstFileAff
  :: forall relOrAbs
   . IsRelOrAbs relOrAbs
  => SandboxedPath relOrAbs Dir
  -> Aff (Maybe (Path relOrAbs File))
getFirstFileAff path = do
  let printedPath = printPath currentPrinter path
  (maybeFile :: Maybe FilePath) <- Node.FS.getFirstFile path
  map (parse currentParser parseRelFile # ...) maybeFile

and this would be wrong - printPath will always return Abs Path

it can only ever be

getFirstFileAff
  :: forall relOrAbs
   . IsRelOrAbs relOrAbs
  => SandboxedPath relOrAbs Dir
  -> Aff (Maybe (Path Abs File))
srghma commented 1 month ago

Implemented, works ok, error is thrown now if I return Rel 2024-10-09-01pm-00-30-screenshot

also implemented a SafeAppend for Sandboxed (allows only adding new paths, disallows going up)

https://github.com/srghma/purescript-pathy/commit/5a747ea6a204ec332ce59966f4f94ff1ceb34dcd