Skip to content

Add task that generates .props file with SourceLink info for a source package #35

Open

Description

@tmat

To provide SourceLink information for a source package to the project that references such package one can write a task that generates .props file below and include it in the build directory of the source package (the below example is from Microsoft.CodeAnalysis.PooledObjects package):

  <ItemGroup>
    <SourceRoot Include="$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)..\contentFiles\cs\$(TargetFramework)\'))"
                SourceLinkUrl="https://raw.githubusercontent.com/dotnet/roslyn/3590c5574234ecac536c98d69d8a892e61feb4e3/src/Dependencies/PooledObjects/*"/>
  </ItemGroup>

The target in Roslyn repository that generates the above props is:

<Target Name="GenerateSourcePackagePropsFile"
          BeforeTargets="Build"
          Condition="'$(TargetFrameworks)' == '' or '$(IsCrossTargetingBuild)' == 'true'"
          Outputs="$(_SourcePackagePropsFilePath)">
    
    <PropertyGroup>
      <_RelativePath>$([MSBuild]::MakeRelative($(RepoRoot), $(MSBuildProjectDirectory)))</_RelativePath>
      
      <!-- TODO: Proper URL escaping -->
      <_RawUrl>$(RepositoryRawUrl)/$(GitHeadSha)/$(_RelativePath.Replace('\', '/'))/*</_RawUrl>
      
      <_Content>
        <![CDATA[<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <SourceRoot Include="%24([System.IO.Path]::GetFullPath('%24(MSBuildThisFileDirectory)..\contentFiles\cs\%24(TargetFramework)\'))"
                SourceLinkUrl="$(_RawUrl)"/>
  </ItemGroup>
</Project>]]>
      </_Content>
    </PropertyGroup>
    <MakeDir Directories="$(_SourcePackagePropsDirPath)" />
    <WriteLinesToFile File="$(_SourcePackagePropsFilePath)" Lines="$(_Content)" Overwrite="true" Encoding="UTF-8"/>
    <ItemGroup>
      <FileWrites Include="$(_SourcePackagePropsFilePath)" />
    </ItemGroup>
  </Target>

Generalize this target to a build task and expose it from Microsoft.SourceLink.Common package.

To complete the end-to-end scenario we would want to also have NuGet Pack support for source packages. Then we could integrate the above task into the build system automatically when Microsoft.SourceLink.* package is referenced.

Activity

  1. added this to the future milestone on Oct 8, 2019
  2. added
    enhancement
    on Apr 1, 2020
  3. modified the milestones: future, 1.1.0 on Apr 1, 2020
  4. clairernovotny commented on Apr 1, 2020

    @clairernovotny
    Contributor

    This works if the source package is published on GitHub and rebuilt. What about existing source packages or those where the source isn't published?

    Source Link does embed them as untracked sources currently but does not have a source root for it so the path is not deterministic.

    Adding this to the targets worked and handles all source packages.

      <ItemGroup>
        <SourceRoot Include="$(NuGetPackageRoot)" Condition="'$(NuGetPackageRoot)' != ''" />
      </ItemGroup>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions