Showing posts with label visual studio 2010. Show all posts
Showing posts with label visual studio 2010. Show all posts

Saturday, June 11, 2011

Visual Studio 2010 Fixing "The referenced project XX does not exist" Error

Recently one of my friend was facing a weird issue while building our application. The application was developed in C#. We were using MSBuild as the build tool.

What was the issue?

The issue was simple, she was not able to compile the application.

The issue was special in many ways because, all of us (i.e. her team members) were able to compile the project just fine. Even the continuous integration service - Jenkins (formally Hudson), was able to compile the application. We were not able to compile the application only on her machine. The build failed with the error

The project "AB.dosomeprojects.Services.FillImporter.Service.csproj" referred the project "abcdefgh.dosomeprojects.Common.csproj" which was in the path "..\..\..\abcdefgh.dosomeprojects\abcdefgh.dosomeprojects.Common". The path was relative to the Service project directory.

The directory structure was correct, Common project was correctly reffered from the Service project, but still we were not able to build the app on her machine.

Initially, we thought that there must me some local changes on her machine because of which, she was not able to run the build on her machine.  Build was not passing neither with Visual Studio 2010 nor with MSBuild and that too only on her machine.

We looked for any local changes on her machine, but unfortunately her machine was clean.  There were no local changes.  We were simply clueless!  Why does the project build fine on other developers machine while it does not build on her machine?

After some intense Googling, we found an interesting hidden treasure shipped silently by Microsoft Visual Studio 2010.  I call such issues as hidden treasure because, I can hardly believe my ears when I hear/read about such an issue.

I am sure it will come as an surprise to you as well.  The issue is:

The Real Problem:

Visual Studio 2010 fails to build the project when the following conditions are met:
  1. Solution with multiple projects exists, where there exists dependency relationships among them
  2. The sum of the following two path length is exactly added up to 259 characters (= MAX_PATH – 1) 
    1. The path of a referencing project’s directory.
    2. The relative path to a referenced project from the current directory (= a referencing project’s directory).
Hmm, cryptic information lets decrypt it.

Condition 1: We have solution with multiple projects where there exists dependency relationships among them

Yes, we do have the solution with multiple projects in it and one project depends on the other.  In our case the AB.dosomeprojects.Services.FillImporter.Service.csproj depends on the abcdefgh.dosomeprojects.Common.csproj project.  Hence, condition 1 is met.

Condition 2: The sum of the following two path length is exactly added up to 259 characters (= MAX_PATH – 1)

This one is tricky.  Which path sum are we talking about?

Condition 2.1: The path of a referencing project’s directory.

The path of the referencing project's directory, in our case the path of the AB.dosomeprojects.Services.FillImporter.Service.csproj on the file system, which was, "E:\projects\ABC\SVN\dosomeprojectsweb\trunk\AB.dosomeprojects.Services\AB.dosomeprojects.Services.FillImporter\AB.dosomeprojects.Services.FillImporter.Service" has total length of 158 chars.

Condition 2.2: The relative path to a referenced project from the current directory (= a referencing project’s directory).

The relative path of the referenced project from the current directory, in our case its the relative path of abcdefgh.dosomeprojects.Common.csproj, which is, "..\..\..\abcdefgh.dosomeprojects\abcdefgh.dosomeprojects.Common\abcdefgh.dosomeprojects.Common.csproj" has total length of 101 chars.

The sum total of these two path lengths: 158 + 101 = 259!


Wow! condition 2 is also met.

As I said, we have uncovered an hidden treasure of Visual Studio 2010.  Its unable to build the project when the path length of referencing project + path length of relative path to a referenced project = 259!

If the sum total is one less or one greater than 259 it all works fine! On other developers machine the total path length was less than or greater than 259 but was never equal to 259 and because of this reason we were all able to build the project but she was not able to build the project on her machine! Weird isn't?

This issue occurs due to a bug in the Path.GetFullPath in .NET Framework library.  This is a known issue in Visual Studio 2010.

The Fix:

The workaround is pretty straight forward, rename one of the folders a bit longer or a bit shorter and make sure that, the sum of the two path length is NOT equal to 259 chars.

For more information on the issue visit the offical Microsoft Knowledge base article here http://support.microsoft.com/kb/2516078

Now you know, we are having so much fun with .Net (sarcasm intended)!

Saturday, May 28, 2011

How to build the web project on the CI server without Visual Studio 2010

On one of the ASP.Net project, we were using Visual Studio 2010 IDE and MSBuild, to compile and build the application.  Pretty standard setup for a web project right?.  On developer machine all worked very smoothly, since each developer had Visual Studio 2010 installed locally on their machines.

We setup a continuous integration (CI) server Jenkins (formally Hudson) for this project.  Naturally, we didn't wanted to install Visual Studio 2010 on the CI server.  But we still wanted to build our app on the CI server.  Hence, we installed Microsoft Windows SDK 7.1 and all its components on the CI server.  After installing the SDK, we were able to build all projects except the web project on the CI server.

The error that we were getting was

Obviously, since the Visual Studio 2010 was not installed on the CI server, the path mentioned in the error didn't exist and that's why the web project was not able to build.

I opened the csproj file in Notepad++, to find the reference of "Microsoft.WebApplication.targets".  I found a reference of "Microsoft.WebApplication.targets" in the csproj which looked liked 

Indeed the web csproj file had an import statement, which added the reference to "Microsoft.WebApplication.targets". But I was not sure why this reference was needed in the first place.

My first attempt at fixing the CI server:

I removed the reference of "Microsoft.WebApplication.targets" from the web csproj file and tried building the application using MSBuild.  To my surprise, the app build fine, it ran all the tests without any issue.  My confidence grew a little, I thought I had the solution.  I thought the reference was added to the csproj by Visual Studio 2010, but was never used.

I committed the fix, build went green on the CI Server as well!

After a few hours, some developers complained that, when they now open up the web project, Visual Studio 2010 prompted for converting the project into a Visual Studio 2010.  Certainly, this behaviour was triggered after I removed the import statement from the web csproj file.

Its pretty clear now, why the import was required.  Visual Studio 2010 identifies that, the project is a Visual Studio 2010 Web project using the import of "Microsoft.WebApplication.targets".  Hence, although the build run's fine using MSbuild on developer machine as well as on CI server this is not the right fix!

My second attempt at fixing the CI server:

Googling a little I found that, there are only two options to fix this particular issue.
  • Install Visual Studio 2010 on the CI server
  • Copy the "Microsoft.WebApplication.targets" file manually from any developer machine to the CI server
It was sad to see that there was no standalone installer that installed the "Microsoft.WebApplication.targets" at the desired location.  We had to either install the full Visual Studio 2010 or copy the file manually.


I decided to chose the lesser evil of the two.  We copied the file "Microsoft.WebApplication.targets" from my machine (located at path C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications on a Windows 7 64 Bit installation) onto the CI server (at the path C:\Program Files\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications.  CI server has Windows 2003 OS installed on it.) and then ran the build.  Yep! It worked!  The build went green on the CI server.

I know, its just an hack, but for now I had no other option!
Have some Fun!