Support C# language versions: parse C# 15 unions, and honour each project's LangVersion across a mixed-version solution

オープン
#587 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
25/100
issue の種類
機能追加
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
csharp, typescript

調査の方向性

Start with the C# spec and the project-loading path that reads MSBuild evaluation and creates Roslyn parse options; the issue does not name implementation files. Then trace how parse options flow into linked documents, target frameworks, Razor, file-based apps, and tree-sitter. Done means the specified mixed-version solution and language features pass against the latest published release, with tests covering each listed scenario.

索引モデルが issue の本文から書いたものです。

説明

bug

Summary

SharpLsp has no concept of a C# language version. It parses every project with the compiler it ships (Roslyn Microsoft.CodeAnalysis.CSharp.Workspaces 5.9.0, plus tree-sitter-c-sharp 0.23 for the syntax tier), whatever the project targets. C# 15 union types therefore don't parse. A valid C# 15 file is covered in bogus errors:

public union ContentBody(Page, Resource, ResourceList, ResourcesForYou, Container);
CS0246  The type or namespace name 'union' could not be found          [Ln 9, Col 8]
CS0501  '<invalid-global-code>.ContentBody(Page, Resource, …)' must declare a body because it is not marked abstract, extern, or partial
CS1001  Identifier expected   (×5, one per case type)
CS0246  The type or namespace name 'ContentBody' could not be found     [Ln 6, Col 65]  ← every use of the union fails too

<invalid-global-code> shows the parser has no union declaration at all. The second union in the same file (Selector, line 30) fails the same way. The project builds with the SDK's own compiler; only SharpLsp reports these errors.

What C# says (official sources)

  • C# 15 adds union types, declared with the union keyword. Each listed case type is one possible value, and switches over a union are checked for exhaustiveness. Source: What's new in C# 15. The DevBlogs post says the model and syntax may still change: Explore union types in C# 15.
  • The language version comes from the target framework unless <LangVersion> overrides it:
    • Each TFM maps to a default version (.NET 8 → C# 12, .NET 9 → C# 13, .NET 10 → C# 14, .NET 11 → C# 15).
    • Using a version newer than the TFM's default is unsupported.
    • Source: C# language versioning.
  • <LangVersion> values are default, latest, latestMajor, preview or an explicit number. Setting default is not the same as leaving it out: default means the newest version the compiler supports, regardless of TFM. Sources: Compiler options – language (LangVersion), Configure the C# language version.

Required behaviour

  1. Support C# 15 unions. Ship a Roslyn whose parser and binder know C# 15, and a tree-sitter C# grammar that parses union declarations, so syntax features (outline, folding, selection, semantic tokens) work. Every semantic feature must work on unions: hover, go to definition, references, rename, completion, exhaustiveness diagnostics in switch, and code fixes.
  2. Honour each project's own language version. A project on an earlier version must get exactly what dotnet build gives it: no union parse, and the compiler's own "feature 'union types' is not available in C# 14. Please use language version 15.0 or greater" error (CS8652/CS9058 family), never a newer language silently.
    • Roslyn has one parser that is gated per compilation by CSharpParseOptions.LanguageVersion. "The parser for that version" means the newest compiler running with each project's LanguageVersion, taken from MSBuild evaluation (LangVersion, else the TFM default, so default, latest, preview and omission all resolve as the SDK resolves them).
    • Never fall back to CSharpParseOptions.Default for a project-owned document.
  3. Several projects with different versions in one solution:
    • Each project, and each target framework of a multi-targeted project, keeps its own parse options.
    • A file linked into two projects is checked under each project's version. Its diagnostics follow the active target framework ([NETFX-CONTEXT]).
    • Cross-project features (references, rename, Fix All in solution) must not reparse one project's syntax under another's version.
  4. The syntax tier (tree-sitter) has no version gate. It must accept the newest syntax without false syntax errors in older projects; language-version errors come only from the compiler.
  5. SDK and compiler skew: when the loaded SDK's analyzers or source generators are built against a newer Roslyn than SharpLsp ships (CS9057), report it in plain words and keep the rest of the project working ([REFACTOR-NEVER-SILENT]).
  6. File-based apps (dotnet run file.cs) use the same language version dotnet build file.cs would ([SCRIPTING-FILEBASED-SPEC]).

Spec work

Add a [CSHARP-LANGVERSION] section to the C# spec covering resolution, per-project and per-TFM parse options, the compiler/SDK skew policy and the tree-sitter policy. Cross-reference it from [NETFX-CONTEXT] and the Razor spec, since Razor already carries the project's CSharpParseOptions.LanguageVersion. F# has the same concept (<LangVersion> for FCS); F# goes first per project rules, so open an F# sibling issue.

Tests (when fixing)

Use real projects in one solution:

  • A: net11.0 (C# 15) with the union above, matched exhaustively in a switch.
  • B: net10.0 (C# 14) containing the same union.
  • C: net8.0 with <LangVersion>preview</LangVersion>.
  • D: multi-targeted net10.0;net11.0.

Assert:

  • A has zero diagnostics and hover/definition/references/rename work on ContentBody and its cases.
  • A missing union case is the compiler's exhaustiveness warning.
  • B reports exactly the build's language-version error, not CS0246/CS1001 noise.
  • C parses unions.
  • D's diagnostics switch with the active target framework.
  • Outline and folding show the union in every project.

Keep this issue open until the latest published release's own binaries pass this against a real C# 15 project.

主要言語
TypeScript
スター
185
フォーク
12
平均マージ
7時間 33分
マージ済み PR(30日)
63

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

  • Dockerfile・Docker Compose ファイルなし
  • プルリクエストのテンプレートあり
  • コントリビューションガイドなし

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

Nimblesite/SharpLsp のほかの issue

Nimblesite/SharpLsp の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。