dotnet / dotnet/msbuild

'copy local' references get skipped if they are only used in XAML

Open
#3,812 1 comment 2 reactions 0 assignees View on GitHub
triaged
Dominant language
C#
Stars
5.5k
Forks
1.5k
Avg merge
1d 8h
Merged PRs (30d)
141

Description

(Moved from `roslyn` project [as requested](
https://github.com/dotnet/roslyn/issues/25349#issuecomment-41850305).)

If a second-level copy local project reference is only used in XAML (i.e., not in compiling code), MSBuild doesn't recognize that it needs to copy it to the output directory.

### Steps to Reproduce

1. create a solution with a WPF Windows application project (project **A**)
2. add a WPF class library project (project **B**). Add a project reference so you can use B within A.
3. in project B, reference a library that you will only use from within XAML, such as FontAwesome.WPF in our example. Make sure the library is set to **Copy Local**.
4. in project B, create a user control like this:

```xml

```

5. in project A, use the user control somewhere.
6. compile the solution. This will work.
7. debug or run the solution. This fails.

### Expected Behavior
In step 6, MSBuild should recognize that FontAwesome.WPF must be copied to project **A**'s output directory.

### Actual Behavior
MSBuild incorrectly believes that FontAwesome.WPF isn't actually being used and thus doesn't need copying. As a result, when actually trying to *run* the code, `PresentationFramework` tries and fails to load the library:

```
System.Windows.Markup.XamlParseException: 'Could not load file or assembly 'FontAwesome.WPF, PublicKeyToken=0758b07a11a4f466' or one of its dependencies. The system cannot find the file specified.'
```

### Workaround
A dummy method somewhere within project **B** causes MSBuild to copy the library. For example:

```csharp
private static void _FontAwesome_MSBuildXamlFix()
{
/*
* WORKAROUND
* we need this method so that FontAwesome.WPF.dll gets copied as part of the build process
*
* https://stackoverflow.com/a/18221455/1600
*/

var type = typeof(FontAwesome.WPF.FontAwesome);
Console.WriteLine(type.FullName);
}
```

### Environment data

#### `msbuild /version` output:

Microsoft (R) Build Engine version 15.8.169+g1ccb72aefa for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

15.8.169.51996

#### OS info:

Version 1809 (OS Build 17763.1)

#### If applicable, version of the tool that invokes MSBuild (Visual Studio, dotnet CLI, etc):

VS Enterprise 2017 15.8.5

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.