-
Notifications
You must be signed in to change notification settings - Fork 1.8k
C#: Add extractor option for the dependency directory in BMN. #20832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
michaelnebel
merged 6 commits into
github:main
from
michaelnebel:csharp/dependencycaching
Nov 21, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e76e7ab
C#: Read from dependency directory from extractor option.
michaelnebel 1256ccf
C#: Add extractor option for buildless dependency directory.
michaelnebel 2700843
C#: Add an integration test for setting the dependency directory in BMN.
michaelnebel 90dbb7a
C#: Add change note.
michaelnebel 138441b
C#: Address review comments.
michaelnebel 5c454d2
C#: Fix typo.
michaelnebel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyDirectory.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| using System; | ||
| using System.IO; | ||
| using Semmle.Util; | ||
| using Semmle.Util.Logging; | ||
|
|
||
| namespace Semmle.Extraction.CSharp.DependencyFetching | ||
| { | ||
| /// <summary> | ||
| /// A directory used for storing fetched dependencies. | ||
| /// When a specific directory is set via the dependency directory extractor option, | ||
| /// we store dependencies in that directory for caching purposes. | ||
| /// Otherwise, we create a temporary directory that is deleted upon disposal. | ||
| /// </summary> | ||
| public sealed class DependencyDirectory : IDisposable | ||
| { | ||
| private readonly string userReportedDirectoryPurpose; | ||
| private readonly ILogger logger; | ||
| private readonly bool attemptCleanup; | ||
|
|
||
| public DirectoryInfo DirInfo { get; } | ||
|
|
||
| public DependencyDirectory(string subfolderName, string userReportedDirectoryPurpose, ILogger logger) | ||
| { | ||
| this.logger = logger; | ||
| this.userReportedDirectoryPurpose = userReportedDirectoryPurpose; | ||
|
|
||
| string path; | ||
| if (EnvironmentVariables.GetBuildlessDependencyDir() is string dir) | ||
| { | ||
| path = dir; | ||
| attemptCleanup = false; | ||
| } | ||
| else | ||
| { | ||
| path = FileUtils.GetTemporaryWorkingDirectory(out _); | ||
| attemptCleanup = true; | ||
| } | ||
| DirInfo = new DirectoryInfo(Path.Join(path, subfolderName)); | ||
| DirInfo.Create(); | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| if (!attemptCleanup) | ||
| { | ||
| logger.LogInfo($"Keeping {userReportedDirectoryPurpose} directory {DirInfo.FullName} for possible caching purposes."); | ||
| return; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| DirInfo.Delete(true); | ||
| } | ||
| catch (Exception exc) | ||
| { | ||
| logger.LogInfo($"Couldn't delete {userReportedDirectoryPurpose} directory {exc.Message}"); | ||
| } | ||
|
||
| } | ||
|
|
||
| public override string ToString() => DirInfo.FullName; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
csharp/ql/integration-tests/all-platforms/standalone_dependency_dir/Assemblies.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| | dependencies/packages/newtonsoft.json/13.0.1/lib/netstandard2.0/Newtonsoft.Json.dll:0:0:0:0 | Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed | |
7 changes: 7 additions & 0 deletions
7
csharp/ql/integration-tests/all-platforms/standalone_dependency_dir/Assemblies.ql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import csharp | ||
|
|
||
| from Assembly a | ||
| where | ||
| not a.getCompilation().getOutputAssembly() = a and | ||
| a.getName().matches("%Newtonsoft%") | ||
| select a |
6 changes: 6 additions & 0 deletions
6
csharp/ql/integration-tests/all-platforms/standalone_dependency_dir/proj/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| class Program | ||
| { | ||
| static void Main(string[] args) | ||
| { | ||
| } | ||
| } |
5 changes: 5 additions & 0 deletions
5
csharp/ql/integration-tests/all-platforms/standalone_dependency_dir/proj/global.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "sdk": { | ||
| "version": "9.0.304" | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
csharp/ql/integration-tests/all-platforms/standalone_dependency_dir/proj/standalone.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFrameworks>net9.0</TargetFrameworks> | ||
| </PropertyGroup> | ||
|
|
||
| <Target Name="DeleteBinObjFolders" BeforeTargets="Clean"> | ||
| <RemoveDir Directories=".\bin" /> | ||
| <RemoveDir Directories=".\obj" /> | ||
| </Target> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Newtonsoft.Json" Version="13.0.1" /> | ||
| </ItemGroup> | ||
| </Project> |
14 changes: 14 additions & 0 deletions
14
csharp/ql/integration-tests/all-platforms/standalone_dependency_dir/test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import os | ||
| import shutil | ||
|
|
||
| def test(codeql, csharp, cwd): | ||
| path = os.path.join(cwd, "dependencies") | ||
| os.environ["CODEQL_EXTRACTOR_CSHARP_OPTION_BUILDLESS_DEPENDENCY_DIR"] = path | ||
| # The Assemblies.ql query shows that the Newtonsoft assembly is found in the | ||
| # dependency directory set above. | ||
mbg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| codeql.database.create(source_root="proj", build_mode="none") | ||
|
|
||
| # Check that the packages directory has been created in the dependencies folder. | ||
| packages_dir = os.path.join(path, "packages") | ||
| assert os.path.isdir(packages_dir), "The packages directory was not created in the specified dependency directory." | ||
| shutil.rmtree(path) | ||
4 changes: 4 additions & 0 deletions
4
csharp/ql/lib/change-notes/2025-11-17-dependencies-directory.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| category: minorAnalysis | ||
| --- | ||
| * Added a new extractor option to specify a custom directory for dependency downloads in buildless mode. Use `-O buildless_dependency_dir=<path>` to configure the target directory. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.