Env.ServerIsLocal() throws NullReferenceException when DEV env var is unset

Open Beginner friendly
#1,131 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
88/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
csharp

Research direction

Start in test/integration/helpers/Env.cs, read IsTrue and ServerIsLocal, then reproduce with env.json setting DEV to false and the DEV environment variable unset. Run the Android or iOS functional tests, and consider the issue done when an unset DEV value returns false without an exception and the affected fixtures complete setup.

Written by the indexing model from the issue text.

Description

Description

Env.ServerIsLocal() throws a NullReferenceException in CI (and locally, whenever env.json declares "DEV": "false" and the DEV environment variable isn't set), which fails OneTimeSetUp for every integration test fixture that relies on it.

Root cause

test/integration/helpers/Env.cs:

private static bool IsTrue(object val)
{
    val = val?.ToString().ToLowerInvariant().Trim();
    return val.Equals("true") || val.Equals("1");
}

public static bool ServerIsLocal()
{
    Init();
    return (_env.ContainsKey("DEV") && IsTrue(_env["DEV"])) || IsTrue(Environment.GetEnvironmentVariable("DEV"));
}

val?.ToString() correctly short-circuits to null when val is null, but the reassigned val is then used in val.Equals(...) unconditionally — so if it's still null, this throws NullReferenceException instead of returning false.

ServerIsLocal() hits this whenever:

  1. env.json's "DEV" value is falsy (e.g. "false", as set by CI in .github/workflows/functional-test.yml), so the left side of || is false, and
  2. the DEV environment variable is not set (the normal case in CI), so Environment.GetEnvironmentVariable("DEV") returns null and IsTrue(null) throws.

Impact

Every fixture that calls Env.ServerIsLocal() in its [OneTimeSetUp] fails immediately with NullReferenceException, failing all tests in that fixture. Confirmed in a recent CI run for both platforms, e.g.:

  • Android: Android/Device/App/AppTests.cs, Android/Device/NetworkTests.cs, Android/Device/PerformanceDataTests.cs, Android/Session/Logs/LogcatBroadcastTests.cs
  • iOS: IOS/Session/Logs/SyslogBroadcastTests.cs

Stack trace (identical shape on both platforms):

System.NullReferenceException : Object reference not set to an instance of an object.
   at Appium.Net.Integration.Tests.helpers.Env.IsTrue(Object val) in .../test/integration/helpers/Env.cs:line 53
   at Appium.Net.Integration.Tests.helpers.Env.ServerIsLocal() in .../test/integration/helpers/Env.cs:line 65
   at Appium.Net.Integration.Tests.Android.Device.App.AppTests.SetUp() in .../test/integration/Android/Device/App/AppTests.cs:line 21

Reference run: https://github.com/appium/dotnet-client/actions/runs/35200826871 (jobs android-tests and ios-tests).

Note: this bug was previously masked in CI because env.json never actually made it into the test output directory (a separate bug, fixed in #1130) — Env.Init()'s try/catch silently fell back to its default _env dict where DEV is true, so ServerIsLocal() short-circuited on the first operand and never reached the crashing branch. Now that env.json loads correctly, this latent bug surfaces.

Suggested fix

Guard against val being null after the null-conditional chain, e.g.:

private static bool IsTrue(object val)
{
    var str = val?.ToString()?.ToLowerInvariant().Trim();
    return str == "true" || str == "1";
}

Reproduction

Run the Android or iOS functional test suite in CI (or locally with env.json present and "DEV": "false", and no DEV environment variable set) — any fixture calling Env.ServerIsLocal() fails OneTimeSetUp.

Dominant language
C#
Stars
399
Forks
185
Avg merge
1d 16h
Merged PRs (30d)
13

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from appium/dotnet-client

All issues in appium/dotnet-client

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.