microsoft / WindowsAppSDK

The Windows App SDK empowers all Windows desktop apps with modern Windows UI, APIs, and platform features, including back-compat support, shipped via NuGet.
https://docs.microsoft.com/windows/apps/windows-app-sdk/
MIT License
3.85k stars 323 forks source link

Error "Unable to activate Windows Store app" when Directory.Build.targets changes assembly name.Bug title #4606

Closed voroninp closed 3 months ago

voroninp commented 3 months ago

Describe the bug

I define a prefix for assemblies in Directory.Build.props:

<AssemblyPrefix Condition="$(AssemblyPrefix) == ''">MorozCo</AssemblyPrefix>
<AddPrefixToAssembly Condition="$(AddPrefixToAssembly) == ''">true</AddPrefixToAssembly>
<AddPrefixToNamespace Condition="$(AddPrefixToNamespace) == ''">true</AddPrefixToNamespace>

And set names in Directory.Build.targets:

<PropertyGroup>
  <AssemblyName Condition="$(AssemblyName) == ''">$(MSBuildProjectName)</AssemblyName>
  <AssemblyName Condition="$(AddPrefixToAssembly) == 'true' and $(AssemblyPrefix) != '' and !$(AssemblyName.StartsWith(AssemblyPrefix))">$(AssemblyPrefix).$(AssemblyName)</AssemblyName>
</PropertyGroup>

<PropertyGroup>
  <RootNamespace Condition="$(RootNamespace) == ''">$(AssemblyName.Replace(' ', '_'))</RootNamespace>
  <_RootNamespacePrefix>$(AssemblyPrefix.Replace(' ', '_'))</_RootNamespacePrefix>
  <RootNamespace Condition="$(AddPrefixToNamespace) == 'true' and $(_RootNamespacePrefix) != '' and !$(RootNamespace.StartsWith(_RootNamespacePrefix))">$(_RootNamespacePrefix).$(RootNamespace)</RootNamespace>
</PropertyGroup>

If I do not set <AddPrefixToAssembly>false</AddPrefixToAssembly> in my MAUI project, I get this error:

Unable to activate Windows Store app 'com.companyname.myapp_9zz4h110yvjzm!App'. The activation request failed with error 'The system cannot find the file specified'.

Steps to reproduce the bug

See this repo https://github.com/voroninp/BadWindows

Expected behavior

No response

Screenshots

No response

NuGet package version

None

Packaging type

Unpackaged

Windows version

No response

IDE

Visual Studio 2022-preview

Additional context

Advised here to create issue.

evelynwu-msft commented 3 months ago

@voroninp We derive a number of properties from the value of AssemblyName. Because stuff in Directory.Build.targets runs after basically everything else this simply isn't a scenario that can be reasonably supported. My recommendation would be that you take the logic you currently have in Directory.Build.targets and place it into some common .targets file that each .csproj manually imports at the bottom. I know this isn't as elegant as you would like but it's just how MSBuild works.

voroninp commented 3 months ago

Are not SDK targets run the last?

evelynwu-msft commented 3 months ago

Windows App SDK is a Nuget package, not a .NET SDK, so its .targets runs before Directory.Build.targets.