Skip to content

fix: panic in spew when dealing with unexported fields - #1828

Open
ccoVeille wants to merge 1 commit into
stretchr:masterfrom
ccoveille-forks:gospew-panic
Open

ccoVeille wants to merge 1 commit into
stretchr:masterfrom
ccoveille-forks:gospew-panic

Conversation

@ccoVeille

@ccoVeille ccoVeille commented Nov 18, 2025

Copy link
Copy Markdown
Collaborator

Summary

Fix panic that happens when spew tries to sort unexported fields

Changes

Fix the panic raised by Interface() on unexported fields

CanInterface() is the solution

Motivation

Now, go-spew is vendored, we can fix old issues we had

Related issues

@ccoVeille
ccoVeille marked this pull request as ready for review November 28, 2025 11:39
@HaraldNordgren

Copy link
Copy Markdown
Contributor

@ccoVeille Looks good! ✅

fredbi added a commit to fredbi/testify that referenced this pull request Jan 2, 2026
This PR adapts stretchr#1828

It fixes an edge case the spew lib when diffing a map with keys of type
array.

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to go-openapi/testify that referenced this pull request Jan 2, 2026
This PR adapts stretchr#1828

It fixes an edge case the spew lib when diffing a map with keys of type
array.

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
@HaraldNordgren

HaraldNordgren commented Jan 29, 2026

Copy link
Copy Markdown
Contributor

Let's merge this? Then we can also close #1816.

@dolmen dolmen added bug pkg-assert Change related to package testify/assert pkg-require Change related to package testify/require labels Feb 17, 2026
@HaraldNordgren

Copy link
Copy Markdown
Contributor

@ccoVeille Let's merge?

@ccoVeille

Copy link
Copy Markdown
Collaborator Author

I tend to not merge my own PR on a public repository.

Also, the release cycle of testify is slow paced.

@dolmen reviewed the changes and approved it.

Maybe @brackendawson could review them.

@brackendawson

Copy link
Copy Markdown
Collaborator

Results in such maps not being sorted:

func TestIfy(t *testing.T) {
	type test struct {
		m map[[1]byte]*struct{}
	}

	a := test{
		map[[1]byte]*struct{}{
			{1}: nil,
			{2}: nil,
		},
	}
	b := test{
		map[[1]byte]*struct{}{
			{2}: nil,
			{3}: nil,
			{1}: nil,
		},
	}

	assert.Equal(t, a, b)
}
Running tool: /usr/local/go/bin/go test -test.fullpath=true -timeout 30s -coverprofile=/var/folders/8c/f9y61rb14cn8c6_6lnz10q300000gn/T/vscode-gojqA8nF/go-code-cover github.com/brackendawson/kata -race

--- FAIL: TestIfy (0.00s)
    /Users/brackendawson/src/github.com/brackendawson/kata/kata_test.go:29: 
        	Error Trace:	/Users/brackendawson/src/github.com/brackendawson/kata/kata_test.go:29
        	Error:      	Not equal: 
        	            	expected: kata_test.test{m:map[[1]uint8]*struct {}{[1]uint8{0x1}:(*struct {})(nil), [1]uint8{0x2}:(*struct {})(nil)}}
        	            	actual  : kata_test.test{m:map[[1]uint8]*struct {}{[1]uint8{0x1}:(*struct {})(nil), [1]uint8{0x2}:(*struct {})(nil), [1]uint8{0x3}:(*struct {})(nil)}}
        	            	
        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1,8 +1,11 @@
        	            	 (kata_test.test) {
        	            	- m: (map[[1]uint8]*struct {}) (len=2) {
        	            	+ m: (map[[1]uint8]*struct {}) (len=3) {
        	            	+  ([1]uint8) (len=1) {
        	            	+   00000000  02                                                |.|
        	            	+  }: (*struct {})(<nil>),
        	            	+  ([1]uint8) (len=1) {
        	            	+   00000000  03                                                |.|
        	            	+  }: (*struct {})(<nil>),
        	            	   ([1]uint8) (len=1) {
        	            	    00000000  01                                                |.|
        	            	-  }: (*struct {})(<nil>),
        	            	-  ([1]uint8) (len=1) {
        	            	-   00000000  02                                                |.|
        	            	   }: (*struct {})(<nil>)
        	Test:       	TestIfy
FAIL
coverage: 0.0% of statements
FAIL	github.com/brackendawson/kata	0.327s
FAIL

This is better than a panic. We're able to format the map keys, so I wonder if we can fall back on a string sorting of the formatted values?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug pkg-assert Change related to package testify/assert pkg-require Change related to package testify/require

Projects

None yet

Development

Successfully merging this pull request may close these issues.

assert.Equal panic on diff unexported map with array as key Panic in assert/require.Equal when calling into go-spew

4 participants