Skip to content

feat: implement -j raw-errors flag for ODBC sqlcmd compatibility#759

Open
dlevy-msft-sql wants to merge 10 commits into
microsoft:mainfrom
dlevy-msft-sql:feat/raw-errors-flag
Open

feat: implement -j raw-errors flag for ODBC sqlcmd compatibility#759
dlevy-msft-sql wants to merge 10 commits into
microsoft:mainfrom
dlevy-msft-sql:feat/raw-errors-flag

Conversation

@dlevy-msft-sql
Copy link
Copy Markdown
Contributor

@dlevy-msft-sql dlevy-msft-sql commented May 19, 2026

Problem

go-sqlcmd did not implement the -j switch from the ODBC-based sqlcmd, which is required to preserve raw driver-supplied error text. This was tracked in discussion #292 as a gap blocking ODBC sqlcmd retirement.

Root Cause

The default formatter (pkg/sqlcmd/format.go) unconditionally stripped the "mssql: " prefix that go-mssqldb prepends to every mssql.Error.Error() string. There was no way for a caller to opt out, and no CLI flag exposed the behavior.

Solution

Implement -j / --raw-errors with semantics that match the ODBC reference (Sql/utils/sqlcmd/console/src/Formatter.cpp, gated on m_PrintRawErrorMessages):

  • When -j is set, the driver-supplied prefix is preserved verbatim.
  • The Msg N, Level N, State N, Server S, Line N header is always emitted (matches ODBC).
  • Screen-width wrapping via fitToScreen is always applied (matches ODBC).

The formatter is extended via a non-breaking variadic functional-options pattern, so existing callers (including internal/sql/mssql.go) compile unchanged.

Changes

File Change
pkg/sqlcmd/format.go Add rawErrors field, FormatterOption type, WithRawErrors; gate strings.TrimPrefix(msg, "mssql: ") in AddError.
pkg/sqlcmd/format_test.go Add TestAddErrorStripsMssqlPrefixByDefault and TestAddErrorWithRawErrorsKeepsMssqlPrefix — two parallel tests, each readable end-to-end, covering both gate states.
cmd/sqlcmd/sqlcmd.go Add RawErrors to SQLCmdArguments; register -j / --raw-errors; pass WithRawErrors into the formatter.
cmd/sqlcmd/sqlcmd_test.go Add -j case to TestValidCommandLineToArgsConversion.

Testing

  • go build ./... clean.
  • go vet ./... clean (only pre-existing credman warnings on windows/v0.21.0).
  • go test ./pkg/sqlcmd/ ./cmd/sqlcmd/ -count=1 against a live SQL Server: PASS.
  • The two TestAddError… tests assert the header and message body appear in both modes and that the "mssql: " prefix is stripped exactly when -j is unset (would fail in either direction if the gate logic regressed).

Related Issues

Closes the -j gap in discussion #292.

Adds the -j / --raw-errors flag to the legacy sqlcmd CLI, mirroring the ODBC sqlcmd's m_PrintRawErrorMessages behavior (Sql/utils/sqlcmd/console/src/Formatter.cpp). When set, the driver-supplied 'mssql: ' prefix is preserved in error output; the Msg/Level/State/Server header and screen-width wrapping are unaffected, matching ODBC sqlcmd which also only gates the prefix strip.

Closes the -j gap tracked in discussion microsoft#292.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds ODBC-compatible -j/--raw-errors support so go-sqlcmd can optionally preserve the go-mssqldb "mssql: " error prefix, improving parity with ODBC sqlcmd behavior.

Changes:

  • Extend the default formatter with a functional-options pattern and a WithRawErrors option.
  • Gate stripping of the "mssql: " prefix in AddError based on the new option.
  • Wire -j/--raw-errors through CLI args and add targeted test coverage.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
pkg/sqlcmd/format.go Adds rawErrors option plumbing and gates mssql: prefix trimming in AddError.
pkg/sqlcmd/format_test.go Adds coverage validating prefix stripping behavior in default vs raw modes.
cmd/sqlcmd/sqlcmd.go Introduces RawErrors CLI flag and passes formatter option through.
cmd/sqlcmd/sqlcmd_test.go Adds CLI parsing test for -j.
Comments suppressed due to low confidence (1)

pkg/sqlcmd/format.go:1

  • The docstring is internally inconsistent: it describes the prefix as "driver-supplied" while also stating it is prepended by go-mssqldb. Consider rewording to clarify that -j preserves the go-mssqldb-added prefix (or more generally, preserves the error string exactly as returned by the Go driver) to avoid confusing API consumers.
// Copyright (c) Microsoft Corporation.

Comment thread pkg/sqlcmd/format.go Outdated
Comment thread pkg/sqlcmd/format_test.go
@dlevy-msft-sql
Copy link
Copy Markdown
Contributor Author

Took the docstring feedback as well — reworded both the WithRawErrors docstring and the -j flag help text in 878e6d4 to explicitly name the go-mssqldb client driver's mssql: prefix instead of the ambiguous "driver-supplied" phrasing.

@dlevy-msft-sql dlevy-msft-sql requested a review from Copilot May 19, 2026 21:47
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

Comment thread cmd/sqlcmd/sqlcmd.go Outdated
Comment thread pkg/sqlcmd/format.go Outdated
Comment thread pkg/sqlcmd/format_test.go Outdated
Comment thread pkg/sqlcmd/format.go
@dlevy-msft-sql dlevy-msft-sql requested a review from Copilot May 19, 2026 22:02
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

Comment thread pkg/sqlcmd/format_test.go
Comment thread cmd/sqlcmd/sqlcmd.go Outdated
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

Comment thread pkg/sqlcmd/format.go Outdated
Comment thread cmd/sqlcmd/sqlcmd.go
rootCmd.Flags().IntVar(&args.DriverLoggingLevel, "driver-logging-level", 0, localizer.Sprintf("Level of mssql driver messages to print"))
rootCmd.Flags().BoolVarP(&args.ExitOnError, "exit-on-error", "b", false, localizer.Sprintf("Specifies that sqlcmd exits and returns a %s value when an error occurs", localizer.DosErrorLevel))
rootCmd.Flags().IntVarP(&args.ErrorLevel, "error-level", "m", 0, localizer.Sprintf("Controls which error messages are sent to %s. Messages that have severity level greater than or equal to this level are sent", localizer.StdoutName))
rootCmd.Flags().BoolVarP(&args.RawErrors, "raw-errors", "j", false, localizer.Sprintf("Do not strip the \"mssql: \" prefix from error messages"))
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going to push back on this one, similar to the test-coupling thread above.

The whole point of CLI flag help is to let a user predict the concrete diff they'll see in their output. With -j set, every error line will literally contain mssql: where it wouldn't otherwise; that's user-observable text, not implementation. Replacing the concrete string with abstract phrasing like "preserve raw driver error text" makes the help less actionable — a user reading it can't tell what -j will actually change in their terminal.

format.go hardcodes strings.TrimPrefix(msg, "mssql: ") against that exact literal. If go-mssqldb ever changes its prefix format, the gate, the help text, and the tests all need to update together — that coupling is intentional and consistent with how the rest of the flag help in this file describes concrete behavior (see -b, -h, etc.).

Comment thread pkg/sqlcmd/format_test.go
@dlevy-msft-sql dlevy-msft-sql requested a review from Copilot May 19, 2026 22:37
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

Comment thread pkg/sqlcmd/format.go Outdated
Comment thread pkg/sqlcmd/format_test.go Outdated
@dlevy-msft-sql dlevy-msft-sql requested a review from Copilot May 19, 2026 22:50
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

Comment thread pkg/sqlcmd/format_test.go
Comment thread cmd/sqlcmd/sqlcmd.go
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

Comment thread cmd/sqlcmd/sqlcmd.go
Comment thread pkg/sqlcmd/format.go Outdated
Comment thread pkg/sqlcmd/format_test.go
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

Comment thread pkg/sqlcmd/format.go Outdated
Comment thread pkg/sqlcmd/format.go Outdated
Comment thread cmd/sqlcmd/sqlcmd.go
Comment thread cmd/sqlcmd/sqlcmd_test.go
Comment thread pkg/sqlcmd/format_test.go
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Comment thread pkg/sqlcmd/format.go
@dlevy-msft-sql dlevy-msft-sql marked this pull request as ready for review May 20, 2026 03:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants