Skip to content

Commit fc3bc95

Browse files
authored
Merge pull request #14218 from michaelnebel/csharp/dotnetdotnet
Lua: Tracing of `dotnet dotnet`.
2 parents 43cdbf2 + e577fb6 commit fc3bc95

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,8 @@ def check_build_out(msg, s):
5858
s = run_codeql_database_create_stdout(['dotnet clean', 'rm -rf test8-db', 'dotnet run "hello world part1" part2'], "test9-db")
5959
check_build_out("hello world part1, part2", s)
6060
check_diagnostics(test_db="test9-db")
61+
62+
# two arguments, no '--' (second argument quoted) and using dotnet to execute dotnet
63+
s = run_codeql_database_create_stdout(['dotnet clean', 'rm -rf test9-db', 'dotnet dotnet run part1 "hello world part2"'], "test10-db")
64+
check_build_out("part1, hello world part2", s)
65+
check_diagnostics(test_db="test10-db")

csharp/tools/tracing-config.lua

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,40 @@ function RegisterExtractorPack(id)
55

66
local extractor = Exify(GetPlatformToolsDirectory() .. 'Semmle.Extraction.CSharp.Driver')
77

8+
local function isDotnet(name)
9+
return name == 'dotnet' or name == 'dotnet.exe'
10+
end
11+
12+
local function isDotnetPath(path)
13+
return path:match('dotnet[.]exe$') or path:match('dotnet$')
14+
end
15+
16+
local function isPossibleDotnetSubcommand(arg)
17+
-- dotnet options start with either - or / (both are legal)
18+
-- It is possible to run dotnet with dotnet, e.g., `dotnet dotnet build`
19+
-- but we shouldn't consider `dotnet` to be a subcommand.
20+
local firstCharacter = string.sub(arg, 1, 1)
21+
return not (firstCharacter == '-') and
22+
not (firstCharacter == '/') and
23+
not isDotnetPath(arg)
24+
end
25+
826
function DotnetMatcherBuild(compilerName, compilerPath, compilerArguments,
927
_languageId)
10-
if compilerName ~= 'dotnet' and compilerName ~= 'dotnet.exe' then
28+
if not isDotnet(compilerName) then
1129
return nil
1230
end
1331

1432
-- The dotnet CLI has the following usage instructions:
15-
-- dotnet [sdk-options] [command] [command-options] [arguments]
33+
-- dotnet [sdk-options] [command] [command-options] [arguments] OR
34+
-- dotnet [runtime-options] [path-to-application] [arguments]
1635
-- we are interested in dotnet build, which has the following usage instructions:
1736
-- dotnet [options] build [<PROJECT | SOLUTION>...]
1837
-- For now, parse the command line as follows:
1938
-- Everything that starts with `-` (or `/`) will be ignored.
20-
-- The first non-option argument is treated as the command.
21-
-- if that's `build`, we append `-p:UseSharedCompilation=false` to the command line,
39+
-- The first non-option argument is treated as the command (except if it is dotnet itself).
40+
-- if that's `build` or similar, we append `-p:UseSharedCompilation=false`
41+
-- and `-p:EmitCompilerGeneratedFiles=true` to the command line,
2242
-- otherwise we do nothing.
2343
local match = false
2444
local testMatch = false
@@ -36,9 +56,7 @@ function RegisterExtractorPack(id)
3656
NativeArgumentsToArgv(compilerArguments.nativeArgumentPointer)
3757
end
3858
for i, arg in ipairs(argv) do
39-
-- dotnet options start with either - or / (both are legal)
40-
local firstCharacter = string.sub(arg, 1, 1)
41-
if not (firstCharacter == '-') and not (firstCharacter == '/') then
59+
if isPossibleDotnetSubcommand(arg) then
4260
if (not match) and inSubCommandPosition then
4361
Log(1, 'Dotnet subcommand detected: %s', arg)
4462
end
@@ -80,7 +98,7 @@ function RegisterExtractorPack(id)
8098
end
8199
-- if we see an option to `dotnet run` (e.g., `--project`), inject just prior
82100
-- to the last option
83-
if firstCharacter == '-' then
101+
if string.sub(arg, 1, 1) == '-' then
84102
dotnetRunNeedsSeparator = false
85103
dotnetRunInjectionIndex = i
86104
end

0 commit comments

Comments
 (0)