提交 1f27d165 编写于 作者: M Matt Warren

Dont rely on link metdata being valid file path

上级 aae1ded0
......@@ -636,7 +636,7 @@ private async Task<ProjectId> LoadProjectAsync(string projectFilePath, IProjectF
var projectId = this.GetOrCreateProjectId(projectFilePath);
var name = Path.GetFileNameWithoutExtension(projectFilePath);
var projectName = Path.GetFileNameWithoutExtension(projectFilePath);
var projectFile = await loader.LoadProjectFileAsync(projectFilePath, _properties, cancellationToken).ConfigureAwait(false);
var projectFileInfo = await projectFile.GetProjectFileInfoAsync(cancellationToken).ConfigureAwait(false);
......@@ -660,10 +660,14 @@ private async Task<ProjectId> LoadProjectAsync(string projectFilePath, IProjectF
var docs = new List<DocumentInfo>();
foreach (var docFileInfo in docFileInfos)
{
string name;
ImmutableArray<string> folders;
GetDocumentNameAndFolders(docFileInfo.LogicalPath, out name, out folders);
docs.Add(DocumentInfo.Create(
DocumentId.CreateNewId(projectId, debugName: docFileInfo.FilePath),
Path.GetFileName(docFileInfo.LogicalPath),
GetDocumentFolders(docFileInfo.LogicalPath),
name,
folders,
projectFile.GetSourceCodeKind(docFileInfo.FilePath),
new FileTextLoader(docFileInfo.FilePath, defaultEncoding),
docFileInfo.FilePath,
......@@ -673,10 +677,14 @@ private async Task<ProjectId> LoadProjectAsync(string projectFilePath, IProjectF
var additonalDocs = new List<DocumentInfo>();
foreach (var docFileInfo in projectFileInfo.AdditionalDocuments)
{
string name;
ImmutableArray<string> folders;
GetDocumentNameAndFolders(docFileInfo.LogicalPath, out name, out folders);
additonalDocs.Add(DocumentInfo.Create(
DocumentId.CreateNewId(projectId, debugName: docFileInfo.FilePath),
Path.GetFileName(docFileInfo.LogicalPath),
GetDocumentFolders(docFileInfo.LogicalPath),
name,
folders,
SourceCodeKind.Regular,
new FileTextLoader(docFileInfo.FilePath, defaultEncoding),
docFileInfo.FilePath,
......@@ -709,7 +717,7 @@ private async Task<ProjectId> LoadProjectAsync(string projectFilePath, IProjectF
ProjectInfo.Create(
projectId,
version,
name,
projectName,
assemblyName,
loader.Language,
projectFilePath,
......@@ -746,18 +754,29 @@ private static Encoding GetDefaultEncoding(int codePage)
}
}
private static readonly char[] s_directorySplitChars = new char[] { Path.DirectorySeparatorChar };
private static readonly char[] s_directorySplitChars = new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar };
private static ImmutableArray<string> GetDocumentFolders(string logicalPath)
private static void GetDocumentNameAndFolders(string logicalPath, out string name, out ImmutableArray<string> folders)
{
var logicalDirectory = Path.GetDirectoryName(logicalPath);
var pathNames = logicalPath.Split(s_directorySplitChars, StringSplitOptions.RemoveEmptyEntries);
if (pathNames.Length > 0)
{
if (pathNames.Length > 1)
{
folders = pathNames.Take(pathNames.Length - 1).ToImmutableArray();
}
else
{
folders = ImmutableArray.Create<string>();
}
if (!string.IsNullOrEmpty(logicalDirectory))
name = pathNames[pathNames.Length - 1];
}
else
{
return logicalDirectory.Split(s_directorySplitChars, StringSplitOptions.None).ToImmutableArray();
name = logicalPath;
folders = ImmutableArray.Create<string>();
}
return ImmutableArray.Create<string>();
}
private void CheckDocuments(IEnumerable<DocumentFileInfo> docs, string projectFilePath, ProjectId projectId)
......
......@@ -311,6 +311,11 @@
<ItemGroup>
<EmbeddedResource Include="TestFiles\VisualBasicProject_VisualBasicProject_WithoutVBTargetsImported.vbproj" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="TestFiles\CSharpProject_CSharpProject_BadLink.csproj">
<SubType>Designer</SubType>
</EmbeddedResource>
</ItemGroup>
<ImportGroup Label="Targets">
<Import Project="..\..\Tools\Microsoft.CodeAnalysis.Toolset.Open\Targets\VSL.Imports.targets" />
<Import Project="..\..\..\build\VSL.Imports.Closed.targets" />
......
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -->
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{686DD036-86AA-443E-8A10-DDB43266A8C4}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>CSharpProject</RootNamespace>
<AssemblyName>CSharpProject</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AssemblyOriginatorKeyFile>snKey.snk</AssemblyOriginatorKeyFile>
<MyProperty>xyz</MyProperty>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="System" />
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<MyNode Include="CSharpClass.cs" />
<MyNode Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="@(MyNode)">
<Link>$([MSBuild]::MakeRelative($(MyProperty), %(Identity))</Link>
</Compile>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
......@@ -2607,5 +2607,18 @@ public void TestAddRemoveProjectReference()
Assert.Equal(true, projFileText.Contains(@"<ProjectReference Include=""..\CSharpProject\CSharpProject.csproj"">"));
}
}
[Fact, Trait(Traits.Feature, Traits.Features.Workspace)]
[WorkItem(1101040)]
public void TestOpenProject_BadLink()
{
CreateFiles(GetSimpleCSharpSolutionFiles()
.WithFile(@"CSharpProject\CSharpProject.csproj", GetResourceText(@"CSharpProject_CSharpProject_BadLink.csproj")));
var ws = MSBuildWorkspace.Create();
var proj = ws.OpenProjectAsync(GetSolutionFileName(@"CSharpProject\CSharpProject.csproj")).Result;
var docs = proj.Documents.ToList();
Assert.Equal(3, docs.Count);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册