Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

models.ts no longer generated after update to v2.9.2 #3809

Closed
BenoitBotton opened this issue Oct 7, 2024 · 13 comments · Fixed by #3975
Closed

models.ts no longer generated after update to v2.9.2 #3809

BenoitBotton opened this issue Oct 7, 2024 · 13 comments · Fixed by #3975
Labels
Bug Something isn't working

Comments

@BenoitBotton
Copy link
Contributor

Description

After updating wails to v2.9.2 (from v2.9.1) models.ts is no longer generated and therefore the frontend fails to build due to typescript errors.
No error is output by typescriptify

To Reproduce

have menu callbacks defined as app methods (so they can be called from frontend)
reproduction here: https://github.com/BenoitBotton/test

Expected behaviour

project runs with wails dev or builds with wails build

Screenshots

No response

Attempted Fixes

git bisect gives the following:

fe1f23b0fd52997b16529a7abb2cfdeb52141601 is the first bad commit
commit fe1f23b0fd52997b16529a7abb2cfdeb52141601
Author: Jeremy Jay <[email protected]>
Date:   Tue Aug 13 17:49:08 2024 -0400

    If a field is exported, generate json even in the absence of tags (#3678)

    * if no JSON tag, check if field is exported

    * add no-tags binding test case

    * update changelog for #3678

 v2/internal/binding/binding.go                     |  5 +-
 .../binding/binding_test/binding_notags_test.go    | 59 ++++++++++++++++++++++
 v2/internal/binding/binding_test/binding_test.go   |  1 +
 v2/internal/typescriptify/typescriptify.go         |  8 ++-
 website/src/pages/changelog.mdx                    |  1 +
 5 files changed, 72 insertions(+), 2 deletions(-)
 create mode 100644 v2/internal/binding/binding_test/binding_notags_test.go

System Details

# Wails
Version | v2.9.2

# System
┌─────────────────────────────────────────────────────────────────────────────────────────────────┐
| OS           | Windows 10 Pro                                                                   |
| Version      | 2009 (Build: 22631)                                                              |
| ID           | 23H2                                                                             |
| Go Version   | go1.23.2                                                                         |
| Platform     | windows                                                                          |
| Architecture | amd64                                                                            |
| CPU          | AMD Ryzen 9 5900HX with Radeon Graphics                                          |
| GPU 1        | AMD Radeon(TM) Graphics (Advanced Micro Devices, Inc.) - Driver: 31.0.21023.2010 |
| GPU 2        | NVIDIA GeForce RTX 3070 Laptop GPU (NVIDIA) - Driver: 32.0.15.5585               |
| Memory       | 64GB                                                                             |
└─────────────────────────────────────────────────────────────────────────────────────────────────┘

# Dependencies
┌───────────────────────────────────────────────────────┐
| Dependency | Package Name | Status    | Version       |
| WebView2   | N/A          | Installed | 129.0.2792.79 |
| Nodejs     | N/A          | Installed | 20.10.0       |
| npm        | N/A          | Installed | 10.8.2        |
| *upx       | N/A          | Available |               |
| *nsis      | N/A          | Installed | v3.08         |
└─────────────── * - Optional Dependency ───────────────┘

Additional context

this can obviously be avoided in my project by making the app methods not be menu callbacks, and call them in callback definitions in main.go

@BenoitBotton BenoitBotton added the Bug Something isn't working label Oct 7, 2024
@fkhadra
Copy link
Contributor

fkhadra commented Dec 27, 2024

Hey, I'm bumping into a similar issue after upgrading to 2.9.2 as well.

In my case, I was relying on the fact that fields without a json tag are not generated. Now when I try to generate the bindings it panics.

          panic: runtime error: invalid memory address or nil pointer dereference
          [signal SIGSEGV: segmentation violation code=0x2 addr=0x18 pc=0x103c74fdc]
          
          goroutine 1 [running]:
          github.com/wailsapp/wails/v2/internal/binding.(*Bindings).GenerateModels(0x1400042a1c0)
          	/Users/gugu/go/pkg/mod/github.com/wailsapp/wails/[email protected]/internal/binding/binding.go:142 +0x41c
          github.com/wailsapp/wails/v2/internal/binding.(*Bindings).WriteModels(0x14000053ad8?, {0x140004330e0, 0x2d})
          	/Users/gugu/go/pkg/mod/github.com/wailsapp/wails/[email protected]/internal/binding/binding.go:209 +0x28
          github.com/wailsapp/wails/v2/internal/binding.(*Bindings).GenerateGoBindings(0x1400042a1c0, {0x140004330e0, 0x2d})
          	/Users/gugu/go/pkg/mod/github.com/wailsapp/wails/[email protected]/internal/binding/generate.go:141 +0x1a0
          github.com/wailsapp/wails/v2/internal/app.generateBindings(0x1400042a1c0)
          	/Users/gugu/go/pkg/mod/github.com/wailsapp/wails/[email protected]/internal/app/app_bindings.go:118 +0x1d0
          github.com/wailsapp/wails/v2/internal/app.(*App).Run(0x140004384b0)
          	/Users/gugu/go/pkg/mod/github.com/wailsapp/wails/[email protected]/internal/app/app_bindings.go:68 +0x394
          github.com/wailsapp/wails/v2/pkg/application.(*Application).Run(0x140004180a0)
          	/Users/gugu/go/pkg/mod/github.com/wailsapp/wails/[email protected]/pkg/application/application.go:73 +0x148
          github.com/wailsapp/wails/v2.Run(0x14000440000?)
          	/Users/gugu/go/pkg/mod/github.com/wailsapp/wails/[email protected]/wails.go:14 +0xa8
          main.main()
          	/Users/gugu/collibra/gojo/main.go:41 +0x628
          
          exit status 2

I've reverted If a field is exported, generate json even in the absence of tags locally and everything works again.

IMO, the fact that only fields with json tags were exported was a good design decision because it gives you control over what's exposed to the frontend.
Now with that change you have to think twice about the design of your struct to avoid leaking information to the frontend.

I'm wondering if we cannot find a middle ground by making this feature it opt-in or opt-out.

@leaanthony
Copy link
Member

I mean, it should never crash so I'd like to know what triggers this. To prevent export, just use non-exported values (lower case).

@fkhadra
Copy link
Contributor

fkhadra commented Dec 28, 2024

To prevent export, just use non-exported values (lower case).
I wish it was that simple. In my case, I call some external API and I have to parse the response, so my fields must all be exported, then I only want to expose some of them in the frontend. I can definitely solve this issue on my side by having one struct for the api, one for the frontend.

That being said, I created a project from scratch and a simple method like the one below is crashing wails generate module.

type TestStruct struct {
	A string `json:"a"`
	B string
}

func (a *App) Test() TestStruct {
	return TestStruct{
		A: "hello",
		B: "test",
	}
}
  ERROR   Converting type main.TestStruct
          - simple field TestStruct.A
          - simple field TestStruct.B
          
          panic: runtime error: invalid memory address or nil pointer dereference
          [signal SIGSEGV: segmentation violation code=0x2 addr=0x18 pc=0x104b8526c]
          
          goroutine 1 [running]:
          github.com/wailsapp/wails/v2/internal/binding.(*Bindings).GenerateModels(0x14000138ee0)
          	/Users/gugu/go/pkg/mod/github.com/wailsapp/wails/[email protected]/internal/binding/binding.go:142 +0x41c
          github.com/wailsapp/wails/v2/internal/binding.(*Bindings).WriteModels(0x14000027b98?, {0x140001289c0, 0x35})
          	/Users/gugu/go/pkg/mod/github.com/wailsapp/wails/[email protected]/internal/binding/binding.go:209 +0x28
          github.com/wailsapp/wails/v2/internal/binding.(*Bindings).GenerateGoBindings(0x14000138ee0, {0x140001289c0, 0x35})
          	/Users/gugu/go/pkg/mod/github.com/wailsapp/wails/[email protected]/internal/binding/generate.go:141 +0x1a0
          github.com/wailsapp/wails/v2/internal/app.generateBindings(0x14000138ee0)
          	/Users/gugu/go/pkg/mod/github.com/wailsapp/wails/[email protected]/internal/app/app_bindings.go:118 +0x1d0
          github.com/wailsapp/wails/v2/internal/app.(*App).Run(0x1400015e050)
          	/Users/gugu/go/pkg/mod/github.com/wailsapp/wails/[email protected]/internal/app/app_bindings.go:68 +0x394
          github.com/wailsapp/wails/v2/pkg/application.(*Application).Run(0x1400013a180)
          	/Users/gugu/go/pkg/mod/github.com/wailsapp/wails/[email protected]/pkg/application/application.go:73 +0x148
          github.com/wailsapp/wails/v2.Run(0x14000178000?)
          	/Users/gugu/go/pkg/mod/github.com/wailsapp/wails/[email protected]/wails.go:14 +0xa8
          main.main()
          	/Users/gugu/oss/wails-debug-types/main.go:19 +0x1b8
          
          exit status 2

I'll dig more to find the root cause, I'm just wondering if you're having the same issue on your side. I'm running all the commands on my terminal.

@leaanthony
Copy link
Member

@BenoitBotton - I'm still really keen to understand how you got the nil pointer dereference - I couldn't reproduce, even with your example.

Please try the above PR to see if this fixes your problem. If you are unsure how to do this, please follow this guide. Let us know how you get on 👍

@pbnjay
Copy link
Contributor

pbnjay commented Dec 28, 2024

I suspect the original issue here is the menu package not being scanned for export? Is it the only value expected in models.ts or are other structures not exported?

@fkhadra That sounds like a different issue since it’s crashing. If you mark the field tags as json:”-“ do you still have the issue with 2.9.2? It seems like you’re still relying on something you can make explicit

@BenoitBotton
Copy link
Contributor Author

@BenoitBotton - I'm still really keen to understand how you got the nil pointer dereference - I couldn't reproduce, even with your example.
I just tried my example under linux, no issue. I'll go back to windows and have another go.

Wails

Version | v2.9.2
Package Manager | pacman

System

┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
| OS | Manjaro Linux |
| Version | Unknown |
| ID | manjaro |
| Go Version | go1.23.4 |
| Platform | linux |
| Architecture | amd64 |
| CPU | AMD Ryzen 9 5900HX with Radeon Graphics |
| GPU 1 | GA104M [GeForce RTX 3070 Mobile / Max-Q] (NVIDIA Corporation) - Driver: nvidia |
| GPU 2 | Cezanne [Radeon Vega Series / Radeon Vega Mobile Series] (Advanced Micro Devices, Inc. [AMD/ATI]) - Driver: amdgpu |
| Memory | 63GB |
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

Dependencies

┌─────────────────────────────────────────────────────────────────────┐
| Dependency | Package Name | Status | Version |
| *docker | docker | Available | 1:27.3.1-1 |
| gcc | gcc | Installed | 14.2.1+r134+gab884fffe3fc-1 |
| libgtk-3 | gtk3 | Installed | 1:3.24.43-4 |
| libwebkit | webkit2gtk | Installed | 2.46.4-1 |
| npm | npm | Installed | 10.9.0-1 |
| pkg-config | pkgconf | Installed | 2.3.0-1 |
└────────────────────── * - Optional Dependency ──────────────────────┘

Diagnosis

Optional package(s) installation details:

  • docker: sudo pacman -S docker

SUCCESS Your system is ready for Wails development!

@BenoitBotton
Copy link
Contributor Author

BenoitBotton commented Dec 29, 2024

hmmm, no crash either under windows. But on closer inspection, models.ts is not generated and I get the below error:

  • Generating bindings: 2024/12/29 12:09:01 KnownStructs: keys.Accelerator   main.Msg menu.CallbackData       menu.Menu       menu.MenuItem
keys.Accelerator
KnownStructs: keys.Accelerator  main.Msg        menu.CallbackData       menu.Menu      menu.MenuItem
menu.Menu
Error: cannot find type for func (Click/Callback)

edit: having gone back to linux, I get the same error

@BenoitBotton
Copy link
Contributor Author

same thing if I use the PR

@BenoitBotton
Copy link
Contributor Author

BenoitBotton commented Dec 29, 2024

going back to the app in which I found the issue (I can't share that unfortunately), the models.ts is still deleted and the frontend doesn't build due to typescript errors. using the PR does not change anything

@fkhadra
Copy link
Contributor

fkhadra commented Dec 29, 2024

So in my case,

  • @pbnjay even with json:"-" on field B I'm still unable to generate the binding
  ERROR   Converting type main.TestStruct
          - simple field TestStruct.A
          
          panic: runtime error: invalid memory address or nil pointer dereference
          [signal SIGSEGV: segmentation violation code=0x2 addr=0x18 pc=0x101109a0c]
  • I tried with the PR explicit json tags and things are back to normal. However, it seems that "generate_all_exported_fields": true has no effect.

I'm trying to understand where the nil pointer dereference could come from, I'm a bit confused.

@fkhadra
Copy link
Contributor

fkhadra commented Dec 29, 2024

I have more update, after deleting ~/go/pkg/mod/github.com/wailsapp and rerunning go get -u I no longer have the pointer dereference issue and the model.ts is generated as expected.

On my other project though, the nil pointer issue is gone but the model.ts is not generated.

@pbnjay
Copy link
Contributor

pbnjay commented Jan 1, 2025

OK, After digging in a bit, my initial thought was wrong. It looks like this is triggered when trying to marshal Go-Exported values that are not json encodable (aka channel, complex numbers, or in this case, a callback function)

Specifically, the menu.MenuItem type has a Callback field which is a type alias for a func - so when the JSON generation for models.ts runs into these, it tries to export it but it cannot be represented.

So right before the .Tag.Lookup("json") call, a guard that checks the type can be json-encoded seems to be sufficient. I have a PR incoming that works perfectly on @BenoitBotton 's test repo!

@BenoitBotton
Copy link
Contributor Author

thanks @pbnjay
I can confirm it fixes the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
4 participants