Skip to content

[nativeaot] set $HOME and $TMPDIR #9889

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

Merged
merged 1 commit into from
Mar 7, 2025
Merged

Conversation

jonathanpeppers
Copy link
Member

Context: https://github.com/dotnet/runtime/blob/d09a42b9149e14a37c8ef8363cdb7e17bb922c37/src/libraries/System.Private.CoreLib/src/System/IO/Path.Unix.cs#L83

System.IO.Path.GetTempPath() doesn't work under NativeAOT as seen by the test failure:

Mono.Android.NET_Tests, Android.RuntimeTests.InputStreamInvokerTest.InputStreamTest / Release
System.IO.DirectoryNotFoundException : IO_PathNotFound_Path, /tmp/
at Interop.ThrowExceptionForIoErrno(Interop.ErrorInfo, String, Boolean) + 0x11
at Interop.CheckIo(Int64, String, Boolean) + 0x5d
at System.IO.Path.GetTempFileName() + 0x14d
at Android.RuntimeTests.InputStreamInvokerTest.InputStreamTest() + 0x18
at libMono.Android.NET-Tests!<BaseAddress>+0x1483023
at System.Reflection.DynamicInvokeInfo.Invoke(Object, IntPtr, Object[], BinderBundle, Boolean) + 0xf3

On Unix-like systems, the BCL just reads $TMPDIR and falls back to /tmp/ if it doesn't exist. $TMPDIR is not set by default in Android applications.

To fix this, we can set $TMPDIR to context.getCacheDir() just as we do in Mono:

String filesDir = context.getFilesDir ().getAbsolutePath ();
String cacheDir = context.getCacheDir ().getAbsolutePath ();

set_environment_variable_for_directory ("TMPDIR", applicationDirs[SharedConstants::APP_DIRS_CACHE_DIR_INDEX]);
set_environment_variable_for_directory ("HOME", home);

I think we can simply read these values in Java at startup, and call Os.setenv() to set them. There does not seem to be a need to pass these paths into managed code.

The Android.RuntimeTests.InputStreamInvokerTest.InputStreamTest() test now passes with these changes.

I fixed $HOME as well, as it will be used by other APIs in the BCL.

Context: https://github.com/dotnet/runtime/blob/d09a42b9149e14a37c8ef8363cdb7e17bb922c37/src/libraries/System.Private.CoreLib/src/System/IO/Path.Unix.cs#L83

`System.IO.Path.GetTempPath()` doesn't work under NativeAOT as seen by
the test failure:

    Mono.Android.NET_Tests, Android.RuntimeTests.InputStreamInvokerTest.InputStreamTest / Release
    System.IO.DirectoryNotFoundException : IO_PathNotFound_Path, /tmp/
    at Interop.ThrowExceptionForIoErrno(Interop.ErrorInfo, String, Boolean) + 0x11
    at Interop.CheckIo(Int64, String, Boolean) + 0x5d
    at System.IO.Path.GetTempFileName() + 0x14d
    at Android.RuntimeTests.InputStreamInvokerTest.InputStreamTest() + 0x18
    at libMono.Android.NET-Tests!<BaseAddress>+0x1483023
    at System.Reflection.DynamicInvokeInfo.Invoke(Object, IntPtr, Object[], BinderBundle, Boolean) + 0xf3

On Unix-like systems, the BCL just reads `$TMPDIR` and falls back to
`/tmp/` if it doesn't exist. `$TMPDIR` is *not* set by default in
Android applications.

To fix this, we can set `$TMPDIR` to `context.getCacheDir()` just as
we do in Mono:

https://github.com/dotnet/android/blob/04cd6f40989a8fad30d9a9759aec02d540cc9478/src/java-runtime/java/mono/android/MonoPackageManager.java#L53-L54

https://github.com/dotnet/android/blob/04cd6f40989a8fad30d9a9759aec02d540cc9478/src/native/mono/monodroid/monodroid-glue.cc#L1444-L1445

I think we can simply read these values in Java at startup, and call
`Os.setenv()` to set them. There does not seem to be a need to pass
these paths into managed code.

The `Android.RuntimeTests.InputStreamInvokerTest.InputStreamTest()`
test now passes with these changes.

I fixed `$HOME` as well, as it will be used by other APIs in the BCL.
@jonathanpeppers
Copy link
Member Author

@jonathanpeppers jonathanpeppers marked this pull request as ready for review March 6, 2025 21:50
@jonpryor jonpryor merged commit 31fe91d into main Mar 7, 2025
58 checks passed
@jonpryor jonpryor deleted the dev/peppers/nativeaottmpdir branch March 7, 2025 19:17
@github-actions github-actions bot locked and limited conversation to collaborators Apr 7, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants