locked
Refactoring problem: the refactoring could not be performed due to build errors

    Întrebare

  • I am trying to rename a field in a class in VS 2008 C# project and I am getting an error "The refactoring could not be performed due to build errors".

    Here is a structure of the solution:

    MyExeProject: references MyClassLibrary and NUnit. Refactoring does not work

    MyClassLibrary: references NUnit. Refactoring works!

    And here is what I see in output window when refactoring fails:

    fatal error CS0009: Metadata file 'x:\Solution\MyClassLibrary\obj\Debug\Refactor\MyClassLibrary.dll' could not be opened -- 'Error importing module 'nunit.util.dll' of assembly 'x:\Solution\MyClassLibrary\obj\Debug\Refactor\MyClassLibrary.dll' -- The system cannot find the file specified. '

    I have not encountered this situation ever before. I am also puzzled by the fact that refactoring in MyClassLibrary project works fine but it fails in MyExeProject. I can also compile solution without any problems and it runs fine. Only refactoring is broken.

    I am out of ideas what to do here. I tried removing and re-adding references, rebuilding everything, changing output locations, saying magic words.. nothing helped. Any ideas what else could I try? 

    6 aprilie 2012 19:01

Răspunsuri

  • Yes, NUnit was included in the solution and yes it is VS refactoring.

    I finally solved refactoring problem by removing NUnit projects from solution. I just cannot understand why would refactoring have a problem when regular build works fine.

    10 aprilie 2012 19:21

Toate mesajele

  • Is nunit being pulled from the GAC, a separate directory or in the same directory as your class library? 

    You'll get this message if the refactoring causes the code to fail to compile or if the binding changes.  This is an option in Tools\Options -> Text Editor\C# -> Advanced - Verify results of refactoring.  By turning off this option you can skip the error, refactor the code and then compile to see if the issue goes away.

    Michael Taylor - 4/6/2012
    http://msmvps.com/blogs/p3net

    6 aprilie 2012 20:40
  • In VS 2008 I only see "Warn if build errors exist when refactoring" and "Warn on members with compiler generated references". Unchecked them both, tried to refactor and got the same error.

    I guess that there are multiple versions of NUnit everywhere in the system.  How can I figure out which copy VS is loading and how can I force VS not to use version from GAC? It is puzzling when refactoring works on one project and does not work on the other in the same solution.

    6 aprilie 2012 23:45
  • VS never pulls assemblies from the GAC.  Assemblies are always from some directory on disk.  You can look at the properties for the reference to determine where it is getting it from and, if CopyLocal is true, then the copy will be in the output directory.  Just refactoring code shouldn't change the references though.

    Michael Taylor - 4/6/2012
    http://msmvps.com/blogs/p3net

    7 aprilie 2012 02:11
  • Everything looks fine in the project. CopyLocal is true for all assemblies and building works but refactoring does not work on MyExeProject. If I clear solution and try refactoring again I see this in the output window:

    fatal error CS0009: Metadata file 'x:\solution\MyClassLibrary\obj\Debug\Refactor\MyClassLibrary.dll' could not be opened -- 'Error importing module 'nunit.util.dll' of assembly 'x:\solution\MyClassLibrary\obj\Debug\Refactor\MyClassLibrary.dll' -- The system cannot find the file specified. '
    Compiling project 'x:\solution\NUnit-2.6.0.12051\src\ClientUtilities\util\obj\Debug\Refactor\nunit.util.dll' for refactoring
    Compiling project 'x:\solution\MyClassLibrary\obj\Debug\Refactor\MyClassLibrary.dll' for refactoring
    fatal error CS0009: Metadata file 'x:\solution\MyClassLibrary\obj\Debug\Refactor\MyClassLibrary.dll' could not be opened -- 'Error importing module 'nunit.util.dll' of assembly 'x:\solution\MyClassLibrary\obj\Debug\Refactor\MyClassLibrary.dll' -- The system cannot find the file specified. '

    There is something mysterious going on during refactoring.


    9 aprilie 2012 18:00
  • Are you building your own copy of NUnit?  It seems odd that it is trying to compile the NUnit DLL.  I've never seen this kind of problem with refactoring.  To clarify you are using the VS version of refactoring and not a third-party tool like ReSharper or CodeRush correct?  You might consider starting VS in safemode (/safemode) then try the refactoring to see how it goes.

    Michael Taylor - 4/9/2012
    http://msmvps.com/blogs/p3net

    10 aprilie 2012 02:48
  • Yes, NUnit was included in the solution and yes it is VS refactoring.

    I finally solved refactoring problem by removing NUnit projects from solution. I just cannot understand why would refactoring have a problem when regular build works fine.

    10 aprilie 2012 19:21