Repository navigation
feat: import stacktrace as errxtrace and add -compat mode - #2
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (14)
✅ Files skipped from review due to trivial changes (5)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughThis PR adds ModeCompat and a dialect-based rewrite abstraction, refactors build/rewrite to use dialects, adds CLI/analyzer/test wiring for compat mode, and introduces many compat-mode golden fixtures and a compat stub. ChangesCompat Mode Implementation
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related issues
Possibly related PRs
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@build_edits.go`:
- Around line 135-140: The wrapper-detection wrongly assumes the format string
is at m.call.Args[1]; update checkWrapVerb or its call site so it inspects the
correct format-argument index for functions like errors.Errorf (format is
m.call.Args[0]) instead of only Args[1]; e.g., modify checkWrapVerb to accept a
format-arg index (or detect "Errorf"/qualified name from m.call.Fun) and use
that index when deciding to return leave, so the build_edits.go logic that sets
rw := c.newCall(... fmt+".Sprintf("+c.joinArgs(...)+")") and rw.todo =
sprintfTODO(m) will not rewrite calls containing a literal %w into fmt.Sprintf.
In `@imports.go`:
- Around line 80-89: The current import rendering in importLines (and the
importAlias helper) only emits a default alias and doesn't account for existing
per-file import names, causing generated uses like errxtrace.*/errxc.* to be
undefined when the file has the same path imported under a different local name;
update the import handling so importLines (or the caller addImportEdits) accepts
or looks up the file's existing import map and: if the target path is already
imported under a local name, render and reuse that alias instead of emitting the
default, otherwise insert the new import as before; additionally, if you detect
an alias mismatch for a path that will leave existing call sites incompatible,
skip rewriting those call sites (leave them unchanged) or update them to the
detected alias so the code remains compilable—use importLines, importAlias and
addImportEdits as the touch points to implement this behavior.
In `@README.md`:
- Line 45: The README line "The `errx/stacktrace` subpackage is always imported
under the alias **`errxtrace`**, and `errx/compat` under **`errxc`**." is
misleading; update it to clarify these aliases only apply when those packages
are actually imported. Change the wording to something like: when
`errx/stacktrace` is imported it is aliased as `errxtrace`, and when
`errx/compat` is imported (e.g., in `-compat` mode) it is aliased as `errxc`;
mention that `errx/stacktrace` may be omitted in lean/compat runs if no
`WithStack` rewrite requires it to make the conditional import explicit.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: ef65189d-97e3-4803-8dd7-546a9a0cf4cb
📒 Files selected for processing (43)
README.mdanalyzer.goanalyzer_test.gobuild_edits.gocmd/errx-migrate/main.gocmd/errx-migrate/main_test.godetect.gogolden_test.goimports.gomode.gorewrite.gotarget.gotestdata/analyzer/src/example/example.gotestdata/src/aliased/in.compat.goldentestdata/src/aliased/in.stack.goldentestdata/src/cause/in.compat.goldentestdata/src/comment/in.compat.goldentestdata/src/dotimport/in.compat.goldentestdata/src/dotimport/in.stack.goldentestdata/src/errorf/in.compat.goldentestdata/src/errorf/in.stack.goldentestdata/src/isasunwrap/in.compat.goldentestdata/src/multiimport/in.compat.goldentestdata/src/multiimport/in.stack.goldentestdata/src/nested/in.compat.goldentestdata/src/nested/in.lean.goldentestdata/src/nested/in.stack.goldentestdata/src/nolint/in.compat.goldentestdata/src/nolint/in.stack.goldentestdata/src/shadowed/in.compat.goldentestdata/src/shadowed/in.stack.goldentestdata/src/stdmix/in.compat.goldentestdata/src/stdmix/in.stack.goldentestdata/src/withmessage/in.compat.goldentestdata/src/withstack/in.compat.goldentestdata/src/withstack/in.lean.goldentestdata/src/withstack/in.stack.goldentestdata/src/wrap/in.compat.goldentestdata/src/wrap/in.stack.goldentestdata/src/wrapf/in.compat.goldentestdata/src/wrapf/in.stack.goldentestdata/src/wverb/in.compat.goldentestdata/stub/errx/compat/compat.go
Address the go-extras/errx#44 review comment the initial migrator missed: - Import errx/stacktrace under the alias `errxtrace` (was `stacktrace`); all rendered calls and diagnostics now use errxtrace.*. - Add a third `-compat` mode (ModeCompat / AnalyzerCompat) that routes the New/Wrap/Wrapf/WithMessage(f)/Errorf family through errx/compat under the alias `errxc`, whose functions accept plain error classifications. An explicit errors.WithStack is still preserved as errxtrace.Classify, so errx*/errxtrace is offered whenever possible. Mutually exclusive with -no-stack. - Light source/target refactor toward an extendable multi-library migrator: detect.go is now the pkg/errors source recognizer; target.go holds the target import paths, qualifiers, import aliases and per-mode dialects. Goldens regenerated for all fixtures (stack/lean re-aliased, new compat goldens); added a compat stub, a compat end-to-end test, and updated the analyzer want-comments. go test -race, go vet and golangci-lint all pass. Verified on kopia/kopia (427 files / 2716 sites) in all three modes: go build, go vet, gofmt and idempotency all green.
dae2481 to
825fcea
Compare
…c edits Addresses the CodeRabbit review of #2: - Errorf's format string is its first argument, so check Args[0] (not Args[1]) for %w. A literal %w in errors.Errorf now correctly leaves the call in place in stack/compat mode; lean still uses fmt.Errorf, which supports %w. - When a file already imports a target path (e.g. errx/stacktrace) under another local name, reuse that name instead of emitting an undefined errxtrace.*/errxc.* qualifier; dot imports are called unqualified. - README: clarify the aliases apply only when those packages are imported. - Add docstrings to the build* helpers. Also fix a pre-existing overlapping-edits failure surfaced while re-verifying kopia: the sole-import path emitted added imports in map (random) order, so the CLI's per-variant edit dedup (Tests:true) kept two differing import edits at the same position. Sort the added paths for deterministic edit text. Fixtures: wverb gains an Errorf %w case; existingalias covers reuse of a pre-existing aliased target import. go test -race, go vet and golangci-lint pass.
There was a problem hiding this comment.
♻️ Duplicate comments (2)
README.md (1)
45-45:⚠️ Potential issue | 🟡 Minor | ⚡ Quick winAvoid “always imported” here; imports are conditional.
This wording is misleading. Prefer “when imported,
errx/stacktraceis aliased aserrxtrace, anderrx/compataserrxc.”🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@README.md` at line 45, Update the README sentence to avoid implying unconditional imports: change the wording to state that when imported, errx/stacktrace is aliased as errxtrace and errx/compat is aliased as errxc; reference the package names errx/stacktrace and errx/compat and the aliases errxtrace and errxc so readers know the conditional aliasing convention.build_edits.go (1)
135-140:⚠️ Potential issue | 🟠 Major | ⚡ Quick winCheck
%wagainst the correct format argument inbuildErrorf.
checkWrapVerbreadsm.call.Args[1](Line 237), but forerrors.Errorfthe format string isArgs[0]. So%wliterals inErrorfcan still be rewritten throughfmt.Sprintf.💡 Minimal fix
func (c *buildCtx) buildErrorf(m match) rewrite { // No format arguments: equivalent to New. if len(m.call.Args) == 1 { return c.newCall(c.dl.stackNew, m, c.text(m.call.Args[0])) } if c.dl.leanErrorf { // fmt.Errorf is idiomatic and supports %w; no Sprintf, no TODO. c.need(fmtPath) return rewrite{newText: qualFmt + ".Errorf(" + c.joinArgs(m.call, 0) + ")", message: msgFor(m, qualFmt+".Errorf")} } + if formatHasWrapVerb(m.call.Args[0]) { + return leaveRewrite(m, "the format string contains %w, which fmt.Sprintf does not support") + } if rw, leave := checkWrapVerb(m); leave { return rw }#!/bin/bash set -euo pipefail echo "Verify format-index handling for Errorf vs Wrapf/WithMessagef:" rg -n -C3 'func \(c \*buildCtx\) buildErrorf|func checkWrapVerb|formatHasWrapVerb\(m\.call\.Args\[1\]\)|checkWrapVerb\(m\)' build_edits.goAlso applies to: 236-238
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@build_edits.go` around lines 135 - 140, checkWrapVerb currently inspects m.call.Args[1], which is wrong for calls like buildErrorf where the format string is at Args[0]; update the call site in buildErrorf (and any other callers) to pass the correct argument index or change checkWrapVerb to accept an index/Expr representing the format argument and use that instead of hardcoding Args[1], ensuring formatHasWrapVerb (and checkWrapVerb) test the actual format expression from m.call.Args[0] when invoked from buildErrorf so %w in errors.Errorf is detected and not rewritten via fmt.Sprintf.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Duplicate comments:
In `@build_edits.go`:
- Around line 135-140: checkWrapVerb currently inspects m.call.Args[1], which is
wrong for calls like buildErrorf where the format string is at Args[0]; update
the call site in buildErrorf (and any other callers) to pass the correct
argument index or change checkWrapVerb to accept an index/Expr representing the
format argument and use that instead of hardcoding Args[1], ensuring
formatHasWrapVerb (and checkWrapVerb) test the actual format expression from
m.call.Args[0] when invoked from buildErrorf so %w in errors.Errorf is detected
and not rewritten via fmt.Sprintf.
In `@README.md`:
- Line 45: Update the README sentence to avoid implying unconditional imports:
change the wording to state that when imported, errx/stacktrace is aliased as
errxtrace and errx/compat is aliased as errxc; reference the package names
errx/stacktrace and errx/compat and the aliases errxtrace and errxc so readers
know the conditional aliasing convention.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: eb71940d-7b28-45fa-8eea-afea2a86c30a
📒 Files selected for processing (43)
README.mdanalyzer.goanalyzer_test.gobuild_edits.gocmd/errx-migrate/main.gocmd/errx-migrate/main_test.godetect.gogolden_test.goimports.gomode.gorewrite.gotarget.gotestdata/analyzer/src/example/example.gotestdata/src/aliased/in.compat.goldentestdata/src/aliased/in.stack.goldentestdata/src/cause/in.compat.goldentestdata/src/comment/in.compat.goldentestdata/src/dotimport/in.compat.goldentestdata/src/dotimport/in.stack.goldentestdata/src/errorf/in.compat.goldentestdata/src/errorf/in.stack.goldentestdata/src/isasunwrap/in.compat.goldentestdata/src/multiimport/in.compat.goldentestdata/src/multiimport/in.stack.goldentestdata/src/nested/in.compat.goldentestdata/src/nested/in.lean.goldentestdata/src/nested/in.stack.goldentestdata/src/nolint/in.compat.goldentestdata/src/nolint/in.stack.goldentestdata/src/shadowed/in.compat.goldentestdata/src/shadowed/in.stack.goldentestdata/src/stdmix/in.compat.goldentestdata/src/stdmix/in.stack.goldentestdata/src/withmessage/in.compat.goldentestdata/src/withstack/in.compat.goldentestdata/src/withstack/in.lean.goldentestdata/src/withstack/in.stack.goldentestdata/src/wrap/in.compat.goldentestdata/src/wrap/in.stack.goldentestdata/src/wrapf/in.compat.goldentestdata/src/wrapf/in.stack.goldentestdata/src/wverb/in.compat.goldentestdata/stub/errx/compat/compat.go
✅ Files skipped from review due to trivial changes (15)
- testdata/src/isasunwrap/in.compat.golden
- testdata/analyzer/src/example/example.go
- testdata/src/multiimport/in.stack.golden
- testdata/src/shadowed/in.stack.golden
- detect.go
- testdata/src/wrapf/in.stack.golden
- testdata/src/aliased/in.stack.golden
- testdata/src/stdmix/in.stack.golden
- testdata/src/stdmix/in.compat.golden
- testdata/src/nolint/in.stack.golden
- testdata/src/errorf/in.stack.golden
- testdata/stub/errx/compat/compat.go
- testdata/src/withstack/in.lean.golden
- testdata/src/dotimport/in.stack.golden
- testdata/src/nested/in.lean.golden
🚧 Files skipped from review as they are similar to previous changes (21)
- testdata/src/cause/in.compat.golden
- testdata/src/nolint/in.compat.golden
- testdata/src/withstack/in.compat.golden
- testdata/src/wverb/in.compat.golden
- testdata/src/nested/in.stack.golden
- analyzer.go
- testdata/src/shadowed/in.compat.golden
- cmd/errx-migrate/main.go
- testdata/src/nested/in.compat.golden
- testdata/src/dotimport/in.compat.golden
- testdata/src/aliased/in.compat.golden
- rewrite.go
- golden_test.go
- testdata/src/comment/in.compat.golden
- cmd/errx-migrate/main_test.go
- testdata/src/errorf/in.compat.golden
- testdata/src/multiimport/in.compat.golden
- target.go
- mode.go
- testdata/src/wrap/in.compat.golden
- testdata/src/wrapf/in.compat.golden
Self-review — fixes pushed in
|
What
Finishes the go-extras/errx#44 migrator by addressing the review comment the first PR (#1) missed.
errxtracealiaserrx/stacktraceis now imported aserrxtrace(notstacktrace); all rendered calls and diagnostics useerrxtrace.*.-compatmodeA new mode routes the message/wrap family through
errx/compataliasederrxc(which accepts plainerrorclassifications). An expliciterrors.WithStackstill maps toerrxtrace.Classify—errx/errxtraceis offered whenever possible. Mutually exclusive with-no-stack; also exposed asAnalyzerCompat.Light source/target refactor
Toward the "extendable multi-library migrator" the issue asks for:
detect.go— the pkg/errors source recognizer (calls → sharederrFnIR).target.go(new) — target dialects: import paths, qualifiers,importAlias,targetCall,dialect,dialectFor. Adding a new source library no longer touches the rendering side.API mapping
pkg/errors-no-stack)-compat)Newerrxtrace.ClassifyNewerrors.Newerrxc.ClassifyNewWraperrxtrace.Wraperrx.Wraperrxc.WrapWithMessageerrx.Wraperrx.Wraperrxc.WrapWithStackerrxtrace.Classifyerrxtrace.Classifyerrxtrace.ClassifyTesting
go test -race ./...,go vet ./...,golangci-lint run ./...— all green.Goldens regenerated; added a
compatstub, a compat end-to-end test, and updated analyzer// wantcomments.Verified on
kopia/kopia(427 files / 2716pkg/errorssites, errxv1.3.0) in all three modes —go build,go vet, gofmt-clean and idempotency all pass:go build ./...go vet ./...Summary by CodeRabbit
New Features
Documentation
Tests