none
What Target should be used with IVsMSBuildTaskFileManager.PutGeneratedFileContents

    Question

  • Hi,
    I need to update some projects files during (or before) the build process, I managed to register my custom task under the MSBuildHostObjects registry key, So, the IDE is passing to me a valid IVsMSBuildTaskFileManager as the HostObject.
    The problem is:
    When updating any file using IVsMSBuildTaskFileManager.PutGeneratedFileContents method the following scary warning message appears:
    "Save during build"
    "An editor or project is attempting to save a file that is modified in memory. Saving files during a build is dangerous and may result in incorrect build outputs in the future. Continue with save?"
    I tried to but my task in different targets to avoid interfering with the build process Like "BeforeBuild" , or before $(BuildDependsOn), but no luck.
    In what context should IVsMSBuildTaskFileManager.PutGeneratedFileContents used to avoid this warning?
    <Target Name = "ThankYouForAnyAdvice">
    </Target>



    • Edited by FadiXY Wednesday, December 14, 2011 10:21 AM
    Monday, December 12, 2011 8:48 PM

All replies

  • Hi FadiXY,

    Please try to go to Tools->Options->Projects and Solutions and make sure the "Save new projects when created" is checked and then try to rebuild your project. 

    If that doesn't work, can you provide some reproduce steps detailing how you get this error to be displayed? 

    Thank you for your understanding!

    Best regards,

    Lucy


    Lucy Liu [MSFT]
    MSDN Community Support | Feedback to us
    Wednesday, December 14, 2011 5:51 AM
    Moderator
  • Thank you Lucy for replying,

    I have XML project document which generates code at design time using custom tool, both the XML file and the .cs file should be updated when the user decides to build the project to reflect the up to date state.

    To achieve that , I'm using custom MSBuild task which read the XML document ,update it, generate the .cs file and write them back to their original locations in the project directory.

    By updating project files directly from MSBuild Task, the Visual Studio IDE will not be notified, and "File Updated outside Visual Studio IDE" may appear if the files are open in the designer, so I thought I should use IVsMSBuildTaskFileManager.PutGeneratedFileContents which should legally "Puts the contents of a generated file into an in-memory buffer and registers it in the RDT".

    When calling IVsMSBuildTaskFileManager.PutGeneratedFileContents from my custom Task I get the warning: 

    "Save during build"
    "An editor or project is attempting to save a file that is modified in memory. Saving files during a build is dangerous and may result in incorrect build outputs in the future. Continue with save?"
    The task invoked in this way:
    <PropertyGroup>
        <BuildDependsOn>
          AssetBuildTarget;
          $(BuildDependsOn);
        </BuildDependsOn>
      </PropertyGroup>

      <Target Name="AssetBuildTarget">
        <AssetBuildTask ContinueOnError="false" SourceFiles="%(None.FullPath)" TargetPath="$(TargetPath)">
          <Output TaskParameter="AssetProjectFile" ItemName="AssetProjectFile" />
        </AssetBuildTask>
      </Target>

      <UsingTask TaskName="AssetBuildTask" AssemblyFile="AssetBuilderBuildTask.dll" />
    Thanks again,
    Fadi

    Wednesday, December 14, 2011 10:18 AM
  • Hi FadiXY,

    Thank you for your question.

    I am trying to involve someone familiar with this topic to further look at this issue. There might be some time delay. Appreciate your patience.

    Thank you for your understanding and support.

     

    Best regards,

    Lucy


    Lucy Liu [MSFT]
    MSDN Community Support | Feedback to us
    Friday, December 16, 2011 5:20 AM
    Moderator
  • Hi Fadi,

    Use the DependsOnTargets property to make sure the code is generated before the compiler start to work. http://msdn.microsoft.com/en-us/library/microsoft.build.buildengine.target.dependsontargets.aspx

    Is the WriteCodeFragment task an option for you if you just want to write some code to a file: http://msdn.microsoft.com/en-us/library/ff598685.aspx

    Thanks & Regards,

    Jian-Wei Yu [MSFT]

    Microsoft Online Community Support

     

    --------------------------------------------------------------------------------

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

     

    Thursday, December 22, 2011 3:28 AM
  • Thank you Jian-Wei,

    I tried every possible scenario, even running my target before the very first target in the build process, like this:

    <Target Name="AssetBuildTarget" BeforeTargets="_CheckForInvalidConfigurationAndPlatform">

    I thought that dependencies build process may cause this message to appear, so I removed all project dependencies and the build process should handle only one project, but no luck.

    Any other suggestions....?

     

    Monday, January 09, 2012 4:54 PM
  • Hi Fadi,

    Can you use the ReadLinesFromFile and the WriteLinesToFile task to update the projects files during build?

    Thanks & Regards,

    Jian-Wei Yu [MSFT]

    Microsoft Online Community Support

     

    --------------------------------------------------------------------------------

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

     

    Tuesday, January 31, 2012 8:08 AM
  • Hi Jian-Wei,

    Actually, I ignored IVsMSBuildTaskFileManager.PutGeneratedFileContents, and I'm using File.WriteAllText which is doing fine, except if the generated files is cashed in the IDE (opened and closed before build starts) I got the message of "source file changed outside the IDE and would you like to reload it".

    This approach is less confusing, but I lost the communications between  the MSBuild task and the IDE.

    I'll stick to this until I figure out a way of updating the source file at build time without confusing the IDE.

    Cheers,

    Saturday, February 04, 2012 8:03 PM