Sdk: Defining a hook for Custom TargetFramework Parsing

Created on 31 Mar 2020  路  3Comments  路  Source: dotnet/sdk

Sdk parses TargetFramework into TargetFrameworkIdentifier, TargetFrameworkVersion and TargetFrameworkMoniker. A bunch of sdk and msbuild targets depends on correct values of these derived properties. The parsing fails if TargetFramework is not a defined value.

The TargetFramework parsing is done https://github.com/dotnet/sdk/blob/master/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.TargetFrameworkInference.targets#L47

This file automatically gets imported just after the .csproj by the sdk.
When we use custom Target Framework, we need to change the value of tfm to some already defined value before this parsing evaluation. Otherwise, it will result in Unsupported TargetFramework Error while running targets.

There are 2 work arounds that we use to address this:-

  • Parsing out the rid part in Directory.Build.Props file. (has a lot of side effects)
  • Do the entire sdk parsing in directory.Build.Props and set the derived properties ( a lot of duplication and is meant to break if sdk makes any changes in parsing)

The ask here is to provide a hook to do this parsing(either of just tfm or both tfm and the derived props) before the sdk parses the tfm.

cc @ericstj @ViktorHofer

Most helpful comment

Why not provide a hook for importing build files before the contents of the SDK targets?

Here: Before BeforeCommon*.targets
and
Here: Before/After LanguageTargets
and
Here: after NuGet's Pack.targets

Those 4 are the most critical points a dev would need to inject the build files to customize their build.

All 3 comments

The hook would look like some conditional import of a targets files based on some convention (property / name / or otherwise as deemed appropriate).

@sfoslund Here's another issue you could tackle.

Why not provide a hook for importing build files before the contents of the SDK targets?

Here: Before BeforeCommon*.targets
and
Here: Before/After LanguageTargets
and
Here: after NuGet's Pack.targets

Those 4 are the most critical points a dev would need to inject the build files to customize their build.

Was this page helpful?
0 / 5 - 0 ratings