dav_i
dav_i

Reputation: 28147

Output errors, warnings and messages from batch file in Visual Studio build event

I'm writing a batch file to be executed from a pre-build event in Visual Studio.

How can I make it output errors, warnings and messages to the Visual Studio Error List?

It's very hard to search for this as most things that come up are how to fix errors, not throw them!

Upvotes: 7

Views: 1549

Answers (1)

jeb
jeb

Reputation: 82400

The output needs a special format:

<Filename> (<LineNumber>): Warning: <ErrorMessage>

Instead of Warning you could also use Error
Also the spaces are important!

You could create it like this one

echo %~f0 (!lineNo!^): Warning: Invalid target for production

And to give a hint for the error position you should add a more or less accurate line number.

if /i "!TargetPath:~-3!"=="DLL" (
    set "targetValid=1"
) ELSE (
    call :GetLineNumber lineNo +1 {3D967145-10B6-44A0-96EF-91B6C6E2DD0E}
    echo %~f0 (!lineNo!^): Warning: Invalid target '!TargetPath:~-3!' for production
)
....

:GetLineNumber returnVar add uniqueGUID
:::
:::
setlocal EnableDelayedExpansion
set "GetLineNumber.lineNo=0"
set /a "GetLineNumber.Add=%~2"
set "GetLineNumber.unique=%~3"

for /F "delims=:" %%L in ('findstr /n "!GetLineNumber.unique!" "%~f0"') do (
    set /a "GetLineNumber.lineNo=%%L"
)

(
endlocal
set /a "%~1=%GetLineNumber.lineNo%" + %GetLineNumber.Add%
)
exit /b

Upvotes: 8

Related Questions