Merge pull request #13794 from github/mbg/csharp/improve-tracer-command-detection

C#: Limit detection of sub-command names in tracer configuration
This commit is contained in:
Michael B. Gale
2023-07-25 17:01:22 +01:00
committed by GitHub
4 changed files with 47 additions and 15 deletions

View File

@@ -0,0 +1 @@
Console.WriteLine(args[0]);

View File

@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,9 @@
from create_database_utils import *
from diagnostics_test_utils import *
# the tracer configuration should not inject the extra command-line arguments for these commands
# and they should therefore run successfully
run_codeql_database_init(lang="csharp")
# this command fails on Windows for some reason, so we comment it out for now
# run_codeql_database_trace_command(['dotnet', 'tool', 'search', 'publish'])
run_codeql_database_trace_command(['dotnet', 'new', 'console', '--force', '--name', 'build', '--output', '.'])

View File

@@ -24,6 +24,10 @@ function RegisterExtractorPack(id)
local testMatch = false
local dotnetRunNeedsSeparator = false;
local dotnetRunInjectionIndex = nil;
-- A flag indicating whether we are in a position where we expect a sub-command such as `build`.
-- Once we have found one, we set this to `false` to not accidentally pick up on things that
-- look like sub-command names later on in the argument vector.
local inSubCommandPosition = true;
local argv = compilerArguments.argv
if OperatingSystem == 'windows' then
-- let's hope that this split matches the escaping rules `dotnet` applies to command line arguments
@@ -35,30 +39,38 @@ function RegisterExtractorPack(id)
-- dotnet options start with either - or / (both are legal)
local firstCharacter = string.sub(arg, 1, 1)
if not (firstCharacter == '-') and not (firstCharacter == '/') then
if (not match) then
if (not match) and inSubCommandPosition then
Log(1, 'Dotnet subcommand detected: %s', arg)
end
if arg == 'build' or arg == 'msbuild' or arg == 'publish' or arg == 'pack' then
match = true
break
end
if arg == 'run' then
-- for `dotnet run`, we need to make sure that `-p:UseSharedCompilation=false` is
-- not passed in as an argument to the program that is run
match = true
dotnetRunNeedsSeparator = true
dotnetRunInjectionIndex = i + 1
end
if arg == 'test' then
match = true
testMatch = true
-- only respond to strings that look like sub-command names if we have not yet
-- encountered something that looks like a sub-command
if inSubCommandPosition then
if arg == 'build' or arg == 'msbuild' or arg == 'publish' or arg == 'pack' then
match = true
break
end
if arg == 'run' then
-- for `dotnet run`, we need to make sure that `-p:UseSharedCompilation=false` is
-- not passed in as an argument to the program that is run
match = true
dotnetRunNeedsSeparator = true
dotnetRunInjectionIndex = i + 1
end
if arg == 'test' then
match = true
testMatch = true
end
end
-- for `dotnet test`, we should not append `-p:UseSharedCompilation=false` to the command line
-- if an `exe` or `dll` is passed as an argument as the call is forwarded to vstest.
if testMatch and (arg:match('%.exe$') or arg:match('%.dll')) then
match = false
break
end
-- we have found a sub-command, ignore all strings that look like sub-command names from now on
inSubCommandPosition = false
end
-- if we see a separator to `dotnet run`, inject just prior to the existing separator
if arg == '--' then