Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 24, 2025

Summary

This PR enhances the C# compiler's #error version directive to include the path of the current compiler assembly and consistently uses the full commit SHA throughout all version outputs, making it easier for developers to troubleshoot version-related issues.

Changes

The implementation makes two key enhancements to the existing ERR_CompilerAndLanguageVersion diagnostic and simplifies the version string generation throughout the compiler:

Before:

error CS8304: Compiler version: '5.3.0-dev (a1b2c3d4)'. Language version: default (14.0).

After:

error CS8304: Compiler version: '5.3.0-dev (a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0)'. Language version: default (14.0). Compiler path: '/path/to/Microsoft.CodeAnalysis.CSharp.dll'.

Key Implementation Details

  1. Added GetAssemblyLocation(Type type) method in CommonCompiler.cs that retrieves the assembly location and returns <unknown> for edge cases like single-file deployments where Assembly.Location may be empty

  2. Modified GetProductVersion(Type type) method in CommonCompiler.cs to use the full commit SHA directly from CommitHashAttribute instead of truncating to 8 characters

  3. Removed short SHA logic: Eliminated ExtractShortCommitHash(), GetShortCommitHash(), GetFullCommitHash(), and GetProductVersionWithFullHash() methods to simplify the codebase and ensure consistent use of full commit SHA everywhere

  4. Updated DirectiveParser.cs to call GetProductVersion and GetAssemblyLocation, passing both the full SHA and assembly path to the error diagnostic

  5. Modified the error message format in CSharpResources.resx to include a third placeholder for the compiler path

  6. Updated tests in PreprocessorTests.cs to validate the new output format with full SHA

  7. Removed the ExtractShortCommitHash test that's no longer applicable

Code Simplification

This change results in a net reduction of 43 lines of code while improving consistency:

  • Single source of truth: GetProductVersion() now handles all version string generation
  • Eliminated code duplication and redundant methods
  • Full commit SHA used consistently in all outputs (both #error version and /version)

Testing

All existing tests pass, including:

  • 7 TestErrorWithVersion tests across different language versions
  • 236 preprocessor directive tests
  • 145 version-related command line tests
  • Manual verification with multiple language versions (default, 9.0, preview)

Compatibility

This change only affects the diagnostic message text for #error version and version display output. It does not modify any APIs or change compiler behavior beyond the error message format and version string display. The change maintains backward compatibility while providing more detailed version information.

Original prompt

Create a PR that expands the output of #error version from the C# compiler to log the path of the current compiler assembly


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Add logging for current compiler assembly path in error version output Expand #error version output to include compiler assembly path Oct 24, 2025
Copilot AI requested a review from jcouv October 24, 2025 17:29
Copilot finished work on behalf of jcouv October 24, 2025 17:29
@jcouv
Copy link
Member

jcouv commented Oct 24, 2025

@copilot The existing output of #error version also includes a shortened commit SHA. Let's switch that to use a full SHA

Copy link
Contributor Author

Copilot AI commented Oct 24, 2025

@copilot The existing output of #error version also includes a shortened commit SHA. Let's switch that to use a full SHA

Done! Switched to using the full commit SHA in commit 1fac3d9.

Changes made:

  • Added GetFullCommitHash() method that returns the full SHA from CommitHashAttribute
  • Added GetProductVersionWithFullHash() method that uses the full hash instead of the 8-character short version
  • Updated DirectiveParser and tests to use the new method

The output now shows the full 40-character SHA (e.g., a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0) instead of the shortened 8-character version (e.g., a1b2c3d4). Special values like <developer build> are preserved as-is.

Copilot AI changed the title Expand #error version output to include compiler assembly path Expand #error version output to include compiler assembly path and full commit SHA Oct 24, 2025
Copilot finished work on behalf of jcouv October 24, 2025 18:59
@jcouv jcouv force-pushed the copilot/expand-error-version-output branch from 8724a59 to 1fac3d9 Compare October 24, 2025 21:18
@jcouv
Copy link
Member

jcouv commented Oct 24, 2025

@copilot Let's replace all usages of shortened SHA with full SHA. We don't need to keep short SHA logic anymore

Copy link
Contributor Author

Copilot AI commented Oct 24, 2025

@copilot Let's replace all usages of shortened SHA with full SHA. We don't need to keep short SHA logic anymore

Done! Removed all short SHA logic in commit 0d15c99.

Changes made:

  • Modified GetProductVersion() to use full commit SHA directly
  • Removed ExtractShortCommitHash(), GetShortCommitHash(), GetFullCommitHash(), and GetProductVersionWithFullHash() methods
  • Updated DirectiveParser and tests to use GetProductVersion() consistently
  • Removed the ExtractShortCommitHash test

This simplifies the codebase by 43 net lines while ensuring all version outputs (both #error version and /version) consistently use the full commit SHA.

@jcouv
Copy link
Member

jcouv commented Oct 24, 2025

FWIW, here's what the diagnostic looks like now for a local/dev build (ie. no SHA):
image

Copilot AI changed the title Expand #error version output to include compiler assembly path and full commit SHA Expand #error version output to include compiler assembly path and use full commit SHA everywhere Oct 24, 2025
Copilot finished work on behalf of jcouv October 24, 2025 21:30
@jcouv jcouv marked this pull request as ready for review October 24, 2025 23:27
@jcouv jcouv requested a review from a team as a code owner October 24, 2025 23:27
@AlekseyTs
Copy link
Contributor

Could this break tools that parse the output?

@jcouv
Copy link
Member

jcouv commented Oct 27, 2025

Could this break tools that parse the output?

Possibly, if there's any. But I don't think the output is specified/locked.
We need to get the full SHA out. The alternative is to introduce new commands/directives and I don't think that's worth it.

Copy link
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

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

LGTM (commit 5)

@jcouv jcouv merged commit 3b8c432 into main Oct 27, 2025
24 checks passed
@jcouv jcouv deleted the copilot/expand-error-version-output branch October 27, 2025 18:33
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Oct 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants