Elliot Blackburn
Elliot Blackburn

Reputation: 4174

FAKE MSBuild step can't resolve references

I have a FAKE build script which will run perfectly fine if I build the project once using VisualStudio, but if I try and do a fresh build then it throws errors saying it can't find the namespace of both a number of nuget packages and my dependant visual studio projects.

I'm using FAKE's MSBuildRelease and passing in the .sln file like so:

Target "BuildWindowsProject" (fun _ ->
    MSBuildRelease
        ""
        "Build"
        [ "./Source/My.Project.sln" ]
        |> Log "Windows-Build: "
)

It's giving out a number of errors such as below, but these include both projects within the solution and external nuget packages.

The type or namespace name 'Caliburn' could not be found (are you missing a using directive or an assembly reference?)

I did notice that back in 2011 this was posted - MSBuild cannot find a reference but it's quite old and I can't find any more recent occurrences of the issue online.

It works just fine if I do a visual studio build first and then run my build script but this doesn't solve the problem for a build server. What am I doing wrong?

Upvotes: 0

Views: 766

Answers (1)

stukselbax
stukselbax

Reputation: 5935

You have to perform deep diagnostic by yourself, because the question under consideration requires the source code to be published here. It also can depend on the environment where the script is running, in order to get you the right answer.

You have an option on MSBuild utility to pass verbosity level of output. Use the following switch value to get detailed information about how the references are resolved during the build:

msbuild ./Source/My.Project.sln /t:Build /v:d

Using this command, you can navigate to the steps called ResolveAssemblyReference and ResolveProjectReference. You can see all the paths, where it will search for assemblies, and if it is succeded:

4>  Primary reference "System.Runtime.Remoting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089".
4>      Resolved file path is "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Runtime.Remoting.dll".
4>      Reference found at search path location "{TargetFrameworkDirectory}".
4>          For SearchPath "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\atlmfc\lib\amd64".
4>          Considered "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\atlmfc\lib\amd64\System.Runtime.Remoting.winmd", but it didn't exist.
4>          Considered "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\atlmfc\lib\amd64\System.Runtime.Remoting.dll", but it didn't exist.
4>          Considered "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\atlmfc\lib\amd64\System.Runtime.Remoting.exe", but it didn't exist.
4>          For SearchPath "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\lib\amd64".
4>          Considered "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\lib\amd64\System.Runtime.Remoting.winmd", but it didn't exist.
4>          Considered "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\lib\amd64\System.Runtime.Remoting.dll", but it didn't exist.
4>          Considered "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\lib\amd64\System.Runtime.Remoting.exe", but it didn't exist.
4>          For SearchPath "{TargetFrameworkDirectory}".
4>          Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Runtime.Remoting.winmd", but it didn't exist.
4>      This reference is not "CopyLocal" because it's a prerequisite file.
4>      The ImageRuntimeVersion for this reference is "v4.0.30319".

You also can examine which references were passed to the compiler if you look at commandline, which was generated by MSBuild and find out if there is a reference you are required in.

For cl:

4>  C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\x86_amd64\CL.exe 
...
/AI"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0" 
...    
/FU"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\mscorlib.dll" 
/FUE:\YourProj\bin\Debug\Your.Proj.dll 
/FU"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Configuration.dll" 
...
/errorReport:prompt 
YourSourceCodeFiles.cpp
/clr:nostdlib 

For csc:

2>  C:\Program Files (x86)\MSBuild\12.0\bin\Csc.exe
...
/reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\mscorlib.dll" 
/reference:E:\svn\PRM\trunk\PRM30\bin\Debug\Prm.Base.dll 
/reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Configuration.dll" 
/reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Core.dll" 
...

(most of parameters were omited for simplicity)

Upvotes: 2

Related Questions