Repository navigation
fix: Map FileAccessMode to appropriate FileMode in StorageFile.OpenStreamAsync - #22796
Conversation
|
|
There was a problem hiding this comment.
Pull request overview
This PR adjusts Uno’s StorageFile stream-opening behavior so write access can create missing files (instead of failing with FileNotFoundException), and adds a runtime test to cover the scenario.
Changes:
- Introduce a
ToFileMode(FileAccessMode)helper to mapRead→FileMode.OpenandReadWrite→FileMode.OpenOrCreate. - Update iOS (security-scoped) and local
OpenStreamAsyncimplementations to use the newToFileModemapping. - Add a runtime test validating
OpenStreamForWriteAsync()on a path that doesn’t exist yet.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/Uno.UWP/Storage/StorageFile.iOS.cs |
Use FileMode.OpenOrCreate for ReadWrite stream opens via ToFileMode. |
src/Uno.UWP/Storage/StorageFile.cs |
Add ToFileMode(FileAccessMode) helper (shared mapping). |
src/Uno.UWP/Storage/StorageFile.Local.cs |
Use ToFileMode in local OpenStreamAsync to allow creating missing files on write. |
src/Uno.UI.RuntimeTests/Tests/Windows_Storage/Given_StorageFile_Native.base.cs |
Add runtime test for OpenStreamForWriteAsync() when the file doesn’t exist. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-22796/wasm-skia-net9/index.html |
|
The build 200351 found UI Test snapshots differences: Details
|
f8c0a80 to
e9f6c24
Compare
|
The build 200524 found UI Test snapshots differences: Details
|
ajpinedam
left a comment
There was a problem hiding this comment.
Change looks good.
The analyzer is complaining about non-nullable objects though:
Error: /home/runner/work/uno/uno/src/Uno.UI.RuntimeTests/Tests/Windows_Storage/Given_StorageFile_Native.base.cs(297,30): error CS8600: Converting null literal or possible null value to non-nullable type. [/home/runner/work/uno/uno/src/Uno.UI.RuntimeTests/Uno.UI.RuntimeTests.Skia.csproj::TargetFramework=net10.0]
Error: /home/runner/work/uno/uno/src/Uno.UI.RuntimeTests/Tests/Windows_Storage/Given_StorageFile_Native.base.cs(319,30): error CS8600: Converting null literal or possible null value to non-nullable type. [/home/runner/work/uno/uno/src/Uno.UI.RuntimeTests/Uno.UI.RuntimeTests.Skia.csproj::TargetFramework=net10.0]
e9f6c24 to
65f8cf1
Compare
|
@ajpinedam Thanks for the catch — fixed in 5d6ba7a by annotating |
Review comments addressed —
|
| Provider | OpenStreamAsync |
Path |
|---|---|---|
StorageFile.Local |
File.Open(Path, ToFileMode(...)) ← fixed here |
real filesystem path |
StorageFile.iOS (security-scoped) |
File.Open(Path, ToFileMode(...)) ← fixed here |
real filesystem path |
| WASM native (OPFS) | inherits base → OpenAsync(...).AsStream() — never calls ToFileMode |
synthetic (Path.Combine(parent.Path, name)) |
| Android SAF | inherits base → OpenAsync(...).AsStream() — never calls ToFileMode |
SAF document URI path |
So the two regression tests were asserting File.Open-semantics against providers that don't have them, using System.IO against paths that aren't real. They'd have failed spuriously on WASM native, and on Android SAF the OpenStreamForWriteAsync re-create simply doesn't happen (the SAF ContentResolver stream would throw instead).
Fix: a protected virtual bool HasFileSystemBackedPath => false; on the base class, overridden to true only by the Local and iOS security-scoped test classes. The two regression tests Assert.Inconclusive(...) on the other providers — matching the existing Assert.Inconclusive pattern already used in Given_StorageFile_Native.wasm.cs, and reported as TestResult.Inconclusive (not a failure) by UnitTestsControl.
Fixed (6)
| File:line | Reviewer point | What changed |
|---|---|---|
Given_StorageFile_Native.base.cs:364 (copilot) |
File.Delete/File.Exists on createdFile.Path isn't valid for providers with a synthetic path |
Tests now opt in via HasFileSystemBackedPath; only Local + iOS security-scoped run them, everything else is Inconclusive |
Given_StorageFile_Native.base.cs:374 (copilot) |
File.Exists assertion won't reflect existence for non-filesystem providers |
Same opt-in gate — the assertion now only runs where Path is a real filesystem path |
Given_StorageFile_Native.base.cs:362 (github-actions) |
System.IO in a base class shared with virtual-FS providers; prefer [PlatformCondition] over #if for attributes |
Both points taken: #if WINAPPSDK [Ignore] → [PlatformCondition(ConditionMode.Exclude, RuntimeTestPlatforms.NativeWinUI)], plus the per-provider gate. See note below on why I did not use RuntimeTestPlatforms.Wasm |
Given_StorageFile_Native.base.cs:349 (github-actions) |
Missing [GitHubWorkItem] for #21311 |
Added to When_OpenStreamForWriteAsync_FileDoesNotExist_Stream_CreatesFile |
Given_StorageFile_Native.base.cs:387 (github-actions) |
Missing [GitHubWorkItem] for #21311 |
Added to When_OpenStreamForReadAsync_FileDoesNotExist_Throws |
StorageFile.cs:227 (github-actions) |
7-line XML doc for a 3-line self-evident method | Trimmed to a 2-line <summary> keeping only the non-obvious bit (ReadWrite creates, Read requires existence) |
One deliberate deviation from the suggested fix
The ultrareview bot offered [PlatformCondition(ConditionMode.Exclude, RuntimeTestPlatforms.NativeWinUI | RuntimeTestPlatforms.Wasm)]. I used the platform condition for NativeWinUI but not the Wasm flag, because PlatformCondition is per-runtime-platform, not per-provider, and both Given_StorageFile_Native (OPFS) and Given_StorageFile_Native_Local (real emscripten FS) derive from this base and run on WASM:
- Excluding
Wasmwould also have killed the Local-provider coverage on WASM, which does exercise the fix. - It would still have left Android SAF broken, which the suggestion didn't account for.
The HasFileSystemBackedPath gate scopes precisely to the providers the fix applies to, which is what the Copilot threads asked for ("limiting these tests to providers where Path maps to a local filesystem path (Local + iOS security-scoped)").
Needs @MartinZikmund input (0)
Nothing — no thread was left open.
Head branch was pushed to by a user without write access
The CS8600 nullable warnings on lines 297 and 319 were already fixed (both |
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-22796/wasm-skia-net9/index.html |
|
The build 211139 found UI Test snapshots differences: Details
|
|
|
|
This PR is stale because it has been open 60 days with no activity. Remove stale label or comment or it will be closed in 10 days. |
…OpenStreamAsync Co-authored-by: MartinZikmund <[email protected]>
Co-authored-by: MartinZikmund <[email protected]>
Co-authored-by: MartinZikmund <[email protected]>
Co-Authored-By: Claude Opus 4.6 <[email protected]>
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Add When_OpenStreamForWriteAsync_FileDoesNotExist_Stream_CreatesFile (positive case: ReadWrite → FileMode.OpenOrCreate creates the file) and When_OpenStreamForReadAsync_FileDoesNotExist_Throws (negative case: Read → FileMode.Open still throws FileNotFoundException). Skipped on WINAPPSDK since native WinUI's StorageFile isn't subject to this fix. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…torageFile test Agent-Logs-Url: https://github.com/unoplatform/uno/sessions/9eae162b-7918-4ad0-93c9-c6e0861dfede Co-authored-by: MartinZikmund <[email protected]>
062a7a4 to
823db2e
Compare
|
Claude finished @MartinZikmund's task in 4m 14s —— View job Review complete
Overall verdict: fix-first (minor)The core fix is correct. Issues (3 inline comments)
What's correct
|
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-22796/wasm-skia-net9/index.html |
|
The build 221946 found UI Test snapshots differences: Details
|
|
|
…roviders The two #21311 regression tests manipulate the backing file through System.IO, which is only valid where StorageFile.Path is a real filesystem path (Local and iOS security-scoped). Browser OPFS and Android SAF expose a synthetic path and never route through ToFileMode, so they now opt out via HasFileSystemBackedPath. Also swaps the #if WINAPPSDK [Ignore] for [PlatformCondition] and adds the [GitHubWorkItem] annotations.
|
Claude finished @MartinZikmund's task in 3m 22s —— View job Review complete
Overall verdict: ship (minor)The fix is correct and complete.
1 inline comment (Low)
Cross-cutting notes (no specific line)
|
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-22796/wasm-skia-net9/index.html |
|
The build 222260 found UI Test snapshots differences: Details
|
closes #21311
StorageFile.Local.OpenStreamAsyncusesFileMode.Openinstead of mappingFileAccessModeto appropriateFileModeToFileModeto convertFileAccessModetoFileModeOpenStreamAsyncto use the correctFileModebased onFileAccessModeStorageFile.Local.cs(Skia, Desktop, etc.)StorageFile.iOS.cs(iOS security-scoped files)Original prompt
This section details on the original issue you should resolve
<issue_title>
StorageFile.Local.OpenStreamAsyncalways useFile.Openinstead of theFileAccessModeargument</issue_title><issue_description>### Current behavior 🐛
FileNotFoundExceptionthrown when trying to write new StorageFile in a Skia.Desktop Mac app.Expected behavior 🎯
No exception thrown with correct entitlements or choice of output folder
How to reproduce it (as minimally and precisely as possible) 🔬
Demo project: https://github.com/baskren/MacDesktop_StorageFile_Write
Relevant configurations:
entitlements.plist [CLICK HERE TO EXPAND]
.csproj, in the<Properties>group:Workaround 🛠️
None known at this time
Renderer 🎨
Affected platforms 📱💻🖥️
Desktop (macOS)
Uno.Sdk version (and other relevant versions) 📦
Uno.Sdk: 6.1.23
UnoFeatures:
IDE version 🧑💻
Click to expand [Mac Dev Environment]
JetBrains Rider
JetBrains Rider 2025.2
Build #RD-252.23892.524, built on August 12, 2025
Source revision: 18364647ddbb4
Licensed to Ben Askren
Subscription is active until July 10, 2026.
Runtime version: 21.0.7+6-b1038.58 aarch64 (JCEF 122.1.9)
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Toolkit: sun.lwawt.macosx.LWCToolkit
macOS 15.6
.NET Core v8.0.11 ARM64 (Server GC)
GC: G1 Young Generation, G1 Concurrent GC, G1 Old Generation
Memory: 4096M
Cores: 12
Metal Rendering is ON
Registry:
ide.lookup.shrink=true
ide.experimental.ui=true
llm.show.ai.promotion.window.on.start=false
Non-Bundled Plugins:
org.antlr.intellij.plugin (1.24)
com.intellij.resharper.HeapAllocationsViewer (2025.1.0)
com.intellij.copyright (252.23892.464)
com.redhat.devtools.lsp4ij (0.14.2)
lermitage.intellij.iconviewer (1.31.3)
com.github.camork.fileExpander (2.5)
com.wakatime.intellij.plugin (15.0.4)
com.cppcxy.Intellij-SumnekoLua (3.15.0.46-IDEA243)
com.jetbrains.rider.android (252.23892.524)
unoplatform (1.5.19)
Karma (252.25204.0)
me.rafaelldi.aspire (1.8.8)
com.intellij.resharper.azure (4.5.3)
Anything else we need to know? 💬
No response</issue_description>
Comments on the Issue (you are @copilot in this section)
@spouliot https://github.com/baskren/MacDesktop_StorageFile_Write is a 404. Is it public ? @spouliot Your `entitlements.plist` above does not match the one inside the repo.However that's not the problem. Entitlements are only needed if/when the app is packaged as an app bundle (and in the case of this entitlement the app would have to be sandboxed to need this).
For a dotnet app the file system is accessible with the same permission that the current user (launching the app) has.
The full exception is: