Skip to content

Commit 1e8a368

Browse files
committed
Use the full file path as the target path when it's missing
1 parent 71676b1 commit 1e8a368

File tree

2 files changed

+93
-2
lines changed

2 files changed

+93
-2
lines changed

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/SourceGenerators/RazorSourceGenerator.RazorProviders.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ private static (SourceGeneratorProjectItem?, Diagnostic?) ComputeProjectItems((A
9494
}
9595
else
9696
{
97-
// If the TargetPath is not provided, we effectively assume its in the root of the project.
98-
relativePath = Path.GetFileName(additionalText.Path);
97+
// If the TargetPath is not provided, it could be a Misc Files situation, or just a project that isn't using the
98+
// Web or Razor SDK. In this case, we just use the physical path.
99+
relativePath = additionalText.Path;
99100
}
100101

101102
options.TryGetValue("build_metadata.AdditionalFiles.CssScope", out var cssScope);
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Razor;
6+
using Microsoft.CodeAnalysis.Text;
7+
using Microsoft.VisualStudio.Razor.LanguageClient.Cohost;
8+
using Xunit;
9+
using Xunit.Abstractions;
10+
11+
namespace Microsoft.VisualStudioCode.RazorExtension.Test.Endpoints.Shared;
12+
13+
public class ComputedTargetPathTest(ITestOutputHelper testOutputHelper) : CohostEndpointTestBase(testOutputHelper)
14+
{
15+
[Fact]
16+
public async Task NoGlobalConfig()
17+
{
18+
var doc1Path = FilePath(@"Pages\Index.razor");
19+
20+
// Creating a misc files project will mean that there is no globalconfig created, so no target paths will be set
21+
var document = CreateProjectAndRazorDocument("", miscellaneousFile: true);
22+
23+
var doc1 = document.Project.AddAdditionalDocument(
24+
doc1Path,
25+
SourceText.From("""
26+
<div>This is a page</div>
27+
"""),
28+
filePath: doc1Path);
29+
30+
var endpoint = new CohostDocumentSymbolEndpoint(IncompatibleProjectService, RemoteServiceInvoker);
31+
var result = await endpoint.GetTestAccessor().HandleRequestAsync(doc1, useHierarchicalSymbols: true, DisposalToken);
32+
Assert.NotNull(result);
33+
}
34+
35+
[Fact]
36+
public async Task NoGlobalConfig_MultipleFilesWithTheSameName()
37+
{
38+
var doc1Path = FilePath(@"Pages\Index.razor");
39+
var doc2Path = FilePath(@"Components\Index.razor");
40+
41+
// Creating a misc files project will mean that there is no globalconfig created, so no target paths will be set
42+
var document = CreateProjectAndRazorDocument("", miscellaneousFile: true);
43+
44+
var doc1 = document.Project.AddAdditionalDocument(
45+
doc1Path,
46+
SourceText.From("""
47+
<div>This is a page</div>
48+
"""),
49+
filePath: doc1Path);
50+
var doc2 = doc1.Project.AddAdditionalDocument(
51+
doc2Path,
52+
SourceText.From("""
53+
<div>This is a component</div>
54+
"""),
55+
filePath: doc2Path);
56+
57+
// Make sure we have a doc1 from the final project
58+
doc1 = doc2.Project.GetAdditionalDocument(doc1.Id).AssumeNotNull();
59+
60+
var endpoint = new CohostDocumentSymbolEndpoint(IncompatibleProjectService, RemoteServiceInvoker);
61+
var result = await endpoint.GetTestAccessor().HandleRequestAsync(doc1, useHierarchicalSymbols: true, DisposalToken);
62+
Assert.NotNull(result);
63+
64+
result = await endpoint.GetTestAccessor().HandleRequestAsync(doc2, useHierarchicalSymbols: true, DisposalToken);
65+
Assert.NotNull(result);
66+
}
67+
68+
[Fact]
69+
public async Task NotAllFilesHaveTargetPaths()
70+
{
71+
var doc1Path = FilePath(@"Pages\Index.razor");
72+
73+
// This will create a project with a globalconfig, and target paths for a single razor file
74+
var document = CreateProjectAndRazorDocument("""
75+
<div>This is a normal file with a target path
76+
""");
77+
78+
// Now add a file without updating the globalconfig
79+
var doc1 = document.Project.AddAdditionalDocument(
80+
doc1Path,
81+
SourceText.From("""
82+
<div>This is an extra document</div>
83+
"""),
84+
filePath: doc1Path);
85+
86+
var endpoint = new CohostDocumentSymbolEndpoint(IncompatibleProjectService, RemoteServiceInvoker);
87+
var result = await endpoint.GetTestAccessor().HandleRequestAsync(doc1, useHierarchicalSymbols: true, DisposalToken);
88+
Assert.NotNull(result);
89+
}
90+
}

0 commit comments

Comments
 (0)