DependencyScanner Class

Provides the base class for scanning source code files to discover dependencies across multiple package ecosystems and formats.

// Scan a directory for all dependencies
var dependencies = await DependencyScanner.ScanDirectoryAsync(
    "C:\\MyProject",
    options: null,
    cancellationToken);

foreach (var dependency in dependencies)
{
    Console.WriteLine($"{dependency.Type}: {dependency.Name}@{dependency.Version}");
}
public abstract class DependencyScanner

Constructors

NameDescription
DependencyScanner()

DependencyScanner()

protected DependencyScanner()

No documentation is available.

Properties

NameDescription
SupportedDependencyTypes

SupportedDependencyTypes

protected internal abstract IReadOnlyCollection<DependencyType> SupportedDependencyTypes { get; }

No documentation is available.

Methods

NameDescription
ScanAsync(ScanFileContext)When overridden in a derived class, performs the actual scanning of a file to discover dependencies.
ScanDirectoryAsync(string, ScannerOptions?, DependencyFound, CancellationToken)Scans a directory and its subdirectories for dependencies, invoking a callback for each dependency found.
ScanDirectoryAsync(string, ScannerOptions?, CancellationToken)Scans a directory and its subdirectories for dependencies.
ScanFileAsync(string, string, ScannerOptions?, CancellationToken)Scans a single file from the file system for dependencies.
ScanFileAsync(string, string, byte[], IReadOnlyList<DependencyScanner>?, CancellationToken)Scans a single file from memory for dependencies, using the specified scanners.
ScanFileAsync(string, string, byte[], CancellationToken)Scans a single file from memory for dependencies.
ScanFilesAsync(string, IEnumerable<string>, ScannerOptions?, DependencyFound, CancellationToken)Scans multiple specific files for dependencies, invoking a callback for each dependency found.
ScanFilesAsync(string, IEnumerable<string>, ScannerOptions?, CancellationToken)Scans multiple specific files for dependencies.
ShouldScanFile(CandidateFileContext)Determines whether this scanner should scan the specified file.
ShouldScanFile(ReadOnlySpan<char>, ReadOnlySpan<char>)Determines whether this scanner should scan the specified file.
ShouldScanFileCore(CandidateFileContext)When overridden in a derived class, determines whether this scanner should scan the specified file.

ScanAsync(ScanFileContext)

When overridden in a derived class, performs the actual scanning of a file to discover dependencies.

public abstract ValueTask ScanAsync(ScanFileContext context)

Parameters

context ScanFileContext
The context containing the file to scan and methods to report discovered dependencies.

Returns

ValueTask

ScanDirectoryAsync overloads

ScanDirectoryAsync(string, ScannerOptions?, DependencyFound, CancellationToken)

DependencyScanner.cs

Scans a directory and its subdirectories for dependencies, invoking a callback for each dependency found.

net10.0

public static Task ScanDirectoryAsync(string path, ScannerOptions? options, DependencyFound onDependencyFound, CancellationToken cancellationToken = default)

net11.0

public static Task ScanDirectoryAsync(string path, ScannerOptions? options, DependencyFound onDependencyFound, CancellationToken cancellationToken = default)
Parameters
path string
The root directory path to scan.
options ScannerOptions?
The scanner options, or null to use defaults.
onDependencyFound DependencyFound
The callback invoked when a dependency is found. It can be invoked concurrently.
cancellationToken CancellationToken
A token to cancel the operation.
Returns

Task

Exceptions
DirectoryNotFoundException
The directory does not exist. The exception is reported through the returned task.
NotSupportedException
ScannerOptions.FileSystem is a custom file system, and a file or directory predicate is set. The exception is reported through the returned task.
Remarks

Files are scanned concurrently unless ScannerOptions.DegreeOfParallelism is 1, so onDependencyFound can be invoked concurrently from several threads and must be thread-safe. An exception thrown by onDependencyFound stops the scan.

A file that cannot be read, or that a scanner fails to scan, is skipped and reported to ScannerOptions.OnFileScanFailed. Symbolic links to directories are not followed, and symbolic links to files outside path are not scanned.

ScanDirectoryAsync(string, ScannerOptions?, CancellationToken)

DependencyScanner.cs

Scans a directory and its subdirectories for dependencies.

net10.0

public static Task<IReadOnlyCollection<Dependency>> ScanDirectoryAsync(string path, ScannerOptions? options, CancellationToken cancellationToken = default)

net11.0

public static Task<IReadOnlyCollection<Dependency>> ScanDirectoryAsync(string path, ScannerOptions? options, CancellationToken cancellationToken = default)
Parameters
path string
The root directory path to scan.
options ScannerOptions?
The scanner options, or null to use defaults.
cancellationToken CancellationToken
A token to cancel the operation.
Returns

Task<IReadOnlyCollection<Dependency>> A collection of all discovered dependencies.

Exceptions
DirectoryNotFoundException
The directory does not exist. The exception is reported through the returned task.
NotSupportedException
ScannerOptions.FileSystem is a custom file system, and a file or directory predicate is set. The exception is reported through the returned task.
Remarks

A file that cannot be read, or that a scanner fails to scan, is skipped and reported to ScannerOptions.OnFileScanFailed. Symbolic links to directories are not followed, and symbolic links to files outside path are not scanned.

ScanFileAsync overloads

ScanFileAsync(string, string, ScannerOptions?, CancellationToken)

DependencyScanner.cs

Scans a single file from the file system for dependencies.

net10.0

public static Task<IReadOnlyCollection<Dependency>> ScanFileAsync(string rootDirectory, string filePath, ScannerOptions? options, CancellationToken cancellationToken = default)

net11.0

public static Task<IReadOnlyCollection<Dependency>> ScanFileAsync(string rootDirectory, string filePath, ScannerOptions? options, CancellationToken cancellationToken = default)
Parameters
rootDirectory string
The root directory context for relative path resolution.
filePath string
The path of the file to scan.
options ScannerOptions?
The scanner options, or null to use defaults.
cancellationToken CancellationToken
A token to cancel the operation.
Returns

Task<IReadOnlyCollection<Dependency>> A collection of dependencies found in the file.

Remarks

A single file is handled like a file of a directory scan: when the file cannot be read, or a scanner fails to scan it, the failure is reported to ScannerOptions.OnFileScanFailed, the remaining scanners are skipped, and the dependencies found before the failure are returned.

ScanFileAsync(string, string, byte[], IReadOnlyList<DependencyScanner>?, CancellationToken)

DependencyScanner.cs

Scans a single file from memory for dependencies, using the specified scanners.

net10.0

public static Task<IReadOnlyCollection<Dependency>> ScanFileAsync(string rootDirectory, string filePath, byte[] content, IReadOnlyList<DependencyScanner>? scanners, CancellationToken cancellationToken = default)

net11.0

public static Task<IReadOnlyCollection<Dependency>> ScanFileAsync(string rootDirectory, string filePath, byte[] content, IReadOnlyList<DependencyScanner>? scanners, CancellationToken cancellationToken = default)
Parameters
rootDirectory string
The root directory context for relative path resolution.
filePath string
The path of the file being scanned.
content byte[]
The file content as a byte array.
scanners IReadOnlyList<DependencyScanner>?
The scanners to use, or null to use the default scanners.
cancellationToken CancellationToken
A token to cancel the operation.
Returns

Task<IReadOnlyCollection<Dependency>> A collection of dependencies found in the file.

Remarks

When a scanner fails to scan the content, the dependencies found before the failure are returned, and the failure is not reported. The locations of the dependencies are not updatable, because there is no file to write.

ScanFileAsync(string, string, byte[], CancellationToken)

DependencyScanner.cs

Scans a single file from memory for dependencies.

public static Task<IReadOnlyCollection<Dependency>> ScanFileAsync(string rootDirectory, string filePath, byte[] content, CancellationToken cancellationToken = default)
Parameters
rootDirectory string
The root directory context for relative path resolution.
filePath string
The path of the file being scanned.
content byte[]
The file content as a byte array.
cancellationToken CancellationToken
A token to cancel the operation.
Returns

Task<IReadOnlyCollection<Dependency>> A collection of dependencies found in the file.

Remarks

When a scanner fails to scan the content, the dependencies found before the failure are returned, and the failure is not reported. The locations of the dependencies are not updatable, because there is no file to write.

ScanFilesAsync overloads

ScanFilesAsync(string, IEnumerable<string>, ScannerOptions?, DependencyFound, CancellationToken)

DependencyScanner.cs

Scans multiple specific files for dependencies, invoking a callback for each dependency found.

public static Task ScanFilesAsync(string rootDirectory, IEnumerable<string> filePaths, ScannerOptions? options, DependencyFound onDependencyFound, CancellationToken cancellationToken = default)
Parameters
rootDirectory string
The root directory context for relative path resolution.
filePaths IEnumerable<string>
The collection of file paths to scan.
options ScannerOptions?
The scanner options, or null to use defaults.
onDependencyFound DependencyFound
The callback invoked when a dependency is found. It can be invoked concurrently.
cancellationToken CancellationToken
A token to cancel the operation.
Returns

Task

Remarks

Files are scanned concurrently unless ScannerOptions.DegreeOfParallelism is 1, so onDependencyFound can be invoked concurrently from several threads and must be thread-safe. An exception thrown by onDependencyFound stops the scan. A file that cannot be read, or that a scanner fails to scan, is skipped and reported to ScannerOptions.OnFileScanFailed.

ScanFilesAsync(string, IEnumerable<string>, ScannerOptions?, CancellationToken)

DependencyScanner.cs

Scans multiple specific files for dependencies.

net10.0

public static Task<IReadOnlyCollection<Dependency>> ScanFilesAsync(string rootDirectory, IEnumerable<string> filePaths, ScannerOptions? options, CancellationToken cancellationToken = default)

net11.0

public static Task<IReadOnlyCollection<Dependency>> ScanFilesAsync(string rootDirectory, IEnumerable<string> filePaths, ScannerOptions? options, CancellationToken cancellationToken = default)
Parameters
rootDirectory string
The root directory context for relative path resolution.
filePaths IEnumerable<string>
The collection of file paths to scan.
options ScannerOptions?
The scanner options, or null to use defaults.
cancellationToken CancellationToken
A token to cancel the operation.
Returns

Task<IReadOnlyCollection<Dependency>> A collection of all dependencies found across the specified files.

Remarks

A file that cannot be read, or that a scanner fails to scan, is skipped and reported to ScannerOptions.OnFileScanFailed.

ShouldScanFile overloads

ShouldScanFile(CandidateFileContext)

DependencyScanner.cs

Determines whether this scanner should scan the specified file.

public bool ShouldScanFile(CandidateFileContext context)
Parameters
context CandidateFileContext
The context containing information about the candidate file.
Returns

bool true if the file should be scanned; otherwise, false.

ShouldScanFile(ReadOnlySpan<char>, ReadOnlySpan<char>)

DependencyScanner.cs

Determines whether this scanner should scan the specified file.

public bool ShouldScanFile(ReadOnlySpan<char> rootDirectory, ReadOnlySpan<char> fullPath)
Parameters
rootDirectory ReadOnlySpan<char>
The root directory path.
fullPath ReadOnlySpan<char>
The full path of the file.
Returns

bool true if the file should be scanned; otherwise, false.

ShouldScanFileCore(CandidateFileContext)

When overridden in a derived class, determines whether this scanner should scan the specified file.

protected abstract bool ShouldScanFileCore(CandidateFileContext context)

Parameters

context CandidateFileContext
The context containing information about the candidate file.

Returns

bool true if the file should be scanned; otherwise, false.