Visual Studio Developer Center > Visual Studio Forums > Visual Studio Extensibility > Can we fail the build and add build errors OnBuildBegin event
Ask a questionAsk a question
 

AnswerCan we fail the build and add build errors OnBuildBegin event

  • Friday, November 06, 2009 11:26 AMNagaVital Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I'm planning to write a VS Addin for VS 2008
    In that can we fail the build and add build errors(on some conditions) OnBuildBegin event. I'm using folllowing event handler to raise build begin event

    _dispBuildEvents_OnBuildBeginEventHandler

Answers

  • Monday, November 09, 2009 8:12 AMNancy ShaoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Vital,

    Sorry for misunderstanding.

    Based on my understanding, you have two requirements, as following shows:

       1. Add some errors into Error list Window;
       2. Build on some conditions (if it does not meet the conditions, cancel the Build).

    If I understand correctly, you can achieve these objectives with Add-in:

       1. The automation model (EnvDTE80) provides the EnvDTE80.ErrorList and the EnvDTE80.ErrorItems collection. You can use EnvDTE80.ErrorList() function, or OutputWindowPane.OutputTaskItemString method to add errors to Error List. For more information, please see:

          HOWTO: Add an error with navigation to the Error List from a Visual Studio add-in

       2. You can use _DTE::ExecuteCommand Method to cancel Build as this code shows: _DTE::ExecuteCommand(Build.Cancel) in OnBuildBegin Event on conditions.

    Best Regards,
    Nancy 

     
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

All Replies

  • Monday, November 09, 2009 6:23 AMNancy ShaoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hi NagaVital,

    OnBuildBegin event just is triggered when Build action is begin, it means when you click Build this project, this event is firstly  triggered.

    If you want to use OnBuildBegin Event, firstly add the following code in Exec() function:

     EnvDTE.Events events = _applicationObject.Events;
     buildEvents = (EnvDTE.BuildEvents)events.BuildEvents;
     buildEvents.OnBuildBegin += new _dispBuildEvents_OnBuildBeginEventHandler(this.OnBuildBegin);
    

    And declare OnBuildBegin class out of Exec() function, as following shows:

            public void OnBuildBegin(EnvDTE.vsBuildScope Scope, EnvDTE.vsBuildAction Action)
            {
               //Add Your Code here
                System.Windows.Forms.MessageBox.Show("OnBuildBegin");
            }
    
    The most important thing is defining buildEvents in whole namespace but not in Exec() function, or buildEvents will not valid, for example:

    private DTE2 _applicationObject;
    private AddIn _addInInstance;
    BuildEvents buildEvents;
    
    Please let me know if you have any questions.

    Best Regards,
    Nancy
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Monday, November 09, 2009 7:21 AMNagaVital Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Nancy,

    Thanks for your reply


    I have added the code which fires the Buildbegin event that works fine.

    But my main problem is, I want to add some errors into Error List window and I want to fail the Build on some conditions, can we do this?

    Either it may be on onBuildbegin or any other event


    Refards,
    Vital.
  • Monday, November 09, 2009 8:12 AMNancy ShaoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Vital,

    Sorry for misunderstanding.

    Based on my understanding, you have two requirements, as following shows:

       1. Add some errors into Error list Window;
       2. Build on some conditions (if it does not meet the conditions, cancel the Build).

    If I understand correctly, you can achieve these objectives with Add-in:

       1. The automation model (EnvDTE80) provides the EnvDTE80.ErrorList and the EnvDTE80.ErrorItems collection. You can use EnvDTE80.ErrorList() function, or OutputWindowPane.OutputTaskItemString method to add errors to Error List. For more information, please see:

          HOWTO: Add an error with navigation to the Error List from a Visual Studio add-in

       2. You can use _DTE::ExecuteCommand Method to cancel Build as this code shows: _DTE::ExecuteCommand(Build.Cancel) in OnBuildBegin Event on conditions.

    Best Regards,
    Nancy 

     
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.