Project Reference does not find project outputs if they are placed in a different folder
- Dominant language
- C#
- Stars
- 5.5k
- Forks
- 1.5k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 139
Description
### Steps to reproduce
We have an ASP.NET MVC project with some controllers that we want to test. In Visual Studio we created a solution containing the project and an MSTest unit test project that references it. In VS the build is successful and the unit tests are successfully run. When we use MSBuild it is unable to build the solution because it cannot find the MVC project output to place into the unit test project.
The issue, we believe, is that the $(OutDir) property is changed during the build process so the project reference logic is using the wrong path. In a typical web application the `Microsoft.WebApplication.targets` file is part of the project file. It "publishes" the project to the `_PublishedWebsites` folder. This is handled conditionally by comparing the output directory specified to MSBuild with the $(OutDir) variable in the project. If they differ than it appends this extra folder and then copies the "contents" of the site to the output.
For a solution that only contains an MVC app and support libraries then this works fine. You can specify the appropriate arguments when calling MSBuild and it'll "publish" the site. However if the solution contains other projects like console applications or even multiple sites then publishing will cause them to all be output to the same folder which won't work. To handle this case we have a custom build step (via YAML) that generates a `directory.build.targets` file in the solution root before the build runs. Within this custom .targets file is a conditional check to see if this is a website project. If it is then it changes the $(OutDir) variable which will cause the condition in `Microsoft.WebApplication.targets` to detect a difference and publish the site to a different folder.
When the unit test project is built it doesn't recognize the output of the project as being different and looks in the original path. Of course the files don't exist there so it fails.
Typical MVC project file
```xml
Debug
AnyCPU
2.0
Library
Properties
WebApplication1
WebApplication1
v4.7
true
44301
true
true
full
false
bin\
DEBUG;TRACE
prompt
4
pdbonly
true
bin\
TRACE
prompt
4
10.0
$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)
```
Generated `directory.build.targets` file in root of solution.
```xml
$(OutDir)publish\
```
Output of a build will be to `$(OutDir)publish\_PublishedWebsite`.
Test project with project reference.
```xml
net47
true
true
```
MSBuild command that was run: `/p:platform="any cpu" /p:configuration="Release" /p:VisualStudioVersion="15.0" /m`
### Expected behavior
Solution should build the same when called from Azure DevOps via MSBuild task as it does when called from Visual Studio. Unit tests should be able to use project references to locate outputs of other projects even if the other project uses a different output path.
### Actual behavior
When run from VS where the `directory.build.targets` file is not used then the project reference works correctly as the output is consistent with what it expected.
When run from a build triggered via Azure DevOps and using the modified output because of the addition of the `directory.build.targets` then the project output is not located. MSBuild is looking in the output directory that was defined in the project file of the project reference rather than what was actually used.
Relevant section from build log.
```text
ResolveAssemblyReferences:
2019-02-06T20:10:18.3922917Z Primary reference "WebApplication1".
2019-02-06T20:10:18.3922983Z Could not find dependent files. Expected file "D:\a\1\s\WebApplication1\bin\WebApplication1.dll" does not exist.
2019-02-06T20:10:18.3923056Z Could not find dependent files. Expected file "D:\a\1\s\WebApplication1\bin\WebApplication1.dll" does not exist.
2019-02-06T20:10:18.3923099Z Resolved file path is "D:\a\1\s\WebApplication1\bin\WebApplication1.dll".
2019-02-06T20:10:18.3923154Z Reference found at search path location "".
2019-02-06T20:10:18.3923373Z The ImageRuntimeVersion for this reference is "".
##[error]CSC(0,0): Error CS0006: Metadata file 'D:\a\1\s\WebApplication1\bin\WebApplication1.dll' could not be found
```
### Environment data
Visual Studio Build Azure DevOps Task: 1.146.0
`msbuild /version` output: 15.0
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.