wails icon indicating copy to clipboard operation
wails copied to clipboard

Runtime Folder Deletes itself if you inject runtime manually to get access to wails runtime

Open taylorlanglopez opened this issue 2 years ago • 26 comments

I got the same errors as

D:\Gitlab\mccl>wails dev
Wails CLI v2.0.0-beta.42 

Executing: go mod tidy -compat=1.17
Executing: wails generate module 
Installing frontend dependencies... 

> [email protected] dev
> vite
 

  vite v2.9.15 dev server running at:
 
Vite Server URL: http://localhost:3000/ 
  > Local: http://localhost:3000/ 
  > Network: use `--host` to expose

  ready in 2370ms.

Running frontend DevWatcher command: 'npm run dev' 
Building application for development...
  - Installing frontend dependencies: Done. 
  - Compiling frontend: Done. 
  - Generating bundle assets: Done. 
  - Compiling application:
Build error - open D:\Gitlab\mccl\frontend\wailsjs\runtime\package.json: Access is denied.
No version running, build will be retriggered as soon as changes have been detected
Watching (sub)/directory: D:\Gitlab\mccl 
Using DevServer URL: http://localhost:34115
Using Frontend DevServer URL: http://localhost:3000/
Using reload debounce setting of 100 milliseconds
 
 
To develop in the browser and call your bound Go methods from Javascript, navigate to: http://localhost:34115
[Rebuild triggered] files updated 
  - Generating bundle assets: Done. 
  - Compiling application: Done. 

Added new directory to watcher: D:\Gitlab\mccl\frontend\wailsjs\runtime 
INF | Serving assets from frontend DevServer URL: http://localhost:3000/ 
DEB | WebView2 Runtime Version '104.0.1293.47' installed. Minimum version required: 94.0.992.31.
Error: mkdir D:\Gitlab\mccl\frontend\wailsjs\go\main: Access is denied. 
Development mode exited 
DevWatcher command exited!
DevWatcher command killed!


Situation

Under I monitored, I found some strange things.

  1. First, I need to run wails build to generate two folders go runtime.
  2. Then, run wails dev, at - Installing frontend dependencies: ... , it will delete the runtime folder.
  3. Then, wails cannot find the runtime and report errors.

Resolve

I try to give frontend\wailsjs\runtime\package.json the r&w permissions.

  1. wails build to get the runtime folder.
  2. Used the windows file properties to remove read.
  3. run chmod 777 frontend/wailsjs/runtime/package.json
  4. run wails dev
  5. Unfortunately, frontend\wailsjs\runtime\package.json still removed.

I try to keep the runtime folder.

  1. run wails dev
  2. get the runtime go folders.
  3. run chmod 777 frontend/wailsjs/runtime/package.json and use the windows file properties to remove read propertie of go and runtime between Executing: wails generate module and Installing frontend dependencies: ...
  4. Then wails will keep building, nothing reported.
  5. Then reported Error: mkdir frontend\wailsjs\go\main: Access is denied.
  6. I found the go folder removed.

I try to watch the go folder.

  1. run wails dev
  2. get the go folders.
  3. run chmod 777 frontend/wailsjs/runtime/package.json and use the windows file properties to remove read propertie of go and runtime between Executing: wails generate module and Installing frontend dependencies: ...
  4. Then, All errors are gone.

It's amazing, isn't it?

But then I edit files,

[Rebuild triggered] files updated 
  - Generating bundle assets: Done. 
  - Compiling application:  
Build error - open D:\Gitlab\mccl\frontend\wailsjs\runtime\package.json: Access is denied. 

I found runtime deleted again.

sad...

So I guess this is a question of permissions.

Originally posted by @Maicarons2022 in https://github.com/wailsapp/wails/issues/1752#issuecomment-1219007533

taylorlanglopez avatar Feb 10 '23 21:02 taylorlanglopez

I have also granted full permissions to everyone just like this person. I have the same issue where the runtime folder just deletes itself if I want to have working code for wails build.

This is still broken. The runtime folder will always delete itself if you want to inject the runtime into a component for example.

Vue Component (Works but breaks wails dev)

<script setup lang="ts">
import * as wailsrt from '../../wailsjs/runtime/runtime'
....
wailsrt.EventsOn("progress", (proginc: string) => {
 //code to update a progress bar
}
...
</script>

Vue Component (Doesn't work and throws a compiler error on type-check)

<script setup lang="ts">
....
window.runtime.EventsOn("progress", (proginc: string) => {
 //code to update a progress bar
}
...
</script>

The second example is "correct" according to the documentation, but it just throws a compiler error.

/src/components/File.vue(48,8): error TS2339: Property 'runtime' does not exist on type 'Window & typeof globalThis'

I'm really lost as to how I get access to the runtime inside a component without breaking wails dev.

My structure is src/components src/views App.vue main.ts

I have not modified main.ts (main.js) at all, I added a router to App.vue and il8n.

My App.vue has a from the standard Vue router in order to host the View for each component.

taylorlanglopez avatar Feb 10 '23 21:02 taylorlanglopez

I got the same error as u,did u work it out?

Francisundermoon avatar Feb 12 '23 12:02 Francisundermoon

Thanks for using Wails and bringing up this issue.

Unfortunately AFAIK none of the Core Dev were ever able to reproduce this. I've also followed your reproduction guide and added a vue component with the specified import and the issue didn't occur here.

I suspect this is a timing issue between vite and the creation of the runtime/bindings. Nevertheless I've created a Draft PR which should improve this situation. Would it be possible for you to give PR #2372 a try according to this guide?

stffabi avatar Feb 13 '23 06:02 stffabi

Thanks for using Wails and bringing up this issue.

Unfortunately AFAIK none of the Core Dev were ever able to reproduce this. I've also followed your reproduction guide and added a vue component with the specified import and the issue didn't occur here.

I suspect this is a timing issue between vite and the creation of the runtime/bindings. Nevertheless I've created a Draft PR which should improve this situation. Would it be possible for you to give PR #2372 a try according to this guide?

Wow,it worked!!!tks again for the effort u guys made!谢谢~

Francisundermoon avatar Feb 13 '23 08:02 Francisundermoon

Awesome, thanks for your feedback and testing it.

Let's see what the other people say that are suffering from this and if it does fix it we could take the PR out of draft.

stffabi avatar Feb 13 '23 08:02 stffabi

Awesome, thanks for your feedback and testing it.

Let's see what the other people say that are suffering from this and if it does fix it we could take the PR out of draft.

after applying the new PR u proposed,the mode of dev works well,but when i altered my code ,the dev watcher retrigerred that error again,differed from the last message ,it saysthat Failed to resolve import "../../wailsjs/runtime"(Runtime Folder Deletes itself) ,which force me to restart the dev mode onemore time

Francisundermoon avatar Feb 13 '23 09:02 Francisundermoon

Hmm, I had suspected something like that might happen. I really think there's something that vite is locking there when deleting the files and recreating them. My first impression was that this only happens during startup because starting vite and compiling the app might run in parallel.

We could try to skip rebuilding the runtime, because that won't ever change during reload. Hopefully there's not the same problem with the no normal bindings. Will let you know when the PR is updated.

stffabi avatar Feb 13 '23 09:02 stffabi

@chuyijia could you please try the updated version of the PR? Please also make sure to have the correct replace in your go.mod in place, since the latest changes also affect the binary itself.

stffabi avatar Feb 13 '23 09:02 stffabi

@chuyijia could you please try the updated version of the PR? Please also make sure to have the correct replace in your go.mod in place, since the latest changes also affect the binary itself.

Gladly,the lose of runtime folder is no concern anymore(your lastest pr worked).Sadly, the go/module folder disappeared as follow(the error show that Failed to resolve import "../wailsjs/go/module/App" from "utils\bsc.ts". Does the file exist?) 0.0

Francisundermoon avatar Feb 13 '23 10:02 Francisundermoon

Seems like Vite (at least on some systems) has problems when a folder is deleted and recreated with new content. Seems like we probably need to sync that folders instead of just deleting and recreating them to work around this.

stffabi avatar Feb 13 '23 11:02 stffabi

Unfortunately, updating my wails is kind of a hassle due to corporate constraints. It takes me multiple days to get stuff updated on the machine where I'm experiencing issues. I can try and recreate my issue with runtime injection and the folder deletion in a public GitHub repo, but that requires me rewriting a significant amount of code. I'll try and get it to this thread by tomorrow. I will also try the PR when I can, thanks for your help.

taylorlanglopez avatar Feb 13 '23 18:02 taylorlanglopez

This is a pure permissions issue, I haven't tracked down what permission scheme makes the wails dev runtime folder mess itself up, but I cannot replicate the same issue outside of my corporate network. I am going to try the PR you posted today to see if the PR can fix it on my work network.

taylorlanglopez avatar Feb 14 '23 18:02 taylorlanglopez

@taylorlanglopez Have you solved it, I am also this Access is denied.

107295472 avatar Mar 09 '23 01:03 107295472

@taylorlanglopez any update?

leaanthony avatar Apr 14 '23 23:04 leaanthony

@taylorlanglopez any update?

Not yet. Unfortunately I've been travelling for work and haven't been on that isolated network for awhile. So I haven't been able to look at the permissions structure.

taylorlanglopez avatar Apr 14 '23 23:04 taylorlanglopez

Also having this issue on Windows, the wailsjs/runtime directory is not being created. panic: open <path>\wailsjs\runtime\package.json: Access is denied

The wailsjs has drwxr-xr-x permissions (755). Wails CLI version v2.5.1.

Works fine on Linux (the wailsjs directory was pushed to Git from Linux).

Edit: This problem occurs in a Virtual Machine (VirtualBox), but a dual boot seems to work fine.

Edit2: I will setup Wails for local IDE development tomorrow and have a go at finding the cause. Should really be using stack traces ( https://github.com/rotisserie/eris or https://github.com/pkg/errors instead of err.Error(), makes debugging much easier.

Edit3: Got a stack-trace, need to go deeper...

Build error: exit status 2

panic: Failed to start Wails: open C:\Users\user\Documents\goforensics\frontend\wailsjs\runtime\package.json: Access is denied.

goroutine 1 [running]:
main.main()
        C:/Users/user/Documents/goforensics/goforensics.go:142 +0x111f


github.com/wailsapp/wails/v2/pkg/commands/build.GenerateBindings
        C:/Users/user/Documents/wails/v2/pkg/commands/build/build.go:232
github.com/wailsapp/wails/v2/pkg/commands/build.Build
        C:/Users/user/Documents/wails/v2/pkg/commands/build/build.go:129
github.com/wailsapp/wails/v2/cmd/wails/internal/dev.restartApp
        C:/Users/user/Documents/wails/v2/cmd/wails/internal/dev/dev.go:285
github.com/wailsapp/wails/v2/cmd/wails/internal/dev.Application
        C:/Users/user/Documents/wails/v2/cmd/wails/internal/dev/dev.go:122
main.devApplication
        C:/Users/user/Documents/wails/v2/cmd/wails/dev.go:36
reflect.Value.call
        C:/Program Files/Go/src/reflect/value.go:586
reflect.Value.Call
        C:/Program Files/Go/src/reflect/value.go:370
github.com/leaanthony/clir.(*Command).NewSubCommandFunction.func1
        C:/Users/user/go/pkg/mod/github.com/leaanthony/[email protected]/command.go:350
github.com/leaanthony/clir.(*Command).run
        C:/Users/user/go/pkg/mod/github.com/leaanthony/[email protected]/command.go:108
github.com/leaanthony/clir.(*Command).run
        C:/Users/user/go/pkg/mod/github.com/leaanthony/[email protected]/command.go:87
github.com/leaanthony/clir.(*Cli).Run
        C:/Users/user/go/pkg/mod/github.com/leaanthony/[email protected]/cli.go:73
main.main
        C:/Users/user/Documents/wails/v2/cmd/wails/main.go:96
runtime.main
        C:/Program Files/Go/src/runtime/proc.go:250
runtime.goexit
        C:/Program Files/Go/src/runtime/asm_amd64.s:1598
github.com/wailsapp/wails/v2/pkg/commands/build.Build
        C:/Users/user/Documents/wails/v2/pkg/commands/build/build.go:131
github.com/wailsapp/wails/v2/cmd/wails/internal/dev.restartApp
        C:/Users/user/Documents/wails/v2/cmd/wails/internal/dev/dev.go:285
github.com/wailsapp/wails/v2/cmd/wails/internal/dev.Application
        C:/Users/user/Documents/wails/v2/cmd/wails/internal/dev/dev.go:122
main.devApplication
        C:/Users/user/Documents/wails/v2/cmd/wails/dev.go:36
reflect.Value.call
        C:/Program Files/Go/src/reflect/value.go:586
reflect.Value.Call
        C:/Program Files/Go/src/reflect/value.go:370
github.com/leaanthony/clir.(*Command).NewSubCommandFunction.func1
        C:/Users/user/go/pkg/mod/github.com/leaanthony/[email protected]/command.go:350
github.com/leaanthony/clir.(*Command).run
        C:/Users/user/go/pkg/mod/github.com/leaanthony/[email protected]/command.go:108
github.com/leaanthony/clir.(*Command).run
        C:/Users/user/go/pkg/mod/github.com/leaanthony/[email protected]/command.go:87
github.com/leaanthony/clir.(*Cli).Run
        C:/Users/user/go/pkg/mod/github.com/leaanthony/[email protected]/cli.go:73
main.main
        C:/Users/user/Documents/wails/v2/cmd/wails/main.go:96
runtime.main
        C:/Program Files/Go/src/runtime/proc.go:250
runtime.goexit
        C:/Program Files/Go/src/runtime/asm_amd64.s:1598

Edit4: I am unable to get the stack-trace produced by internal/app/app_bindings.go which is called by compiling with the bindings flag in pkg/commands/bindings.go.

mooijtech avatar Jun 13 '23 13:06 mooijtech

I was also trying to trace these errors, they break the react-ts/angular workflow for me on Windows 10... might happen with all TS templates, not sure.

  • Error: open C:\dev\myproject\frontend\wailsjs\runtime\package.json: Access denied. Seems to be caused by vite, as already mentioned. PR 2372 fixes on initialization, but when changing app.go I get:

     • Generating bindings: 2023/06/25 15:38:22 Error: CreateFile C:\dev\wails-react\frontend\wailsjs\go\main: Acesso negado.
    ...
     [vite] Internal server error: Failed to resolve import "../wailsjs/go/main/App" from "src\App.tsx". Does the file exist?
    
  • Error: CreateFile C:\dev\myproject\frontend\wailsjs\go\main: Access denied. This one happens after I open App.tsx on vscode, seems to be caused by vscode's TS analyzer. Happens on master branch too.


Steps to reproduce both errors on my machine

go install github.com/wailsapp/wails/v2/cmd/wails@latest

wails doctor

Wails CLI v2.5.1

 SUCCESS  Done.

# System

OS           | Windows 10 Pro
Version      | 1909 (Build: 18363)
ID           |
Go Version   | go1.20.5
Platform     | windows
Architecture | amd64

# Wails

Version | v2.5.1

# Dependencies

Dependency | Package Name | Status    | Version
WebView2   | N/A          | Installed | 114.0.1823.51
Nodejs     | N/A          | Installed | 16.13.2
npm        | N/A          | Installed | 8.1.2
*upx       | N/A          | Available |
*nsis      | N/A          | Available |
* - Optional Dependency

# Diagnosis

Your system is ready for Wails development!
Optional package(s) installation details:
  - upx : Available at https://upx.github.io/
  - nsis : More info at https://wails.io/docs/guides/windows-installer/

 ♥   If Wails is useful to you or your company, please consider sponsoring the project:
https://github.com/sponsors/leaanthony

wails init -n myproject -t react-ts

cd .\myproject\

wails dev

...
  • Generating bindings:
2023/06/24 23:53:24 Error: open C:\dev\myproject\frontend\wailsjs\runtime\package.json: Acesso negado.
...

Ctrl+C

code .

On vscode, open frontend/src/App.tsx. Go back to powershell

wails dev

Wails CLI v2.5.1

Executing: go mod tidy
  • Generating bindings:
2023/06/24 23:55:01 Error: CreateFile C:\dev\myproject\frontend\wailsjs\go\main: Acesso negado.
...

Easiest way to test "go\main: Access denied"
  • It happens on master branch too.
  • Create a react-ts project: wails init -n myproject -t react-ts
  • Open myproject in vscode.
  • Open the file App.tsx, this will trigger vscode's TS analyzer. Without this step the error doesn't happen.
  • On my project's root: go build -tags bindings -o wailsbindings.exe
  • Run several times at random intervals: .\wailsbindings.exe. It fails sometimes, example:
PS C:\dev\myproject> go build -tags bindings -o wailsbindings.exe
PS C:\dev\myproject> .\wailsbindings.exe
Error: CreateFile C:\dev\myproject\frontend\wailsjs\go\main: Access denied.
PS C:\dev\myproject> .\wailsbindings.exe
PS C:\dev\myproject> .\wailsbindings.exe
PS C:\dev\myproject> .\wailsbindings.exe
Error: CreateFile C:\dev\myproject\frontend\wailsjs\go\main: Access denied.
PS C:\dev\myproject> .\wailsbindings.exe
PS C:\dev\myproject> .\wailsbindings.exe
PS C:\dev\myproject> .\wailsbindings.exe
PS C:\dev\myproject> .\wailsbindings.exe
Error: CreateFile C:\dev\myproject\frontend\wailsjs\go\main: Access denied.
PS C:\dev\myproject> .\wailsbindings.exe
PS C:\dev\myproject> .\wailsbindings.exe
Error: CreateFile C:\dev\myproject\frontend\wailsjs\go\main: Access denied.

More info: Visual Studio Code, no TS extensions but I'll try disabling the others. Windows Defender disabled, no AVs installed.

haroflow avatar Jun 25 '23 19:06 haroflow

Thanks for the info @haroflow - Do you think simply overwriting the files would be enough?

leaanthony avatar Jun 25 '23 22:06 leaanthony

@leaanthony You're welcome!

I was thinking about it, not sure if the lock will allow that... don't have much experience with locks on Windows but it's worth a shot.

Some other options, just to throw it out there:

  • Move the folder and create another one in its place.
  • Don't remove folders, just remove files. Not sure if it's possible and if it would change anything, but I noticed some error was on folder deletion.
  • On error, try again a few times to delete/create those files. Sounds bad though, would probably need a few seconds between tries... should be the last resort. I'm mentioning this because it seems that the lock is not held forever, only briefly after changes to the file.
  • Stop vite, change files and restart vite? Don't know how long that takes... and doesn't solve vscode's TS analyzer problem.
  • Any way to use vite to generate bindings as a compilation step? I never studied vite, it's on my list =P

I also wasn't able to find the exact code that fails yet, will give another try this week.

haroflow avatar Jun 26 '23 02:06 haroflow

I spent a few more hours on this:

  • Tried disabling all extensions in vscode, didn't help.

  • Tried to "retry on failure" wherever wails removed/created the bindings just to get a sense of what is going on, and it helped a bit, but it's still failing with: [vite] Internal server error: Failed to resolve import "../wailsjs/go/main/App" from "src\App.tsx". Does the file exist?. So it seems that Vite tries to read files between wails' changes which is problematic. Here are the changes for reference, but it's terrible: https://gist.github.com/haroflow/34f58450b148cf243b5b8d38c54619cd

  • Knowing that vite is doing this, I tried a few vite.config.ts options:

    • build: { watch: { buildDelay: 2000 } }: doesn't work, wails dev gets stuck on Compiling frontend.
    • server: { watch: { usePolling: true } }: seems to have fixed all errors for me, even with the latest Wails. Needs more testing.

Windows 10. Windows Defender disabled.

haroflow avatar Jul 09 '23 23:07 haroflow

Thanks for putting in the time on this 🙏 Sounds promising!

leaanthony avatar Jul 10 '23 10:07 leaanthony

I seem to have found a fix step, but I don't know how it works or if it works for anyone else. I did a test for this:

  1. Assuming "main-2" is your application, when you run the wails dev command, you get two errors. (white font + red font) 01

  2. To fix this bug, you'll need to create another project. For example wails init -n test -t svelte 02

  3. After that, you can go to the frontend folder and download the npm package you need. 03

  4. Use the command wails dev to run the initial application. This may load something, if this step is missing, it may result in the repair failing. (I don't know how it works) We can see an error message in white font, but this does not affect the running. 04

  5. Assuming "main-2" is your application, copy the project to the "test" directory. (Except for the build and frontend folders) 05

  6. Delete the frontend/src folder in the test project. 06

  7. Similar to step 5, copy the frontend/src folder. 07

  8. Use the command wails dev to run the brand new "test" project. We can see an error message in white font, but this does not affect the running. 08

During testing, I found that a lot of behaviors lead to fatal errors in red fonts: 2.1 Using the npm install command to get packages while developing a project, and then running the wails dev command produces a fatal error with red fonts. If you use npm install to get these packages ahead of time, and then develop your project, you won't get an error. ( The question of the order of execution ) 2.2 "main-2" works fine, copy it as "main-3". All files for "main-2" and "main-3" are the same, but the copied "main-3" produces a fatal error with red font when running wails dev. ( Problems when copying project folders )

Combining step 4 with testing 2.1, I think it might be an issue with npm package loading?

CLincat avatar May 25 '24 08:05 CLincat

Also, when encountering a fatal error with red fonts, the command wails dev fails to work, but the command wails build works fine.

CLincat avatar May 25 '24 08:05 CLincat

Are your Dev and non Dev dependencies the same?

leaanthony avatar May 26 '24 01:05 leaanthony

Yes, they are the same.

CLincat avatar May 26 '24 02:05 CLincat

I retested "testing 2.2" and found two things in common.

  1. Project "main-3" is working fine, I copied it as "test". 51

  2. Fatal error with red font after using the command wails dev in project "test". By contrasting, I found two differences. 52

    2.1 The frontend/wailsjs/runtime/ folder is deleted. 53

    2.2 The frontend/node_modules/.vite/deps/ folder is deleted and replaced by deps_temp. 54

  3. deps/ and deps_temp/. 55 56

powershell_CJnneyavGS

I found that my previous projects (e.g. "main-2") had the same wrong behavior 2.1 + 2.2 Project main-3 works fine and does not result in a deps_temp directory during the wails dev process.

CLincat avatar May 26 '24 03:05 CLincat