Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Uri] Absolute paths using UriKind.RelativeOrAbsolute parsed as relative on iOS and Android #69618

Closed
dotMorten opened this issue May 20, 2022 · 6 comments
Milestone

Comments

@dotMorten
Copy link

Description

Uri.TryCreate with UriKind.RelativeOrAbsolute fails to detect absolute paths on .NET 6 iOS and Android, whereas this worked with the legacy Xamarin mono runtimes.

Reproduction Steps

Run the code on both .NET 6 ios/android and legacy xamarin. Note that the asserts are very different for the code to work on both platforms.

#if __IOS__ || __ANDROID__
            if (System.Uri.TryCreate("/data/user/0/file.txt", System.UriKind.RelativeOrAbsolute, out System.Uri? result))
            {
#if NETCOREAPP
                System.Diagnostics.Debug.Assert(result.IsAbsoluteUri == false);
                try
                {
                    System.Diagnostics.Debug.Assert(result.IsFile == false); //Throws invalid operation
                    System.Diagnostics.Debug.Assert(false); // We don't get here
                }
                catch(System.InvalidOperationException)
                {
                }
#else // Legacy xamarin
                System.Diagnostics.Debug.Assert(result.IsAbsoluteUri == true);
                System.Diagnostics.Debug.Assert(result.IsFile == true);
#endif
            }
#endif

Expected behavior

IsAbsoluteUri and IsFile behavior matches legacy xamarin behavior.

Actual behavior

Incorrectly doesn't detect that the URL is absolute (ie starting with '/').

Regression?

Yes this worked correctly in Xamarin.

Known Workarounds

No response

Configuration

.NET 6.0.5, ARM64. iOS and Android.

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label May 20, 2022
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@dotMorten
Copy link
Author

dotMorten commented May 20, 2022

Found this reference regarding the difference: https://www.mono-project.com/docs/faq/known-issues/urikind-relativeorabsolute/
However this article discusses solving the opposite problem, when you want to read paths as relative when they are absolute

@dotMorten dotMorten changed the title UriKind.RelativeOrAbsolute parsed wrong on iOS and Android Absolute paths using UriKind.RelativeOrAbsolute parsed as relative on iOS and Android May 20, 2022
@jkotas jkotas changed the title Absolute paths using UriKind.RelativeOrAbsolute parsed as relative on iOS and Android [Uri] Absolute paths using UriKind.RelativeOrAbsolute parsed as relative on iOS and Android May 20, 2022
@ghost
Copy link

ghost commented May 20, 2022

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

Uri.TryCreate with UriKind.RelativeOrAbsolute fails to detect absolute paths on .NET 6 iOS and Android, whereas this worked with the legacy Xamarin mono runtimes.

Reproduction Steps

Run the code on both .NET 6 ios/android and legacy xamarin. Note that the asserts are very different for the code to work on both platforms.

#if __IOS__ || __ANDROID__
            if (System.Uri.TryCreate("/data/user/0/file.txt", System.UriKind.RelativeOrAbsolute, out System.Uri? result))
            {
#if NETCOREAPP
                System.Diagnostics.Debug.Assert(result.IsAbsoluteUri == false);
                try
                {
                    System.Diagnostics.Debug.Assert(result.IsFile == false); //Throws invalid operation
                    System.Diagnostics.Debug.Assert(false); // We don't get here
                }
                catch(System.InvalidOperationException)
                {
                }
#else // Legacy xamarin
                System.Diagnostics.Debug.Assert(result.IsAbsoluteUri == true);
                System.Diagnostics.Debug.Assert(result.IsFile == true);
#endif
            }
#endif

Expected behavior

IsAbsoluteUri and IsFile behavior matches legacy xamarin behavior.

Actual behavior

Incorrectly doesn't detect that the URL is absolute (ie starting with '/').

Regression?

Yes this worked correctly in Xamarin.

Known Workarounds

No response

Configuration

.NET 6.0.5, ARM64. iOS and Android.

Other information

No response

Author: dotMorten
Assignees: -
Labels:

area-System.Net, untriaged, area-CoreLib-mono

Milestone: -

@filipnavara
Copy link
Member

filipnavara commented May 20, 2022

This was always a difference between .NET [Core] and Mono. The problem is that the input is ambiguous, it can either be relative to server Uri (eg. /foo/bar relative to https://foo.com/example/ is https://foo.com/foo/bar) or an absolute path with implicit file:// scheme. Mono (& legacy Xamarin) chose to interpret it one way and .NET Core the other way. .NET 6 follows the .NET Core interpretation.

@MihaZupan
Copy link
Member

As Filip mentioned, Uri recognizes implicit file paths (without the file:// scheme prefix), and this behavior differs between platforms. The observed behavior is, unfortunately, by design.

We're solving the opposite problem by introducing a new API in .NET 7 (if we get around to implementing it) that will let you explicitly opt out of parsing implicit file paths (#59099), but there's no plan to make Unix absolute path parsing work on other platforms.

If at all possible, I would recommend using Path APIs when dealing with files.

@karelz
Copy link
Member

karelz commented May 26, 2022

Triage: Given that this is difference between Mono and .NET Core, there is not much we can do -- having unified ways across our platforms is somewhat useful.
If we see more people hitting it, we might want to call it out as breaking change in Xamarin/Mono.

@karelz karelz closed this as completed May 26, 2022
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label May 26, 2022
@karelz karelz added this to the 7.0.0 milestone May 26, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Jun 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants