Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

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

未关闭
#794 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

  • #811 来自 @Raymondriter —— 已关闭,未合并

评估

难度
3/5
预计耗时
1-2 天
新手友好度
76/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
冷清
技术栈
go, sql
领域
cli, database

调研方向

从 cmd/sqlcmd/sqlcmd.go 中的 -r PrintError hook 开始,然后阅读 pkg/sqlcmd/format.go 中的 Formatter.AddError,以比较现有的 header 格式。重现 issue 中的命令,并验证 -r 会将相同的 Msg、Level、State、Server 和 Line header 以及消息发送到 stderr,同时保留现有的目标行为。

由索引模型根据 Issue 内容生成。

描述

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)
主要语言
Go
星标
604
派生
91
PR 合并指标
30 天内没有已合并 PR

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

  • 提供 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 没有贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

microsoft/go-sqlcmd 的其他 Issue

查看 microsoft/go-sqlcmd 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。