19

Does anyone have a method to overcome the 260 character limit of the MSBuild tool for building Visual Studio projects and solutions from the command line? I'm trying to get the build automated using CruiseControl (CruiseControl.NET isn't an option, so I'm trying to tie it into normal ant scripts) and I keep on running into problems with the length of the paths. To clarify, the problem is in the length of paths of projects referenced in the solution file, as the tool doesn't collapse paths down properly :(

I've also tried using DevEnv which sometimes works and sometimes throws an exception, which isn't good for an automated build on a separate machine. So please don't suggest using this as a replacement.

And to top it all, the project builds fine when using Visual Studio through the normal IDE.

1
  • I notice that when vs2008 does a build it seems to run a different set of msbuild targets than simple msbuild executed under cc.net - perhaps the answer is in one of those targets
    – Richard
    Sep 26, 2008 at 15:03

8 Answers 8

12

It seems that it is limitation of the MSBuild. We had the same problem, and in the end, we had to get paths shortened, because did not find any other solution that worked properly.

4
  • After several days of effort, this seems to be what I'm going to have to request. Thankfully a new (much shorter) path structure is being proposed for these projects, so the problem should go away then :)
    – workmad3
    Sep 30, 2008 at 11:24
  • System.IO of the .Net framework (v1.0-v3.5) has this limitation and MSBuild is using .Net. (Currently the only way around this is using PInvoke about everywhere a path is used.) Dec 19, 2008 at 0:01
  • 2
    Can anyone confirm that this is not a problem in msbuild 4.0? Jun 15, 2012 at 20:00
  • It seems MSBuild 16.0 is finally adding a fix for this, it does seem to have taken more than a decade to get there though. github.com/Microsoft/msbuild/issues/53#issuecomment-459062618
    – NeverCast
    May 2, 2019 at 3:13
11

The SUBST command stills seems to exist so remapping the root of your build folder to a drive letter may save some characters if Judah Himango's solution is no good.

3
  • They are already being SUBST'd to the most common root of the directory structure :( It would work if the path names weren't so infernally long though.
    – workmad3
    Sep 30, 2008 at 11:24
  • We're using subst to build the Silverlight Toolkit, it isn't perfect, but this hack is a good first step. +1 Jason Sep 14, 2009 at 5:49
  • This is able to fix our problems with MSBuild filepaths, at least for now.
    – nebffa
    Oct 25, 2014 at 2:41
5

I solved similar issue by adjusting CSPROJ-file:

<BaseIntermediateOutputPath>$([System.IO.Path]::GetFullPath('$(MSBuildProjectDirectory)\..\..\..\Intermediate\$(AssemblyName)_$(ProjectGuid)\'))</BaseIntermediateOutputPath>

As the result during compilation CSC.EXE receives full path instead of relative one.

Thanks to harrydev for clue on how CSC.EXE operates with the paths.

1
  • This is extremely helpful in solving MSB3101: Could not write state file errors since the root cause of this is that the Path is too long. In addition you MUST expand out the path because MSBuild will simply append and of the ..\..\obj that you do to the path that is already too long.
    – aolszowka
    Mar 26, 2019 at 19:21
3

There are two kinds of long path problems relevant to build. One is paths that aren't really too long, but have lots of "..\" in them. Typically, these are references' HintPath values. MSBuild should normalize these paths down to below the max limit, so that they work.

The other kind of path is just plain too long. Sorry, but these just won't work. After looking at it a fair bit, the problem is that there just isn't sufficient API support for long paths. The BCL team (see their blog) had similar problems. Only some of the Win32 API's support the \?\ format. Arbitrary build tools, and probably 98% of apps out there, don't; and worse would probably behave badly (think of all the buffers sized for MAX_PATH).

We came to the conclusion that until there's a big ecosystem effort to make long paths work, or Windows comes up with some ingenious way to make them work anyway (like the short paths mangling?) long paths just aren't possible for MSBuild to support. Workarounds include subst, as you found; but if your tree just is simply too deep, your only options are to build it in fragments, or to shorten the folder names. Sorry.

Dan/MSBuild

2
  • Could you elaborate on the "..\" being a problem, we have relative file paths that are not very long. And even if converted to absolute paths are less than 200 characters, but they still fail in the csc.exe call.
    – nietras
    Oct 7, 2011 at 11:37
  • @nietras I realize you posted this over 8 years ago; but the root cause is that the entire path is passed when you use "..\" so for example if your relative path was "..\obj" but the current working directory was already over 260 characters you were already hosed. This is why doomer's answer above (which uses System.IO.Path.GetFullPath) works because it expands it to the absolute path BEFORE handing it off and getting mux'ed. Hopefully this helps someone else.
    – aolszowka
    Mar 26, 2019 at 19:23
2

I found the problem to be that when the C# compiler (csc.exe) is called it uses the projects directory path PROJECTDIRECTORY together with the output path OUTPUTPATH by simply appending them as:

PROJECTDIRECTORY+OUTPUTPATH

However, if the OUTPUTPATH is relative i.e. "..\..\Build\ProjectName\AnyCPU_Debug_Bin\" and the project directory is pretty long then the total length is longer than 259 characters since the path will be:

PROJECTPATH+"..\..\Build\ProjectName\AnyCPU_Debug_Bin\"

instead of an absolute path.

If csc.exe would make an absolute path before calling Win32 functions this would work. Since in our case the absolute path length is less than 160 characters.

For some reason the call to csc.exe from visual studio is then different from MSBuild than it is from visual studio. Do not know why.

In any case, the problem can be resolved by changing either or both PROJECTDIRECTORY and/or OUTPUTPATH paths.

1
  • This is why expanding the path (using System.IO.Path.GetFullPath) as in doomers answer works for a lot of people for anyone else encountering this.
    – aolszowka
    Mar 26, 2019 at 19:24
0

Have you tried DOS paths? Or the \\?\ prefix? The .NET BCL team blog has more info.

0

If the path length is 260, then there is warning resolving reference, for 259 or 261 of this error does not occur. I think there is msbuild bug.

0

I know there is already an accepted answer, but I had a different problem while using msbuild that gave me the same error output, and led me on a circular wild-goose chase. So, for future googlers, here goes:

We have a batch file that calls msbuild, but as the build machine can build for multiple versions of Visual Studio, each batch file calls vcvarsall.bat before it runs msbuild. This has the nasty side effect of stuffing the path completely full of the same thing over and over again. When it fills up, you get the error shown in the question above: The input line is too long. A simple Google search could make you think your paths are suddenly too long for msbuild.

In my case, it was as simple as killing the session of cmd.exe and restarting, as this reverted the environment variables to their native state.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.