Skip to content

fix: panic when running text-document example - #396

Open
malklera wants to merge 2 commits into
tdewolff:masterfrom
malklera:examples
Open

malklera wants to merge 2 commits into
tdewolff:masterfrom
malklera:examples

Conversation

@malklera

Copy link
Copy Markdown

The example uses 'DrawPreview' which has the path to a image hard coded, if your installation of the package is different, it will panic.

Changed to use OS agnostic path building.

It still panic if you run the example from outside the containing directory.

~/study/goLang/canvas/examples/
$ go run text-document/main.go
panic: open ../../resources/lenna.png: no such file or directory

goroutine 1 [running]:
main.drawDocument(0x26da9ad07dc8)
        /home/malklera/study/goLang/canvas/examples/text-document/main.go:86 +0x671
main.main()
        /home/malklera/study/goLang/canvas/examples/text-document/main.go:34 +0x3bd
exit status 2

I think somewhere it has to be indicated that the example has to be run from its containing directory, but i am not sure where because other examples use the function too e.g. opengl, tex, gio, fyne

A recommendation, add

// Deprecated: Only for use in examples.

To indicate that the function is not made to be part of the normal API of the package.

From what i noticed 'DrawPreviewWithAssets' should be marked the same, mostly because of the function signature, instead of accepting something like (assets ...any) to make it more generic/flexible.

The example uses 'DrawPreview' which has the path to a image
hard coded, if your installation of the package is different,
it will panic.

Changed to use OS agnostic path building.

It still panic if you run the example from outside the
containing directory.

```
~/study/goLang/canvas/examples/
$ go run text-document/main.go
panic: open ../../resources/lenna.png: no such file or directory

goroutine 1 [running]:
main.drawDocument(0x26da9ad07dc8)
        /home/malklera/study/goLang/canvas/examples/text-document/main.go:86 +0x671
main.main()
        /home/malklera/study/goLang/canvas/examples/text-document/main.go:34 +0x3bd
exit status 2
```

I think somewhere it has to be indicated that the example has to be
run from its containing directory, but i am not sure where because other
examples use the function too e.g. opengl, tex, gio, fyne

A recommendation, add

// Deprecated: Only for use in examples.

To indicate that the function is not made to be part of the
normal API of the package.

From what i noticed 'DrawPreviewWithAssets' should be marked the same,
mostly because of the function signature, instead of accepting
something like (assets ...any) to make it more generic/flexible.
@tdewolff

Copy link
Copy Markdown
Owner

The path building is OS agnostic (it uses filepath) but maybe the environment variables are not the same across OS'? The solution is not better, as you said it limits to executing it from a particular directory. The whole point was not to.

The DrawPreviewWithAssets shouldn't be more generic not flexible, it needs those exact byte streams...thanks for looking into this though! How can we fix the root direction for each OS and make this work?

Put the path building as it was.

Now the example can be run from anywhere in the repository.
@malklera

Copy link
Copy Markdown
Author

The more you know, just learned that this /home/malklera/go/src\github.com\tdewolff\canvas is valid.

The reason it panic when i try to run it is because the example seems to have been written before modules was introduced.

In my case i have this situation.

go env
...
GOPATH='/home/malklera/go'
...
ls /home/malklera/go
bin  pkg

So when it tries to build the path, it fails to find src

Made a new commit with a different take to fix DrawPreview, have to update the
text-document example too.

This branch has not been deployed

No deployments
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