diff --git a/dotnet/targets/Xamarin.Shared.Sdk.targets b/dotnet/targets/Xamarin.Shared.Sdk.targets
index 1b4f2b80ca74..c929ed3d04ae 100644
--- a/dotnet/targets/Xamarin.Shared.Sdk.targets
+++ b/dotnet/targets/Xamarin.Shared.Sdk.targets
@@ -582,7 +582,7 @@
DeploymentTarget=$(_MinimumOSVersion)
@(_CustomLinkFlags -> 'CustomLinkFlags=%(Identity)')
EnableSGenConc=$(EnableSGenConc)
- @(_BundlerEnvironmentVariables -> 'EnvironmentVariable=%(Identity)=%(Value)')
+ @(_BundlerEnvironmentVariables -> 'EnvironmentVariable=Overwrite=%(Overwrite)|%(Identity)=%(Value)')
@(_XamarinFrameworkAssemblies -> 'FrameworkAssembly=%(Filename)')
Interpreter=$(MtouchInterpreter)
IntermediateLinkDir=$(IntermediateLinkDir)
diff --git a/msbuild/Xamarin.Shared/Xamarin.Shared.props b/msbuild/Xamarin.Shared/Xamarin.Shared.props
index aff250fd1659..4b2c9b8173b7 100644
--- a/msbuild/Xamarin.Shared/Xamarin.Shared.props
+++ b/msbuild/Xamarin.Shared/Xamarin.Shared.props
@@ -235,9 +235,9 @@ Copyright (C) 2020 Microsoft. All rights reserved.
$(DiagnosticConfiguration),suspend
$(DiagnosticConfiguration),nosuspend
-
- $(AppBundleExtraOptions) --setenv=DOTNET_DiagnosticPorts=$(DiagnosticConfiguration)
-
+
+ <_BundlerEnvironmentVariables Include="DOTNET_DiagnosticPorts" Value="$(DiagnosticConfiguration)" Overwrite="false" />
+
$(IntermediateOutputPath)$(_PlatformName)
diff --git a/tools/common/Application.cs b/tools/common/Application.cs
index f8901565d8c9..d5baa01bcfbb 100644
--- a/tools/common/Application.cs
+++ b/tools/common/Application.cs
@@ -151,7 +151,7 @@ public bool AreAnyAssembliesTrimmed {
public bool EnableDiagnostics;
public bool? DebugTrack;
- public Dictionary EnvironmentVariables = new Dictionary ();
+ public Dictionary EnvironmentVariables = new Dictionary ();
public MarshalObjectiveCExceptionMode MarshalObjectiveCExceptions;
public MarshalManagedExceptionMode MarshalManagedExceptions;
diff --git a/tools/common/Target.cs b/tools/common/Target.cs
index def2a09e6bf1..4be7f632de6d 100644
--- a/tools/common/Target.cs
+++ b/tools/common/Target.cs
@@ -777,8 +777,12 @@ void GenerateIOSMain (StringWriter sw, Abi abi)
if (!string.IsNullOrEmpty (app.MonoGCParams))
sw.WriteLine ("\tsetenv (\"MONO_GC_PARAMS\", \"{0}\", 1);", app.MonoGCParams);
// Do this last, so that the app developer can override any other environment variable we set.
- foreach (var kvp in app.EnvironmentVariables)
- sw.WriteLine ("\tsetenv (\"{0}\", \"{1}\", 1);", kvp.Key.Replace ("\"", "\\\""), kvp.Value.Replace ("\"", "\\\""));
+ foreach (var kvp in app.EnvironmentVariables) {
+ var name = kvp.Key;
+ var value = kvp.Value.Value;
+ var overwrite = kvp.Value.Overwrite;
+ sw.WriteLine ("\tsetenv (\"{0}\", \"{1}\", {2});", name.Replace ("\"", "\\\""), value.Replace ("\"", "\\\""), overwrite ? 1 : 0);
+ }
if (app.XamarinRuntime != XamarinRuntime.NativeAOT)
sw.WriteLine ("\txamarin_supports_dynamic_registration = {0};", app.DynamicRegistrationSupported ? "TRUE" : "FALSE");
#if NET && !LEGACY_TOOLS
diff --git a/tools/dotnet-linker/LinkerConfiguration.cs b/tools/dotnet-linker/LinkerConfiguration.cs
index 37cf38c510d8..14424629f368 100644
--- a/tools/dotnet-linker/LinkerConfiguration.cs
+++ b/tools/dotnet-linker/LinkerConfiguration.cs
@@ -173,11 +173,23 @@ public static LinkerConfiguration GetInstance (LinkContext context)
Application.EnableSGenConc = string.Equals (value, "true", StringComparison.OrdinalIgnoreCase);
break;
case "EnvironmentVariable":
+ var overwrite = true;
+ var needle = "Overwrite=";
+ if (value.StartsWith (needle, StringComparison.Ordinal)) {
+ var pipe = value.IndexOf ('|', needle.Length);
+ if (pipe > 0) {
+ var overwriteString = value [needle.Length..pipe];
+ if (!TryParseOptionalBoolean (overwriteString, out var parsedOverwrite))
+ throw new InvalidOperationException ($"Unable to parse the 'Overwrite' value '{overwriteString}' for the environment variable entry '{value}' in {linker_file}");
+ overwrite = parsedOverwrite.Value;
+ value = value [(pipe + 1)..];
+ }
+ }
var separators = new char [] { ':', '=' };
var equals = value.IndexOfAny (separators);
var name = value.Substring (0, equals);
var val = value.Substring (equals + 1);
- Application.EnvironmentVariables.Add (name, val);
+ Application.EnvironmentVariables.Add (name, new (val, overwrite));
break;
case "FrameworkAssembly":
FrameworkAssemblies.Add (value);