Commit 546eafa
authored
fix(islands): propagate notebook filename through MarimoIslandGenerator.from_file (#9409)
## Summary
Closes #9391
`MarimoIslandGenerator.from_file(path)` previously discarded the source
path. `build()` then constructed `AppFileManager` with `filename=None`,
the kernel inherited `app_metadata.filename = None`, and
`create_main_module(file=None)` fell through to
`sys.modules['__main__'].__file__` — i.e. the host process's launcher
script. Cells inside notebooks rendered through islands therefore saw
`__file__` (and `mo.notebook_dir()`) resolve to the calling script (or
to the installed CLI's `.venv/bin/<entry>` shim) rather than to the
notebook source. `marimo edit` and `marimo export` were unaffected; only
the islands code path was broken.
The fix threads the source path through:
- `MarimoIslandGenerator.__init__` gains a `_source_filename` field
(defaults to `None`, so manual `MarimoIslandGenerator() + add_code()`
flows are unchanged).
- `from_file()` records the path on the instance.
- `build()` passes it to `AppFileManager.from_app(self._app,
filename=…)`.
- `AppFileManager.from_app` accepts a new optional `filename` kwarg and
assigns `_filename` directly. All existing single-arg callers (29 in
tests + 1 here) continue to work.
`run_app_until_completion` already populates
`AppMetadata(filename=file_manager.path, …)`, so once the file manager
knows the path the existing kernel / `patch_main_module` plumbing
delivers the correct `__file__` — no further changes in `_runtime/`.
### Before / after (issue's exact repro)
`/tmp/repro/notebooks/nb.py` prints `__file__` and `mo.notebook_dir()`
from a cell. `/tmp/repro/scripts/render.py` calls
`MarimoIslandGenerator.from_file(...)`.
Before:
```
__file__ = '/tmp/repro/scripts/render.py'
notebook_dir() = PosixPath('/tmp/repro/scripts')
```
After:
```
__file__ = '/tmp/repro/notebooks/nb.py'
notebook_dir() = PosixPath('/tmp/repro/notebooks')
```
### Note on backward compatibility
This restores the documented / expected semantics, but it **is** a
behavior change for any island user who relied on `__file__` resolving
to the host process. @mscolnick noted in
[#9391](#9391 (comment))
that this may warrant a breaking-change classification — happy to take
labeling / release-note guidance.
## Pre-Review Checklist
- [x] For large changes, or changes that affect the public API: this
change was discussed or approved through an issue — see @mscolnick's
comment on #9391: *"this does look like a real issue. we'd gladly accept
the contribution to fix this."*
- [x] Any AI generated code has been reviewed line-by-line by the human
PR author, who stands by it.
- [ ] Video or media evidence is provided for any visual changes
(optional). — N/A, no visual change.
## Merge Checklist
- [x] I have read the [contributor
guidelines](https://github.com/marimo-team/marimo/blob/main/CONTRIBUTING.md).
- [x] Documentation has been updated where applicable, including
docstrings for API changes. — `AppFileManager.from_app` docstring
updated to document the new `filename` parameter.
- [x] Tests have been added for the changes made. —
`tests/_islands/test_island_generator.py::test_from_file_propagates_filename_to_cells`.
## Verification
- `pytest tests/_islands/` — 11 passed (10 existing + 1 new regression
test)
- `pytest tests/_server/test_file_manager.py
tests/_server/test_sessions.py tests/_server/export/test_exporter.py` —
81 passed (the 15 deselected `test_export_html*` failures are
pre-existing on `main` due to no `make fe` artifact and unrelated to
this change)
- `ruff check` / `ruff format --check` — clean on changed files
- `mypy marimo --exclude=marimo/_tutorials/` — same 14 pre-existing
errors as `main`; zero new errors introduced1 parent a338d3c commit 546eafa
4 files changed
Lines changed: 143 additions & 2 deletions
File tree
- marimo
- _islands
- _session/notebook
- tests
- _islands
- _server
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
249 | 250 | | |
250 | 251 | | |
251 | 252 | | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
252 | 257 | | |
253 | 258 | | |
254 | 259 | | |
| |||
267 | 272 | | |
268 | 273 | | |
269 | 274 | | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
270 | 279 | | |
271 | 280 | | |
272 | 281 | | |
| |||
338 | 347 | | |
339 | 348 | | |
340 | 349 | | |
341 | | - | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
342 | 353 | | |
343 | 354 | | |
344 | 355 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
105 | 105 | | |
106 | 106 | | |
107 | 107 | | |
108 | | - | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
109 | 112 | | |
110 | 113 | | |
111 | 114 | | |
112 | 115 | | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
113 | 120 | | |
114 | 121 | | |
115 | 122 | | |
116 | 123 | | |
117 | 124 | | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
118 | 130 | | |
119 | 131 | | |
120 | 132 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
151 | 151 | | |
152 | 152 | | |
153 | 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 | + | |
154 | 246 | | |
155 | 247 | | |
156 | 248 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 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 | + | |
0 commit comments