Skip to content

Commit aad9803

Browse files
authored
[dotnet] Don't overwrite any existing value for DOTNET_DiagnosticPorts when computed through our build logic. (#23993)
Until now, people set the 'DOTNET_DiagnosticPorts' environment variable at launch/runtime to profile or trace an app. When we added support for setting specific MSBuild properties to enable diagnostics (DiagnosticsAddress, DiagnosticsSuspend, etc.), we end up unconditionally setting the DOTNET_DiagnosticPorts environment variable, effectively overwriting any value specified by customers when launching the app. This meant that existing (and fully functioning) profiling/tracing workflows would only work if they set the DOTNET_DiagnosticPorts variable to the defaults we have. Fix this by allowing overriding DOTNET_DiagnosticPorts variable at launch/runtime.
1 parent 28226b1 commit aad9803

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

dotnet/targets/Xamarin.Shared.Sdk.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@
590590
DeploymentTarget=$(_MinimumOSVersion)
591591
@(_CustomLinkFlags -> 'CustomLinkFlags=%(Identity)')
592592
EnableSGenConc=$(EnableSGenConc)
593-
@(_BundlerEnvironmentVariables -> 'EnvironmentVariable=%(Identity)=%(Value)')
593+
@(_BundlerEnvironmentVariables -> 'EnvironmentVariable=Overwrite=%(Overwrite)|%(Identity)=%(Value)')
594594
@(_XamarinFrameworkAssemblies -> 'FrameworkAssembly=%(Filename)')
595595
Interpreter=$(MtouchInterpreter)
596596
IntermediateLinkDir=$(IntermediateLinkDir)

msbuild/Xamarin.Shared/Xamarin.Shared.props

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,9 @@ Copyright (C) 2020 Microsoft. All rights reserved.
235235
<DiagnosticConfiguration Condition="'$(DiagnosticSuspend)' == 'true'">$(DiagnosticConfiguration),suspend</DiagnosticConfiguration>
236236
<DiagnosticConfiguration Condition="'$(DiagnosticSuspend)' != 'true'">$(DiagnosticConfiguration),nosuspend</DiagnosticConfiguration>
237237
</PropertyGroup>
238-
<PropertyGroup Condition="'$(EnableDiagnostics)' == 'true' And '$(DiagnosticConfiguration)' != '' And '$(DiagnosticConfiguration)' != 'disable'">
239-
<AppBundleExtraOptions>$(AppBundleExtraOptions) --setenv=DOTNET_DiagnosticPorts=$(DiagnosticConfiguration)</AppBundleExtraOptions>
240-
</PropertyGroup>
238+
<ItemGroup Condition="'$(EnableDiagnostics)' == 'true' And '$(DiagnosticConfiguration)' != '' And '$(DiagnosticConfiguration)' != 'disable'">
239+
<_BundlerEnvironmentVariables Include="DOTNET_DiagnosticPorts" Value="$(DiagnosticConfiguration)" Overwrite="false" />
240+
</ItemGroup>
241241

242242
<PropertyGroup Condition="'$(IsBindingProject)' == 'true'">
243243
<GeneratedSourcesDir Condition="'$(GeneratedSourcesDir)' == ''">$(IntermediateOutputPath)$(_PlatformName)</GeneratedSourcesDir>

tools/common/Application.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public bool AreAnyAssembliesTrimmed {
151151
public bool EnableDiagnostics;
152152
public bool? DebugTrack;
153153

154-
public Dictionary<string, string> EnvironmentVariables = new Dictionary<string, string> ();
154+
public Dictionary<string, (string Value, bool Overwrite)> EnvironmentVariables = new Dictionary<string, (string Value, bool Overwrite)> ();
155155

156156
public MarshalObjectiveCExceptionMode MarshalObjectiveCExceptions;
157157
public MarshalManagedExceptionMode MarshalManagedExceptions;

tools/common/Target.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,8 +777,12 @@ void GenerateIOSMain (StringWriter sw, Abi abi)
777777
if (!string.IsNullOrEmpty (app.MonoGCParams))
778778
sw.WriteLine ("\tsetenv (\"MONO_GC_PARAMS\", \"{0}\", 1);", app.MonoGCParams);
779779
// Do this last, so that the app developer can override any other environment variable we set.
780-
foreach (var kvp in app.EnvironmentVariables)
781-
sw.WriteLine ("\tsetenv (\"{0}\", \"{1}\", 1);", kvp.Key.Replace ("\"", "\\\""), kvp.Value.Replace ("\"", "\\\""));
780+
foreach (var kvp in app.EnvironmentVariables) {
781+
var name = kvp.Key;
782+
var value = kvp.Value.Value;
783+
var overwrite = kvp.Value.Overwrite;
784+
sw.WriteLine ("\tsetenv (\"{0}\", \"{1}\", {2});", name.Replace ("\"", "\\\""), value.Replace ("\"", "\\\""), overwrite ? 1 : 0);
785+
}
782786
if (app.XamarinRuntime != XamarinRuntime.NativeAOT)
783787
sw.WriteLine ("\txamarin_supports_dynamic_registration = {0};", app.DynamicRegistrationSupported ? "TRUE" : "FALSE");
784788
#if NET && !LEGACY_TOOLS

tools/dotnet-linker/LinkerConfiguration.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,23 @@ public static LinkerConfiguration GetInstance (LinkContext context)
173173
Application.EnableSGenConc = string.Equals (value, "true", StringComparison.OrdinalIgnoreCase);
174174
break;
175175
case "EnvironmentVariable":
176+
var overwrite = true;
177+
var needle = "Overwrite=";
178+
if (value.StartsWith (needle, StringComparison.Ordinal)) {
179+
var pipe = value.IndexOf ('|', needle.Length);
180+
if (pipe > 0) {
181+
var overwriteString = value [needle.Length..pipe];
182+
if (!TryParseOptionalBoolean (overwriteString, out var parsedOverwrite))
183+
throw new InvalidOperationException ($"Unable to parse the 'Overwrite' value '{overwriteString}' for the environment variable entry '{value}' in {linker_file}");
184+
overwrite = parsedOverwrite.Value;
185+
value = value [(pipe + 1)..];
186+
}
187+
}
176188
var separators = new char [] { ':', '=' };
177189
var equals = value.IndexOfAny (separators);
178190
var name = value.Substring (0, equals);
179191
var val = value.Substring (equals + 1);
180-
Application.EnvironmentVariables.Add (name, val);
192+
Application.EnvironmentVariables.Add (name, new (val, overwrite));
181193
break;
182194
case "FrameworkAssembly":
183195
FrameworkAssemblies.Add (value);

0 commit comments

Comments
 (0)