|
| 1 | +# Migration Guide: Version 0.4.0 |
| 2 | + |
| 3 | +## Summary |
| 4 | + |
| 5 | +Version 0.4.0 makes `Relative_path.t` reject paths that escape above their starting point (paths with leading `..` segments after normalization). |
| 6 | + |
| 7 | +## Breaking Changes |
| 8 | + |
| 9 | +### Construction Functions |
| 10 | + |
| 11 | +All construction functions (`v`, `of_string`, `of_fpath`) now reject escaping paths: |
| 12 | + |
| 13 | +```ocaml |
| 14 | +# Relative_path.v "../config" ;; |
| 15 | +Exception: |
| 16 | +Invalid_argument |
| 17 | + "Relative_path.v: path \"../config\" escapes above starting point". |
| 18 | +``` |
| 19 | + |
| 20 | +**Migration options:** |
| 21 | + |
| 22 | +Use `Absolute_path.t` for explicit paths: |
| 23 | +```ocaml |
| 24 | +# let path = Absolute_path.v "/path/to/parent/config" ;; |
| 25 | +val path : Absolute_path.t = <abstr> |
| 26 | +``` |
| 27 | + |
| 28 | +Or use `Fpath.t` for paths that may escape: |
| 29 | +```ocaml |
| 30 | +# let path : Fpath.t = Fpath.v "../config" |> Fpath.normalize ;; |
| 31 | +val path : Fpath.t = <abstr> |
| 32 | +``` |
| 33 | + |
| 34 | +### Parent Function |
| 35 | + |
| 36 | +Returns `None` for the empty path (previously returned `"../"`): |
| 37 | + |
| 38 | +```ocaml |
| 39 | +# Relative_path.parent Relative_path.empty ;; |
| 40 | +- : Relative_path.t option = None |
| 41 | +``` |
| 42 | + |
| 43 | +This fixes infinite loops in upward navigation: |
| 44 | + |
| 45 | +```ocaml |
| 46 | +# let rec navigate_to_root path = |
| 47 | + match Relative_path.parent path with |
| 48 | + | None -> path |
| 49 | + | Some p -> navigate_to_root p ;; |
| 50 | +val navigate_to_root : Relative_path.t -> Relative_path.t = <fun> |
| 51 | +# Relative_path.to_string (navigate_to_root (Relative_path.v "a/b/c")) ;; |
| 52 | +- : string = "./" |
| 53 | +``` |
| 54 | + |
| 55 | +### Extend Function |
| 56 | + |
| 57 | +Raises `Invalid_argument` if extending creates an escaping path: |
| 58 | + |
| 59 | +```ocaml |
| 60 | +# Relative_path.extend Relative_path.empty (Fsegment.v "..") ;; |
| 61 | +Exception: |
| 62 | +Invalid_argument |
| 63 | + "Relative_path.extend: path \"./..\" escapes above starting point". |
| 64 | +``` |
| 65 | + |
| 66 | +**Migration:** Use `Fpath.t` if segments might create escaping paths. |
| 67 | + |
| 68 | +### Chop Prefix/Suffix |
| 69 | + |
| 70 | +Empty prefix/suffix now returns `Some path` (previously `None`): |
| 71 | + |
| 72 | +```ocaml |
| 73 | +# match Relative_path.chop_prefix (Relative_path.v "foo/bar") ~prefix:Relative_path.empty with |
| 74 | + | None -> "no match" |
| 75 | + | Some p -> Relative_path.to_string p ;; |
| 76 | +- : string = "foo/bar" |
| 77 | +``` |
| 78 | + |
| 79 | +## Common Migration Patterns |
| 80 | + |
| 81 | +### Dynamic Path Construction |
| 82 | + |
| 83 | +Validate paths and handle rejections: |
| 84 | + |
| 85 | +```ocaml |
| 86 | +# let load_relative_file filename = |
| 87 | + match Relative_path.of_string filename with |
| 88 | + | Error (`Msg err) -> Error err |
| 89 | + | Ok path -> Ok path ;; |
| 90 | +val load_relative_file : string -> (Relative_path.t, string) result = <fun> |
| 91 | +# load_relative_file "config/settings.conf" ;; |
| 92 | +- : (Relative_path.t, string) result = Ok <abstr> |
| 93 | +``` |
| 94 | + |
| 95 | +### Upward Navigation |
| 96 | + |
| 97 | +Use absolute paths for upward traversal: |
| 98 | + |
| 99 | +```ocaml |
| 100 | +# let find_project_root has_marker current_path = |
| 101 | + let rec search path = |
| 102 | + if has_marker path then Some path |
| 103 | + else |
| 104 | + match Absolute_path.parent path with |
| 105 | + | None -> None |
| 106 | + | Some parent -> search parent |
| 107 | + in |
| 108 | + search current_path ;; |
| 109 | +val find_project_root : |
| 110 | + (Absolute_path.t -> bool) -> Absolute_path.t -> Absolute_path.t option = |
| 111 | + <fun> |
| 112 | +``` |
| 113 | + |
| 114 | +## Why These Changes |
| 115 | + |
| 116 | +1. **Improve type safety** - `Relative_path.t` guarantees non-escaping |
| 117 | +2. **Less error-prone APIs** for sandbox operations and recursive parent traversal |
| 118 | + |
| 119 | +See [Path Normalization](../explanation/path-normalization.md) for more details. |
0 commit comments