Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

-r discards the "Msg N, Level N, State N" error header — errors on stderr lose message number and line info

Abierto
#794 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
76/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Tranquilo
Stack tecnológico
go, sql
Área
cli, database

Línea de trabajo

Comienza en cmd/sqlcmd/sqlcmd.go, en el hook PrintError de -r, y luego lee Formatter.AddError en pkg/sqlcmd/format.go para comparar el formato de header existente. Reproduce los comandos del issue y verifica que -r envíe el mismo header Msg, Level, State, Server y Line, además del mensaje, a stderr, preservando el comportamiento de destino existente.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Description

When -r (errors to stderr) is specified, server error messages are written to stderr
as bare message text only — the Msg %d, Level %d, State %d, Server %s, Line %d
header line is silently dropped. Without -r, the header prints correctly (to stdout).

ODBC sqlcmd with -r prints the same formatted output (header + message), just
redirected to stderr. So scripts/CI pipelines migrating from ODBC sqlcmd lose the error
number and line information exactly in the configuration meant for error capture.

Repro
-- repro.sql
SELECT id, badColumn FROM sys.objects WHERE object_id = 1;
GO
> sqlcmd -S myserver -E -b -i repro.sql          # no -r
Msg 207, Level 16, State 1, Server MYSERVER, Line 1
Invalid column name 'id'.

> sqlcmd -S myserver -E -r -b -i repro.sql       # with -r
Invalid column name 'id.'        <-- stderr: message text only, no Msg/Level/State/Line

ODBC sqlcmd (16.0.1000.6) with the identical -r -b flags writes to stderr:

Msg 207, Level 16, State 1, Server MYSERVER, Line 1
Invalid column name 'id'.
Msg 207, Level 16, State 1, Server MYSERVER, Line 1
Invalid column name 'badColumn'.
Root cause (from source)

Formatter.AddError in pkg/sqlcmd/format.go formats the header correctly for
mssql.Error. But when -r is passed, cmd/sqlcmd/sqlcmd.go installs a
s.PrintError hook that receives only e.Message and writes it directly to
os.Stderr, returning true — which bypasses Format.AddError entirely:

s.PrintError = func(msg string, severity uint8) bool {
    if severity >= stderrSeverity {
        s.WriteError(os.Stderr, errors.New(msg+sqlcmd.SqlcmdEol))
        return true
    }
    return false
}

Suggested fix: format the full header inside the hook (or route through the formatter
with a stderr destination) so -r only changes the destination, not the format —
matching ODBC sqlcmd behavior.

Additional observation

Combined with -b, only the first error of a batch is reported (the message loop
exits on the first qualifying error), while ODBC sqlcmd prints all errors of the
response before exiting. Together with the missing header this significantly degrades
error diagnostics for deployment scripts.

Environment
  • sqlcmd v1.10.0 (sqlcmd-windows-amd64.zip from Releases)
  • Windows 11, SQL Server 2022 (16.0.1190.2)
  • Compared against ODBC sqlcmd 16.0.1000.6 with identical flags
  • (Server messages in our environment are localized/Korean; behavior is language-independent)
Lenguaje dominante
Go
Estrellas
595
Forks
91
Merge medio
9 h 35 min
PR fusionados (30 d)
1

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de microsoft/go-sqlcmd

Todos los issues de microsoft/go-sqlcmd

Issues similares

Más issues de Go

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.