Skip to content

Commit

Permalink
Handle syntax trees with empty path
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed May 12, 2022
1 parent 37e59b2 commit 4e0d019
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 31 deletions.
67 changes: 39 additions & 28 deletions src/Workspaces/Core/Portable/Workspace/Solution/ProjectState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,50 +313,35 @@ public override AnalyzerConfigOptions GetOptions(SyntaxTree tree)
var cache = GetCache();
if (documentId != null && _projectState.DocumentStates.TryGetState(documentId, out var documentState))
{
return GetOptions(cache, documentState, tree.FilePath);
var result = GetOptions(cache, documentState);
if (result != null)
{
return result;
}
}

return GetOptionsForSourcePath(cache, tree.FilePath);
}

internal async ValueTask<StructuredAnalyzerConfigOptions?> GetOptionsAsync(DocumentState documentState, CancellationToken cancellationToken)
{
// We need to work out path to this document. Documents may not have a "real" file path if they're something created
// as a part of a code action, but haven't been written to disk yet.
var projectFilePath = _projectState.FilePath;
string? effectiveFilePath = null;

if (documentState.FilePath != null)
{
effectiveFilePath = documentState.FilePath;
}
else if (documentState.Name != null && projectFilePath != null)
{
var projectPath = PathUtilities.GetDirectoryName(projectFilePath);

if (!RoslynString.IsNullOrEmpty(projectPath) &&
PathUtilities.GetDirectoryName(projectFilePath) is string directory)
{
effectiveFilePath = PathUtilities.CombinePathsUnchecked(directory, documentState.Name);
}
}

if (effectiveFilePath == null)
{
return null;
}

var cache = await _projectState._lazyAnalyzerConfigOptions.GetValueAsync(cancellationToken).ConfigureAwait(false);
return GetOptions(cache, documentState, effectiveFilePath);
return GetOptions(cache, documentState);
}

private StructuredAnalyzerConfigOptions GetOptions(in AnalyzerConfigOptionsCache cache, DocumentState documentState, string filePath)
private StructuredAnalyzerConfigOptions? GetOptions(in AnalyzerConfigOptionsCache cache, DocumentState documentState)
{
if (documentState.IsRazorDocument())
{
return _lazyRazorDesignTimeOptions ??= new RazorDesignTimeAnalyzerConfigOptions(_projectState.LanguageServices.WorkspaceServices);
}

var filePath = GetEffectiveFilePath(documentState);
if (filePath == null)
{
return null;
}

var options = GetOptionsForSourcePath(cache, filePath);
var workspace = _projectState._solutionServices.Workspace;

Expand All @@ -377,6 +362,32 @@ public override AnalyzerConfigOptions GetOptions(AdditionalText textFile)

private static StructuredAnalyzerConfigOptions GetOptionsForSourcePath(in AnalyzerConfigOptionsCache cache, string path)
=> cache.GetOptionsForSourcePath(path).ConfigOptions;

private string? GetEffectiveFilePath(DocumentState documentState)
{
if (!string.IsNullOrEmpty(documentState.FilePath))
{
return documentState.FilePath;
}

// We need to work out path to this document. Documents may not have a "real" file path if they're something created
// as a part of a code action, but haven't been written to disk yet.

var projectFilePath = _projectState.FilePath;

if (documentState.Name != null && projectFilePath != null)
{
var projectPath = PathUtilities.GetDirectoryName(projectFilePath);

if (!RoslynString.IsNullOrEmpty(projectPath) &&
PathUtilities.GetDirectoryName(projectFilePath) is string directory)
{
return PathUtilities.CombinePathsUnchecked(directory, documentState.Name);
}
}

return null;
}
}

/// <summary>
Expand Down
12 changes: 9 additions & 3 deletions src/Workspaces/CoreTest/SolutionTests/SolutionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3391,14 +3391,16 @@ public async Task ReplacingTextMultipleTimesDoesNotRootIntermediateCopiesIfCompi
[InlineData("a/proj.csproj", "a/b/.editorconfig", "a/b/test.cs", "*.cs", false, true)]
[InlineData("a/proj.csproj", "a/.editorconfig", null, "*.cs", true, true)]
[InlineData("a/proj.csproj", "a/b/.editorconfig", null, "*.cs", false, false)]
[InlineData("a/proj.csproj", "a/.editorconfig", "", "*.cs", true, true)]
[InlineData("a/proj.csproj", "a/b/.editorconfig", "", "*.cs", false, false)]
[InlineData(null, "a/.editorconfig", "a/b/test.cs", "*.cs", false, true)]
[InlineData(null, "a/.editorconfig", null, "*.cs", false, false)]
[InlineData("a/proj.csproj", "a/.editorconfig", null, "*test.cs", false, true)]
public async Task EditorConfigOptions(string projectPath, string configPath, string sourcePath, string pattern, bool appliedToEntireProject, bool appliedToDocument)
{
projectPath = (projectPath != null) ? Path.Combine(TempRoot.Root, projectPath) : null;
projectPath = string.IsNullOrEmpty(projectPath) ? projectPath : Path.Combine(TempRoot.Root, projectPath);
configPath = Path.Combine(TempRoot.Root, configPath);
sourcePath = (sourcePath != null) ? Path.Combine(TempRoot.Root, sourcePath) : null;
sourcePath = string.IsNullOrEmpty(sourcePath) ? sourcePath : Path.Combine(TempRoot.Root, sourcePath);

using var workspace = CreateWorkspace();
var projectId = ProjectId.CreateNewId();
Expand All @@ -3423,8 +3425,12 @@ public async Task EditorConfigOptions(string projectPath, string configPath, str
var documentOptions = await document.GetOptionsAsync(CancellationToken.None);
Assert.Equal(appliedToDocument, documentOptions.GetOption(FormattingOptions2.UseTabs));

var syntaxTree = await document.GetSyntaxTreeAsync();
var documentOptionsViaSyntaxTree = document.Project.State.AnalyzerOptions.AnalyzerConfigOptionsProvider.GetOptions(syntaxTree);
Assert.Equal(appliedToDocument, documentOptionsViaSyntaxTree.TryGetValue("indent_style", out var value) == true && value == "tab");

var projectOptions = document.Project.GetAnalyzerConfigOptions();
Assert.Equal(appliedToEntireProject, projectOptions?.AnalyzerOptions.TryGetValue("indent_style", out var value) == true && value == "tab");
Assert.Equal(appliedToEntireProject, projectOptions?.AnalyzerOptions.TryGetValue("indent_style", out value) == true && value == "tab");
}
}
}

0 comments on commit 4e0d019

Please sign in to comment.