1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//! `data_root()` — single accessor for the user-scoped mnml data
//! directory. Task #858 phase A.
//!
//! Historically ~25 call sites read `HOME` directly and appended
//! `.config/mnml/…`. That worked but blocked two features:
//!
//! 1. **Portable mode** — a "no HOME footprint" layout that keeps
//! every user-scoped file next to the mnml binary in a
//! `mnml-data/` folder. Wanted for USB sticks, restricted-HOME
//! Windows setups, "try before you install" runs, and pinned
//! per-version data with a portable release.
//! 2. **Sandbox mode** — the existing `--sandbox` flag redirects
//! HOME to a tempdir so the whole surface (config, glyphs,
//! sessions, marketplace cache) is throwaway. That path is
//! already respected by every call site because they all read
//! HOME — but the abstraction was ad-hoc.
//!
//! This module centralizes the resolution. Precedence:
//!
//! - **Portable, opted-in** — if `<binary_dir>/mnml-data/` exists
//! AND contains a `.opted-in` file, use it. The two-file gate
//! prevents an accidentally-named `mnml-data/` folder from
//! silently redirecting a user's data. Phase C's welcome UI
//! creates the marker on user consent; power-users can `touch`
//! it manually for headless setups.
//! - **Portable, awaiting consent** — folder exists but no
//! `.opted-in`. Reported via [`portable_state`] so the welcome
//! UI can default its choice to Portable, but resolution stays
//! on Home until the user consents.
//! - **Otherwise** — `$HOME/.config/mnml/`. Sandbox mode's HOME
//! redirect is invisible here — it just changes what `HOME`
//! resolves to, which is exactly the point.
//!
//! Every downstream helper (config dir, integrations dir, glyphs
//! dir, marketplace cache path, welcome marker) sits on top of
//! `data_root()`. Adding a new user-scoped file? Route through the
//! helper, not `env::var_os("HOME")`.
use ;
use OnceLock;
/// Which layout mnml is running in this process. Reported by
/// `/version` / About / troubleshooting reports so a user copying
/// a bug in can tell us which store their files are in.
/// Cache the fact that we are (or aren't) in portable mode. The
/// binary can't move mid-process, so this is a fine one-shot
/// probe. We deliberately do NOT cache the resolved path — HOME
/// can change mid-process (test env swaps + `--sandbox` mode
/// after startup), and re-reading `env::var_os("HOME")` per call
/// is a cheap HashMap lookup.
static PORTABLE_CACHE: = new;
/// Return the absolute path to the user-scoped mnml data root for
/// this process. Never panics — falls back to `./mnml` if HOME is
/// unset AND no portable marker exists (a broken environment;
/// safer to use CWD than to bail).
///
/// Resolution order:
/// 1. Portable mode (`<binary>/mnml-data/.opted-in`)
/// 2. `$XDG_CONFIG_HOME/mnml/` — XDG-conforming installs.
/// 2026-08-04: added because `config::home_config_path` already
/// consulted XDG but data_root didn't, which split config into
/// one dir and integrations/glyphs into another when both env
/// vars were set (sandbox mode set both → the bug). Making
/// data_root respect XDG means everything lives in one place
/// regardless of layout.
/// 3. `$HOME/.config/mnml/` — standard fallback.
/// 4. `./mnml` — degenerate, no HOME.
/// Which of the two layouts we resolved to. Cheap wrapper around
/// [`is_portable`] — the reported kind can't change once the
/// portable-cache has been probed.
/// Short human-readable label for the current layout. Used by
/// `/version` and About. Not localized.
/// Detect the current binary's containing directory (via
/// `env::current_exe`). Returns None if the OS can't tell us or
/// if the exe path has no parent. Callers use this to place
/// `mnml-data/` for portable-mode probing AND for the welcome
/// flow's "create portable folder here" action.
/// Portable marker path (`<binary_dir>/mnml-data/`) IF the binary
/// directory can be resolved. Just constructs the path — does not
/// check whether it exists.
/// Filename inside the portable folder that gates activation. The
/// welcome UI (Phase C) creates this on user consent; power-users
/// can `touch` it manually for headless setups. Empty file — its
/// presence is the whole signal.
pub const PORTABLE_OPT_IN_FILENAME: &str = ".opted-in";
/// Reported to the welcome UI so it can shape the first-run
/// choice: default to Portable when the folder is already there,
/// default to Normal otherwise.
/// Per-user "the user has picked a data layout" marker. Distinct
/// from the per-workspace `.mnml/.welcomed` — that one gates the
/// tips overlay in each new workspace; this one gates the
/// Portable-vs-Normal choice at first-ever launch.
///
/// Lives inside [`data_root`], so:
/// - Portable install: `<binary_dir>/mnml-data/.user-welcomed`
/// - Normal install: `~/.config/mnml/.user-welcomed`
pub const USER_CHOICE_MARKER_FILENAME: &str = ".user-welcomed";
/// Absolute path to the per-user first-run marker for the current
/// data root. Cheap wrapper — path only, does not check existence.
/// True when the per-user first-run marker exists (either mode).
/// The overlay uses this to decide whether to prompt at startup.
/// Drop the per-user marker in the currently-resolved data root.
/// Used by the "Normal install" branch of the choice overlay.
/// Best-effort — a filesystem failure isn't fatal to the app, just
/// means the overlay may reappear next launch.
/// Materialize a portable install: create `<binary_dir>/mnml-data/`
/// if missing, drop the `.opted-in` gate, and drop the per-user
/// marker so the choice sticks. Returns the mnml-data path on
/// success. Caller should then request a restart — the process
/// has cached `PORTABLE_CACHE` and won't see the new layout until
/// re-exec.
/// Report the portable-mode state at the current binary
/// location. Non-cached — cheap enough to call on demand, and the
/// welcome UI needs the live answer (creating the marker mid-run
/// should be immediately observable).