Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit cc9ecd9

Browse files
authored
feat(codeintel): Add precise auto-inferencing for dotnet (#63348)
Adds support for dotnet inference by adding the lua script and wiring it up in the inference engine.
1 parent 19a0c73 commit cc9ecd9

File tree

10 files changed

+153
-4
lines changed

10 files changed

+153
-4
lines changed

internal/codeintel/autoindexing/internal/inference/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ go_test(
5050
timeout = "short",
5151
srcs = [
5252
"infer_test.go",
53+
"lang_dotnet_test.go",
5354
"lang_go_test.go",
5455
"lang_java_test.go",
5556
"lang_python_test.go",
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package inference
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestDotNetGenerator(t *testing.T) {
8+
testGenerators(t,
9+
generatorTestCase{
10+
description: "dotnet sln files exist",
11+
repositoryContents: map[string]string{
12+
"one.sln": "",
13+
"one.csproj": "",
14+
"foo/baz/two.sln": "",
15+
"foo/baz/two.vbproj": "",
16+
},
17+
},
18+
generatorTestCase{
19+
description: "dotnet sln files do not exist",
20+
repositoryContents: map[string]string{
21+
"one.csproj": "",
22+
"foo/baz/two.vbproj": "",
23+
},
24+
},
25+
)
26+
}

internal/codeintel/autoindexing/internal/inference/libs/indexes.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var defaultIndexers = map[string]string{
2222
"rust": "sourcegraph/scip-rust",
2323
"typescript": "sourcegraph/scip-typescript",
2424
"ruby": "sourcegraph/scip-ruby",
25+
"dotnet": "sourcegraph/scip-dotnet",
2526
}
2627

2728
// To update, run `DOCKER_USER=... DOCKER_PASS=... ./update-shas.sh`
@@ -32,6 +33,7 @@ var defaultIndexerSHAs = map[string]string{
3233
"sourcegraph/scip-python": "sha256:e3c13f0cadca78098439c541d19a72c21672a3263e22aa706760d941581e068d",
3334
"sourcegraph/scip-typescript": "sha256:3df8b36a2ad4e073415bfbeaedf38b3cfff3e697614c8f578299f470d140c2c8",
3435
"sourcegraph/scip-ruby": "sha256:ef53e5f1450330ddb4a3edce963b7e10d900d44ff1e7de4960680289ac25f319",
36+
"sourcegraph/scip-dotnet": "sha256:1d8a590edfb3834020fceedacac6608811dd31fcba9092426140093876d8d52e",
3537
}
3638

3739
func DefaultIndexerForLang(language string) (string, bool) {

internal/codeintel/autoindexing/internal/inference/libs/update-shas.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fi
1616
SCRIPT_DIR="$(dirname "${BASH_SOURCE[0]}")"
1717

1818
# No scip-clang as that doesn't have a Docker image
19-
for indexer in scip-go scip-rust scip-java scip-python scip-typescript scip-ruby; do
19+
for indexer in scip-go scip-rust scip-java scip-python scip-typescript scip-ruby scip-dotnet; do
2020
tag="latest"
2121
if [[ "${indexer}" = "scip-python" ]] || [[ "${indexer}" = "scip-typescript" || "${indexer}" = "scip-ruby" ]]; then
2222
tag="autoindex"

internal/codeintel/autoindexing/internal/inference/lua/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ go_library(
88
"README.md",
99
"config.lua",
1010
"embed.go",
11+
"dotnet.lua",
1112
"go.lua",
1213
"indexes.lua",
1314
"java.lua",
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
local path = require("path")
2+
local pattern = require("sg.autoindex.patterns")
3+
local recognizer = require("sg.autoindex.recognizer")
4+
5+
local indexer = require("sg.autoindex.indexes").get("dotnet")
6+
7+
local env_steps = {
8+
9+
-- macOS enables W^X (https://en.wikipedia.org/wiki/W%5EX) which means
10+
-- that when running in development, we need to set the following environment
11+
-- variable for `dotnet restore` to work correctly. It is technically not needed on
12+
-- Linux in production, and removing this might improve performance, but we
13+
-- currently do not have a way of conditionally passing in OS-level configuration
14+
-- to the Lua script, and doing so would create a divergence between dev vs prod,
15+
-- so leave this as-is for now.
16+
--
17+
-- See also: https://github.com/dotnet/runtime/issues/97828
18+
"export DOTNET_EnableWriteXorExecute=0",
19+
}
20+
21+
local generate_dotnet_jobs = function(_, paths)
22+
local jobs = {}
23+
for i = 1, #paths do
24+
table.insert(jobs, {
25+
indexer = indexer,
26+
root = path.dirname(paths[i]),
27+
local_steps = env_steps,
28+
indexer_args = { "scip-dotnet", "index", paths[i], "--output", "index.scip" },
29+
outfile = "index.scip",
30+
})
31+
end
32+
33+
return jobs
34+
end
35+
36+
local dotnet_proj_recognizer = recognizer.new_path_recognizer({
37+
patterns = {
38+
pattern.new_path_extension("csproj"),
39+
pattern.new_path_extension("vbproj"),
40+
},
41+
42+
generate = generate_dotnet_jobs,
43+
})
44+
45+
local dotnet_sln_recognizer = recognizer.new_path_recognizer({
46+
patterns = {
47+
pattern.new_path_extension("sln"),
48+
},
49+
50+
generate = generate_dotnet_jobs,
51+
})
52+
53+
-- For .NET projects we are employing a fallback recognizer that will first
54+
-- look for only solution files in a repo. This is the most common structure for .NET repos
55+
-- and we will build an indexing job for each solution file we find.
56+
-- If we find no solution files, we will then look for any .csproj or.vbproj files
57+
-- and build an indexing job for each of them. This structure can happen since solution files are not required
58+
-- and sometimes people auto-generate and don't version control them just for IDE support.
59+
-- A repo with a more exotic structure, mixing projects that are part of solutions with
60+
-- projects that are outside of solutions, would need to be indexed by the user manually
61+
-- or have the user customize the inference.
62+
return recognizer.new_fallback_recognizer({
63+
dotnet_sln_recognizer,
64+
dotnet_proj_recognizer,
65+
})

internal/codeintel/autoindexing/internal/inference/lua/recognizers.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
local config = require("sg.autoindex.config").new {}
1+
local config = require("sg.autoindex.config").new({})
22

3-
for _, name in ipairs {
3+
for _, name in ipairs({
44
"go",
55
"java",
66
"python",
77
"ruby",
88
"rust",
99
"test",
1010
"typescript",
11-
} do
11+
"dotnet",
12+
}) do
1213
-- Backdoor set `sg.`-prefixed recognizers
1314
rawset(config, "sg." .. name, require("sg.autoindex." .. name))
1415
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
- steps: []
2+
local_steps:
3+
- export DOTNET_EnableWriteXorExecute=0
4+
root: ""
5+
indexer: sourcegraph/scip-dotnet@sha256:1d8a590edfb3834020fceedacac6608811dd31fcba9092426140093876d8d52e
6+
indexer_args:
7+
- scip-dotnet
8+
- index
9+
- one.csproj
10+
- --output
11+
- index.scip
12+
outfile: index.scip
13+
requestedEnvVars: []
14+
- steps: []
15+
local_steps:
16+
- export DOTNET_EnableWriteXorExecute=0
17+
root: foo/baz
18+
indexer: sourcegraph/scip-dotnet@sha256:1d8a590edfb3834020fceedacac6608811dd31fcba9092426140093876d8d52e
19+
indexer_args:
20+
- scip-dotnet
21+
- index
22+
- foo/baz/two.vbproj
23+
- --output
24+
- index.scip
25+
outfile: index.scip
26+
requestedEnvVars: []
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
- steps: []
2+
local_steps:
3+
- export DOTNET_EnableWriteXorExecute=0
4+
root: ""
5+
indexer: sourcegraph/scip-dotnet@sha256:1d8a590edfb3834020fceedacac6608811dd31fcba9092426140093876d8d52e
6+
indexer_args:
7+
- scip-dotnet
8+
- index
9+
- one.sln
10+
- --output
11+
- index.scip
12+
outfile: index.scip
13+
requestedEnvVars: []
14+
- steps: []
15+
local_steps:
16+
- export DOTNET_EnableWriteXorExecute=0
17+
root: foo/baz
18+
indexer: sourcegraph/scip-dotnet@sha256:1d8a590edfb3834020fceedacac6608811dd31fcba9092426140093876d8d52e
19+
indexer_args:
20+
- scip-dotnet
21+
- index
22+
- foo/baz/two.sln
23+
- --output
24+
- index.scip
25+
outfile: index.scip
26+
requestedEnvVars: []

internal/codeintel/autoindexing/internal/store/config_inference.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ return require("sg.autoindex.config").new({
9292
-- ["sg.ruby"] = false,
9393
-- ["sg.rust"] = false,
9494
-- ["sg.typescript"] = false,
95+
-- ["sg.dotnet"] = false,
9596
["acme.custom"] = custom_recognizer,
9697
})
9798
`

0 commit comments

Comments
 (0)