Skip to content

Commit 321c858

Browse files
authored
Merge pull request #10667 from hvitved/csharp/dotnet-run-tracer-fix2
C#: Recognize options to `dotnet run` in tracer when injecting `-p:UseSharedCompilation=false`
2 parents c1c16e4 + 76abf6f commit 321c858

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

csharp/ql/integration-tests/all-platforms/dotnet_run/test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,7 @@ def check_build_out(msg, s):
4141
# to `dotnet run -p:UseSharedCompilation=true -p:UseSharedCompilation=false -- hello world`
4242
s = run_codeql_database_create_stdout(['dotnet clean', 'rm -rf test6-db', 'dotnet run -p:UseSharedCompilation=true -- hello world'], "test7-db")
4343
check_build_out("hello, world", s)
44+
45+
# option passed into `dotnet run`
46+
s = run_codeql_database_create_stdout(['dotnet clean', 'rm -rf test7-db', 'dotnet build', 'dotnet run --no-build hello world'], "test8-db")
47+
check_build_out("hello, world", s)

csharp/tools/tracing-config.lua

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ function RegisterExtractorPack(id)
2121
-- if that's `build`, we append `-p:UseSharedCompilation=false` to the command line,
2222
-- otherwise we do nothing.
2323
local match = false
24-
local needsSeparator = false;
25-
local injectionIndex = nil;
24+
local dotnetRunNeedsSeparator = false;
25+
local dotnetRunInjectionIndex = nil;
2626
local argv = compilerArguments.argv
2727
if OperatingSystem == 'windows' then
2828
-- let's hope that this split matches the escaping rules `dotnet` applies to command line arguments
@@ -34,7 +34,9 @@ function RegisterExtractorPack(id)
3434
-- dotnet options start with either - or / (both are legal)
3535
local firstCharacter = string.sub(arg, 1, 1)
3636
if not (firstCharacter == '-') and not (firstCharacter == '/') then
37-
Log(1, 'Dotnet subcommand detected: %s', arg)
37+
if (not match) then
38+
Log(1, 'Dotnet subcommand detected: %s', arg)
39+
end
3840
if arg == 'build' or arg == 'msbuild' or arg == 'publish' or arg == 'pack' or arg == 'test' then
3941
match = true
4042
break
@@ -43,22 +45,29 @@ function RegisterExtractorPack(id)
4345
-- for `dotnet run`, we need to make sure that `-p:UseSharedCompilation=false` is
4446
-- not passed in as an argument to the program that is run
4547
match = true
46-
needsSeparator = true
47-
injectionIndex = i + 1
48+
dotnetRunNeedsSeparator = true
49+
dotnetRunInjectionIndex = i + 1
4850
end
4951
end
52+
-- if we see a separator to `dotnet run`, inject just prior to the existing separator
5053
if arg == '--' then
51-
needsSeparator = false
52-
injectionIndex = i
54+
dotnetRunNeedsSeparator = false
55+
dotnetRunInjectionIndex = i
5356
break
5457
end
58+
-- if we see an option to `dotnet run` (e.g., `--project`), inject just prior
59+
-- to the last option
60+
if firstCharacter == '-' then
61+
dotnetRunNeedsSeparator = false
62+
dotnetRunInjectionIndex = i
63+
end
5564
end
5665
if match then
5766
local injections = { '-p:UseSharedCompilation=false' }
58-
if needsSeparator then
67+
if dotnetRunNeedsSeparator then
5968
table.insert(injections, '--')
6069
end
61-
if injectionIndex == nil then
70+
if dotnetRunInjectionIndex == nil then
6271
-- Simple case; just append at the end
6372
return {
6473
order = ORDER_REPLACE,
@@ -69,7 +78,7 @@ function RegisterExtractorPack(id)
6978

7079
-- Complex case; splice injections into the middle of the command line
7180
for i, injectionArg in ipairs(injections) do
72-
table.insert(argv, injectionIndex + i - 1, injectionArg)
81+
table.insert(argv, dotnetRunInjectionIndex + i - 1, injectionArg)
7382
end
7483

7584
if OperatingSystem == 'windows' then

0 commit comments

Comments
 (0)