Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
58aeab6
Add telemetry logger support for API-based MSBuild usage
Copilot Sep 30, 2025
0b8fb6f
Add tests for ProjectInstanceExtensions telemetry logger creation
Copilot Sep 30, 2025
c37c61a
Fix telemetry logger to use distributed logging pattern with central …
Copilot Sep 30, 2025
1dc7f54
Fix ForwardingLoggerRecord to use central logger instance with forwar…
Copilot Sep 30, 2025
c201734
Share telemetry central logger instance between ProjectCollection and…
Copilot Sep 30, 2025
6d409d6
Add integration test for telemetry logger receiving events from API-b…
Copilot Sep 30, 2025
51f1399
Add telemetry logger support to VirtualProjectBuildingCommand
Copilot Oct 1, 2025
9343a69
Add telemetry logger support to PackageAddCommand for project evaluation
Copilot Oct 1, 2025
65f60f0
Add BannedApiAnalyzer to prevent direct MSBuild API usage without tel…
Copilot Nov 4, 2025
102371a
Scope BannedApiAnalyzer to src/Cli and fix all ProjectCollection viol…
Copilot Nov 4, 2025
3cd34a8
Use version property for BannedApiAnalyzers and update copilot-instru…
Copilot Nov 7, 2025
3093c60
Remove formatting-only changes to reduce PR noise
Copilot Nov 20, 2025
31e17b7
Restore BuildFinished event registration outside telemetry check
Copilot Nov 20, 2025
6eb6064
Revert accidental removal of functional code in TestCommand and telem…
Copilot Nov 20, 2025
243ad7f
react to changes post-rebase
baronfel Nov 21, 2025
151ef1a
Add unification plan document
baronfel Nov 23, 2025
bb53470
update local build instructions for copilot
baronfel Nov 23, 2025
ef5751b
big giant refactor towards more explicit types
baronfel Nov 24, 2025
5825147
Update some RunCommand infrastructure
baronfel Nov 25, 2025
6de6502
Tweak
baronfel Nov 25, 2025
ea3fc88
a lot more consolidation
baronfel Nov 25, 2025
ea27e91
finally plumb through centralizing on DotNetProject
baronfel Nov 29, 2025
4540b6a
update runcommand target framework selection to use evaluator
baronfel Nov 30, 2025
d3b9d3d
react to rebase
baronfel Dec 5, 2025
d2a96eb
prevent some duplicate loads when using the raw methods of the evaluator
baronfel Dec 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Revert accidental removal of functional code in TestCommand and telem…
…etry files

Co-authored-by: baronfel <[email protected]>
  • Loading branch information
Copilot and baronfel committed Dec 5, 2025
commit 6eb6064a87007f8d886b13b4e9a29e4df5751c47
52 changes: 35 additions & 17 deletions src/Cli/dotnet/Commands/Test/VSTest/TestCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.CommandLine;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Versioning;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -40,21 +41,41 @@ public static int Run(ParseResult parseResult)
VSTestTrace.SafeWriteTrace(() => $"Argument list: '{commandLineParameters}'");
}

// settings parameters are after -- (including --), these should not be considered by the parser
string[] settings = [.. args.SkipWhile(a => a != "--")];
// all parameters before --
args = [.. args.TakeWhile(a => a != "--")];
(args, string[] settings) = SeparateSettingsFromArgs(args);

// Fix for https://github.com/Microsoft/vstest/issues/1453
// Run dll/exe directly using the VSTestForwardingApp
if (ContainsBuiltTestSources(args))
// Note: ContainsBuiltTestSources need to know how many settings are there, to skip those from unmatched tokens
// When we don't have settings, we pass 0.
// When we have settings, we want to exclude the '--' as it doesn't end up in unmatched tokens, so we pass settings.Length - 1
if (ContainsBuiltTestSources(parseResult, GetSettingsCount(settings)))
{
return ForwardToVSTestConsole(parseResult, args, settings, testSessionCorrelationId);
}

return ForwardToMsbuild(parseResult, settings, testSessionCorrelationId);
}

internal /*internal for testing*/ static (string[] Args, string[] Settings) SeparateSettingsFromArgs(string[] args)
{
// settings parameters are after -- (including --), these should not be considered by the parser
string[] settings = [.. args.SkipWhile(a => a != "--")];
// all parameters before --
args = [.. args.TakeWhile(a => a != "--")];
return (args, settings);
}

internal /*internal for testing*/ static int GetSettingsCount(string[] settings)
{
if (settings.Length == 0)
{
return 0;
}

Debug.Assert(settings[0] == "--", "Settings should start with --");
return settings.Length - 1;
}

private static int ForwardToMsbuild(ParseResult parseResult, string[] settings, string testSessionCorrelationId)
{
// Workaround for https://github.com/Microsoft/vstest/issues/1503
Expand Down Expand Up @@ -154,7 +175,7 @@ private static int ForwardToVSTestConsole(ParseResult parseResult, string[] args

public static TestCommand FromArgs(string[] args, string? testSessionCorrelationId = null, string? msbuildPath = null)
{
var parseResult = Parser.Parse(["dotnet", "test", ..args]);
var parseResult = Parser.Parse(["dotnet", "test", .. args]);

// settings parameters are after -- (including --), these should not be considered by the parser
string[] settings = [.. args.SkipWhile(a => a != "--")];
Expand Down Expand Up @@ -237,9 +258,10 @@ private static TestCommand FromParseResult(ParseResult result, string[] settings
}
}


Dictionary<string, string> variables = VSTestForwardingApp.GetVSTestRootVariables();
foreach (var (rootVariableName, rootValue) in variables) {
foreach (var (rootVariableName, rootValue) in variables)
{
testCommand.EnvironmentVariable(rootVariableName, rootValue);
VSTestTrace.SafeWriteTrace(() => $"Root variable set {rootVariableName}:{rootValue}");
}
Expand Down Expand Up @@ -293,18 +315,14 @@ internal static int RunArtifactPostProcessingIfNeeded(string testSessionCorrelat
}
}

private static bool ContainsBuiltTestSources(string[] args)
internal /*internal for testing*/ static bool ContainsBuiltTestSources(ParseResult parseResult, int settingsLength)
{
for (int i = 0; i < args.Length; i++)
for (int i = 0; i < parseResult.UnmatchedTokens.Count - settingsLength; i++)
{
string arg = args[i];
if (arg.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) || arg.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
string arg = parseResult.UnmatchedTokens[i];
if (!arg.StartsWith("-") &&
(arg.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) || arg.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)))
{
var previousArg = i > 0 ? args[i - 1] : null;
if (previousArg != null && CommonOptions.PropertiesOption.Aliases.Contains(previousArg))
{
return false;
}
return true;
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/Cli/dotnet/Telemetry/EnvironmentDetectionRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.DotNet.Cli.Utils;

namespace Microsoft.DotNet.Cli.Telemetry;

Expand Down Expand Up @@ -33,8 +34,7 @@ public BooleanEnvironmentRule(params string[] variables)

public override bool IsMatch()
{
return _variables.Any(variable =>
bool.TryParse(Environment.GetEnvironmentVariable(variable), out bool value) && value);
return _variables.Any(variable => Env.GetEnvironmentVariableAsBool(variable));
}
}

Expand Down Expand Up @@ -81,12 +81,12 @@ public override bool IsMatch()
/// <typeparam name="T">The type of the result value.</typeparam>
internal class EnvironmentDetectionRuleWithResult<T> where T : class
{
private readonly string[] _variables;
private readonly EnvironmentDetectionRule _rule;
private readonly T _result;

public EnvironmentDetectionRuleWithResult(T result, params string[] variables)
public EnvironmentDetectionRuleWithResult(T result, EnvironmentDetectionRule rule)
{
_variables = variables ?? throw new ArgumentNullException(nameof(variables));
_rule = rule ?? throw new ArgumentNullException(nameof(rule));
_result = result ?? throw new ArgumentNullException(nameof(result));
}

Expand All @@ -96,8 +96,8 @@ public EnvironmentDetectionRuleWithResult(T result, params string[] variables)
/// <returns>The result value if the rule matches; otherwise, null.</returns>
public T? GetResult()
{
return _variables.Any(variable => !string.IsNullOrEmpty(Environment.GetEnvironmentVariable(variable)))
? _result
return _rule.IsMatch()
? _result
: null;
}
}
}
10 changes: 9 additions & 1 deletion src/Cli/dotnet/Telemetry/ILLMEnvironmentDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,13 @@ namespace Microsoft.DotNet.Cli.Telemetry;

internal interface ILLMEnvironmentDetector
{
/// <summary>
/// Checks the current environment for known indicators of LLM usage and returns a string identifying the LLM environment if detected.
/// </summary>
string? GetLLMEnvironment();
}

/// <summary>
/// Returns true if the current environment is detected to be an LLM/agentic environment, false otherwise.
/// </summary>
bool IsLLMEnvironment();
}
19 changes: 13 additions & 6 deletions src/Cli/dotnet/Telemetry/LLMEnvironmentDetectorForTelemetry.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Linq;

namespace Microsoft.DotNet.Cli.Telemetry;

internal class LLMEnvironmentDetectorForTelemetry : ILLMEnvironmentDetector
{
private static readonly EnvironmentDetectionRuleWithResult<string>[] _detectionRules = [
// Claude Code
new EnvironmentDetectionRuleWithResult<string>("claude", "CLAUDECODE"),
new EnvironmentDetectionRuleWithResult<string>("claude", new AnyPresentEnvironmentRule("CLAUDECODE")),
// Cursor AI
new EnvironmentDetectionRuleWithResult<string>("cursor", "CURSOR_EDITOR")
new EnvironmentDetectionRuleWithResult<string>("cursor", new AnyPresentEnvironmentRule("CURSOR_EDITOR")),
// Gemini
new EnvironmentDetectionRuleWithResult<string>("gemini", new BooleanEnvironmentRule("GEMINI_CLI")),
// GitHub Copilot
new EnvironmentDetectionRuleWithResult<string>("copilot", new BooleanEnvironmentRule("GITHUB_COPILOT_CLI_MODE")),
// (proposed) generic flag for Agentic usage
new EnvironmentDetectionRuleWithResult<string>("generic_agent", new BooleanEnvironmentRule("AGENT_CLI")),
];

/// <inheritdoc/>
public string? GetLLMEnvironment()
{
var results = _detectionRules.Select(r => r.GetResult()).Where(r => r != null).ToArray();
return results.Length > 0 ? string.Join(", ", results) : null;
}
}

/// <inheritdoc/>
public bool IsLLMEnvironment() => !string.IsNullOrEmpty(GetLLMEnvironment());
}