Skip to content

Commit aa59741

Browse files
authored
Merge pull request github#13794 from github/mbg/csharp/improve-tracer-command-detection
C#: Limit detection of sub-command names in tracer configuration
2 parents 532552a + f3c6564 commit aa59741

File tree

4 files changed

+47
-15
lines changed

4 files changed

+47
-15
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Console.WriteLine(args[0]);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net7.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from create_database_utils import *
2+
from diagnostics_test_utils import *
3+
4+
# the tracer configuration should not inject the extra command-line arguments for these commands
5+
# and they should therefore run successfully
6+
run_codeql_database_init(lang="csharp")
7+
# this command fails on Windows for some reason, so we comment it out for now
8+
# run_codeql_database_trace_command(['dotnet', 'tool', 'search', 'publish'])
9+
run_codeql_database_trace_command(['dotnet', 'new', 'console', '--force', '--name', 'build', '--output', '.'])

csharp/tools/tracing-config.lua

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ function RegisterExtractorPack(id)
2424
local testMatch = false
2525
local dotnetRunNeedsSeparator = false;
2626
local dotnetRunInjectionIndex = nil;
27+
-- A flag indicating whether we are in a position where we expect a sub-command such as `build`.
28+
-- Once we have found one, we set this to `false` to not accidentally pick up on things that
29+
-- look like sub-command names later on in the argument vector.
30+
local inSubCommandPosition = true;
2731
local argv = compilerArguments.argv
2832
if OperatingSystem == 'windows' then
2933
-- let's hope that this split matches the escaping rules `dotnet` applies to command line arguments
@@ -35,30 +39,38 @@ function RegisterExtractorPack(id)
3539
-- dotnet options start with either - or / (both are legal)
3640
local firstCharacter = string.sub(arg, 1, 1)
3741
if not (firstCharacter == '-') and not (firstCharacter == '/') then
38-
if (not match) then
42+
if (not match) and inSubCommandPosition then
3943
Log(1, 'Dotnet subcommand detected: %s', arg)
4044
end
41-
if arg == 'build' or arg == 'msbuild' or arg == 'publish' or arg == 'pack' then
42-
match = true
43-
break
44-
end
45-
if arg == 'run' then
46-
-- for `dotnet run`, we need to make sure that `-p:UseSharedCompilation=false` is
47-
-- not passed in as an argument to the program that is run
48-
match = true
49-
dotnetRunNeedsSeparator = true
50-
dotnetRunInjectionIndex = i + 1
51-
end
52-
if arg == 'test' then
53-
match = true
54-
testMatch = true
45+
-- only respond to strings that look like sub-command names if we have not yet
46+
-- encountered something that looks like a sub-command
47+
if inSubCommandPosition then
48+
if arg == 'build' or arg == 'msbuild' or arg == 'publish' or arg == 'pack' then
49+
match = true
50+
break
51+
end
52+
if arg == 'run' then
53+
-- for `dotnet run`, we need to make sure that `-p:UseSharedCompilation=false` is
54+
-- not passed in as an argument to the program that is run
55+
match = true
56+
dotnetRunNeedsSeparator = true
57+
dotnetRunInjectionIndex = i + 1
58+
end
59+
if arg == 'test' then
60+
match = true
61+
testMatch = true
62+
end
5563
end
64+
5665
-- for `dotnet test`, we should not append `-p:UseSharedCompilation=false` to the command line
5766
-- if an `exe` or `dll` is passed as an argument as the call is forwarded to vstest.
5867
if testMatch and (arg:match('%.exe$') or arg:match('%.dll')) then
5968
match = false
6069
break
6170
end
71+
72+
-- we have found a sub-command, ignore all strings that look like sub-command names from now on
73+
inSubCommandPosition = false
6274
end
6375
-- if we see a separator to `dotnet run`, inject just prior to the existing separator
6476
if arg == '--' then

0 commit comments

Comments
 (0)