Skip to content

Commit ad48915

Browse files
authored
Merge pull request #10469 from hvitved/csharp/dotnet-run-tracer-fix
C#: Prepend `-p:UseSharedCompilation=false` instead of append for `dotnet run`
2 parents adf5f18 + 659f1cc commit ad48915

File tree

4 files changed

+80
-4
lines changed

4 files changed

+80
-4
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Console.WriteLine(args.Length > 0 ? args[0] + ", " + args[1] : "");
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>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from create_database_utils import *
2+
3+
# no arguments
4+
run_codeql_database_create(['dotnet run'], test_db="test-db", db=None, lang="csharp")
5+
6+
# no arguments, but `--`
7+
run_codeql_database_create(['dotnet clean', 'rm -rf test-db obj bin', 'dotnet run --'], test_db="test2-db", db=None, lang="csharp")
8+
9+
# two arguments, no `--`
10+
run_codeql_database_create(['dotnet clean', 'rm -rf test2-db obj bin', 'dotnet run hello world'], test_db="test3-db", db=None, lang="csharp")
11+
12+
# two arguments, and `--`
13+
run_codeql_database_create(['dotnet clean', 'rm -rf test3-db obj bin', 'dotnet run -- hello world'], test_db="test4-db", db=None, lang="csharp")
14+
15+
# shared compilation enabled; tracer should override by changing the command
16+
# to `dotnet run -p:UseSharedCompilation=true -p:UseSharedCompilation=false -- hello world`
17+
run_codeql_database_create(['dotnet clean', 'rm -rf test4-db obj bin', 'dotnet run -p:UseSharedCompilation=true -- hello world'], test_db="test5-db", db=None, lang="csharp")

csharp/tools/tracing-config.lua

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function RegisterExtractorPack(id)
2222
-- otherwise we do nothing.
2323
local match = false
2424
local needsSeparator = false;
25+
local injectionIndex = nil;
2526
local argv = compilerArguments.argv
2627
if OperatingSystem == 'windows' then
2728
-- let's hope that this split matches the escaping rules `dotnet` applies to command line arguments
@@ -38,13 +39,60 @@ function RegisterExtractorPack(id)
3839
match = true
3940
break
4041
end
42+
if arg == 'run' then
43+
-- for `dotnet run`, we need to make sure that `-p:UseSharedCompilation=false` is
44+
-- not passed in as an argument to the program that is run
45+
match = true
46+
needsSeparator = true
47+
injectionIndex = i + 1
48+
end
49+
end
50+
if arg == '--' then
51+
needsSeparator = false
52+
injectionIndex = i
53+
break
4154
end
4255
end
4356
if match then
44-
return {
45-
order = ORDER_REPLACE,
46-
invocation = BuildExtractorInvocation(id, compilerPath, compilerPath, compilerArguments, nil, { '-p:UseSharedCompilation=false' })
47-
}
57+
local injections = { '-p:UseSharedCompilation=false' }
58+
if needsSeparator then
59+
table.insert(injections, '--')
60+
end
61+
if injectionIndex == nil then
62+
-- Simple case; just append at the end
63+
return {
64+
order = ORDER_REPLACE,
65+
invocation = BuildExtractorInvocation(id, compilerPath, compilerPath, compilerArguments, nil,
66+
injections)
67+
}
68+
end
69+
70+
-- Complex case; splice injections into the middle of the command line
71+
for i, injectionArg in ipairs(injections) do
72+
table.insert(argv, injectionIndex + i - 1, injectionArg)
73+
end
74+
75+
if OperatingSystem == 'windows' then
76+
return {
77+
order = ORDER_REPLACE,
78+
invocation = {
79+
path = AbsolutifyExtractorPath(id, compilerPath),
80+
arguments = {
81+
commandLineString = table.concat(argv, " ")
82+
}
83+
}
84+
}
85+
else
86+
return {
87+
order = ORDER_REPLACE,
88+
invocation = {
89+
path = AbsolutifyExtractorPath(id, compilerPath),
90+
arguments = {
91+
argv = argv
92+
}
93+
}
94+
}
95+
end
4896
end
4997
return nil
5098
end

0 commit comments

Comments
 (0)