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

I am writing a batch file that must be executed from a pre-build event in Visual Studio.

How can I output errors, warnings and messages to the Visual Studio error list?

It is very difficult to find this, since most of the things that arise are how to fix errors, not throw them away!

+4
source share
1 answer

The conclusion needs a special format:

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

Instead of warning you can also use Error
spaces are also important!

You can create it like this

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

And to give a hint for the error position, you must 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
+3

Source: https://habr.com/ru/post/1584034/


All Articles