Skip to content

fileBasedApps: unit tests, no-csproj cone detection, language-ID check#9472

Draft
Copilot wants to merge 2 commits into
mainfrom
copilot/review-issue-2369
Draft

fileBasedApps: unit tests, no-csproj cone detection, language-ID check#9472
Copilot wants to merge 2 commits into
mainfrom
copilot/review-issue-2369

Conversation

Copilot AI commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Improves the "Convert to Project" command's FBA entry-point discovery with three behavioural changes and adds unit test coverage for the previously untestable detection logic.

Changes

Testability refactor (convertToProject.ts)

  • const enum FileBasedAppKindexport enum; detectFileBasedAppKind now exported with an injectable readFileHead parameter (defaults to the real fs impl), eliminating the need to mock fs in tests.
  • Real file-reading logic extracted into private defaultReadFileHead.

No-.csproj-cone detection (item 2)

  • New exported isInProjectCone(filePath, csprojDirs) walks ancestor directories to check for a .csproj.
  • pickAndConvertToProject now collects all .csproj locations via findFiles, then includes any .cs file not inside any project cone in the picker — even without #!/#: markers — since such files have no project to belong to.

VS Code language ID instead of file extension (item 3)

  • Right-click path: opens the document (or finds it if already open) and checks document.languageId === 'csharp' instead of filePath.endsWith('.cs').
  • Palette path: supplements findFiles('**/*.cs') with any already-open textDocuments whose languageId is csharp but whose path wasn't caught by the glob.

SDK detection investigation (item 4)

dotnet project in .NET 10 only exposes convert (no dry-run/detect), and dotnet run --file actually runs the program. No viable CLI detection path exists today. A TODO in pickAndConvertToProject records this and flags workspace/_ms_fileBasedProgramEntryPoints as the right long-term replacement once Roslyn exposes it.

Unit tests (test/lsptoolshost/unitTests/convertToProject.test.ts)

20 new tests across detectFileBasedAppKind and isInProjectCone:

// No fs mocking needed — tests inject a string reader directly
test('returns Shebang for a file starting with #!', () => {
    expect(detectFileBasedAppKind('/a/app.cs', makeReader('#!/usr/bin/env dotnet\n...'))).toBe(FileBasedAppKind.Shebang);
});

test('returns true when the file is in a subdirectory of a csproj directory', () => {
    const csprojDirs = new Set(['/workspace/project']);
    expect(isInProjectCone('/workspace/project/src/Foo.cs', csprojDirs)).toBe(true);
});

Covers: shebang, #: directives, BOM handling, unreadable files, 5-line scan cutoff, and all isInProjectCone ancestor/sibling permutations.

Implements microsoft/vscode-dotnettools#2369.

Adds a new `dotnet.convertToProject` command that lets users convert
a file-based C# app to a project-based app by running
`dotnet project convert <file>` in an integrated terminal.

Two entry points are provided:
- Right-click on a .cs file in the Explorer or editor → "Convert C# File-based App to Project"
- Command palette → "Convert C# File-based App to Project" → quick pick of all
  discoverable file-based app entry points in the workspace (files starting with
  `#!` or containing `#:` directives)

Co-authored-by: mwiemer-microsoft <80539004+mwiemer-microsoft@users.noreply.github.com>
Copilot AI changed the title Add "Convert C# File-based App to Project" command (fixes vscode-dotnettools#2369) Add "Convert C# File-based App to Project" command Jun 25, 2026
Copilot AI requested a review from mwiemer-microsoft June 25, 2026 18:55
…ne, language-ID check

- Export `FileBasedAppKind` enum and `detectFileBasedAppKind` with injectable
  reader so unit tests don't need fs mocks
- Export `isInProjectCone` helper (walk ancestor dirs to find .csproj)
- pickAndConvertToProject: include C# files outside any .csproj cone as FBA
  entry points even when they lack explicit #! / #: markers
- pickAndConvertToProject: augment the findFiles result with any open text
  documents that VS Code already treats as csharp language but lack .cs ext
- convertToProject (right-click path): check document.languageId === 'csharp'
  via openTextDocument instead of filePath.endsWith('.cs')
- Add TODO noting that the .NET 10 SDK has no CLI command for FBA detection
  (only dotnet project convert); flag the Roslyn LSP request as the future fix
- Add 20 unit tests covering shebang, directives, BOM, None edge cases, and
  all isInProjectCone permutations

Co-authored-by: mwiemer-microsoft <80539004+mwiemer-microsoft@users.noreply.github.com>
Copilot AI changed the title Add "Convert C# File-based App to Project" command fileBasedApps: unit tests, no-csproj cone detection, language-ID check Jun 26, 2026
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.

2 participants