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

Abierto
#587 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
25/100
Tipo de issue
Nueva funcionalidad
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
csharp, typescript

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
TypeScript
Estrellas
185
Forks
12
Merge medio
7 h 33 min
PR fusionados (30 d)
63

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

  • Sin Dockerfile ni archivo de Docker Compose
  • Tiene una plantilla de pull request
  • Sin guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de Nimblesite/SharpLsp

Todos los issues de Nimblesite/SharpLsp

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.