Skip to content

Allow referencing NuGet packages#89

Merged
jjonescz merged 31 commits intomainfrom
packages
Jul 26, 2025
Merged

Allow referencing NuGet packages#89
jjonescz merged 31 commits intomainfrom
packages

Conversation

@jjonescz
Copy link
Owner

No description provided.

@jjonescz jjonescz added the enhancement New feature or request label Jul 26, 2025
@jjonescz jjonescz requested a review from Copilot July 26, 2025 13:18
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for referencing NuGet packages in the C# compiler playground through a new #:package directive. This enhancement allows users to include external dependencies dynamically when compiling and executing code.

Key changes:

  • Added #:package directive support for specifying NuGet dependencies
  • Enhanced NuGet downloader with dependency resolution and caching capabilities
  • Improved logging infrastructure for better debugging and error reporting

Reviewed Changes

Copilot reviewed 23 out of 23 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/Worker/Lab/NuGetDownloader.cs Major rewrite to support dependency resolution, caching, and multiple package downloads
src/Compiler/FileLevelDirectiveParser.cs Added Package directive parser and consumer logic
src/Shared/INuGetDownloader.cs New interface defining NuGet download contract with dependency and error handling
src/Shared/Set.cs New equatable set wrapper for dependency collections
src/Shared/Comparable.cs New comparable wrapper with custom equality semantics
test/UnitTests/CompilerProxyTests.cs Added comprehensive test coverage for package directives
src/Worker/Utils/SimpleConsoleLoggerProvider.cs Enhanced logging format with category names and log levels
src/Shared/Executor.cs Added custom assembly loader for runtime package execution
src/Worker/Lab/RefAssemblyDownloader.cs Refactored to use new NuGet infrastructure

{
if (item is not null)
{
hash |= comparer.GetHashCode(item);
Copy link

Copilot AI Jul 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using bitwise OR for hash code combination is incorrect. This will produce poor hash distribution and many collisions. Use HashCode.Combine or XOR (^) operation instead.

Copilot uses AI. Check for mistakes.
Comment on lines +410 to +412
return errors.ToDictionary(
static p => p.Key,
static IReadOnlyList<string> (p) => p.Value.ToList());
Copy link

Copilot AI Jul 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a new dictionary on every call to getErrors() is inefficient. Consider caching this result or returning the ConcurrentDictionary directly with a readonly wrapper.

Suggested change
return errors.ToDictionary(
static p => p.Key,
static IReadOnlyList<string> (p) => p.Value.ToList());
return new ReadOnlyDictionary<NuGetDependency, IReadOnlyList<string>>(errors);

Copilot uses AI. Check for mistakes.
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea but such wrapper won't work (the dictionary isn't covariant in the value), would have to create a custom one and it doesn't seem worth it given this is only for errors.

Comment on lines +320 to +323
return new NuGetResults
{
Assemblies = [],
Errors = getErrors(),
Copy link

Copilot AI Jul 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The getErrors() method creates a new dictionary each time it's called. Since this is called in both error and success paths, consider optimizing by checking if errors.IsEmpty first.

Suggested change
return new NuGetResults
{
Assemblies = [],
Errors = getErrors(),
var errorList = getErrors();
return new NuGetResults
{
Assemblies = [],
Errors = errorList,

Copilot uses AI. Check for mistakes.
IEnumerable<string> sources =
[
"https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json",
// NuGet.org should be tried first because it supports more efficient range requests.
Copy link

Copilot AI Jul 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment mentions 'range requests' but the code reorders sources. Consider clarifying that NuGet.org is prioritized for performance reasons rather than just range request support.

Suggested change
// NuGet.org should be tried first because it supports more efficient range requests.
// NuGet.org should be tried first because it supports more efficient range requests and generally offers better performance.

Copilot uses AI. Check for mistakes.
@jjonescz jjonescz marked this pull request as ready for review July 26, 2025 13:40
@jjonescz jjonescz merged commit 7a84970 into main Jul 26, 2025
3 checks passed
@jjonescz jjonescz deleted the packages branch July 26, 2025 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants