1

Here is my previous post Golang template.ParseFiles "not a directory" error. I have snippet:

root_path, err := osext.Executable()
if err != nil {
    return err
}
templates_path := root_path + "/app/views/mailtemplates/" + "feedback"
text_path := templates_path + ".txt"

textTmpl, err := template.ParseFiles(text_path)
if err != nil {
    return err
}

and next error:

open /home/cnaize/gocode/bin/advorts/app/views/mailtemplates/feedback.txt: not a directory

If I hardcode to:

templates_path := "/home/cnaize/" + "feedback"

everything is ok. Where is the problem? I've tried to remove my bin, but it didn't help.

EDITED: My env variables:

export GOROOT="/usr/local/go"
export GOPATH=$HOME/gocode
export PATH=$PATH:$GOROOT/bin
export PATH="$PATH:$GOPATH/bin"

and my PATH variable:

PATH=/home/cnaize/.rvm/gems/ruby-2.1.0/bin:/home/cnaize/.rvm/gems/ruby-2.1.0@global/bin:/home/cnaize/.rvm/rubies/ruby-2.1.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/cnaize/.rvm/bin:/usr/local/go/bin:/home/cnaize/gocode/bin
Community
  • 1
  • 1
cnaize
  • 3,139
  • 5
  • 27
  • 45
  • Just to be sure: are any of the `/home/cnaize/gocode/bin/advorts/app/views/mailtemplates` folders regular directories? (no permission issue, no symlink?) – VonC Jul 15 '14 at 13:39
  • Also, can you test is `/home/cnaize/gocode/feedback.xxx` woudl work? If yes, then `/home/cnaize/gocode/bin/feedback.xxx`. If yes, ... and so on in order to pinpoint which sub-folder triggers the issue. – VonC Jul 15 '14 at 13:42
  • @VonC I've checked this locations, everything is ok. something wrong with my binary file `advorts`. and interesting thing: when I'm checking file existing, it returns `true`. may be something happed when I was relocate the project? where may be the problem? – cnaize Jul 16 '14 at 05:35
  • So you mean `/home/cnaize/gocode/bin/` works, but not `/home/cnaize/gocode/bin/advorts/xxx` ? – VonC Jul 16 '14 at 05:37
  • @VonC yes, it is. strange thing. I've tried to recreate project and remove my `advorts` file, but it doesn't work too – cnaize Jul 16 '14 at 06:26
  • Where are your sources? in `/home/cnaize/gocode/src/advorts/app/views/mailtemplates`? What `GOPATH` refers to? `/home/cnaize/gocode`? – VonC Jul 16 '14 at 06:27
  • @VonC yes, my sources are where. I've updated my post. may be the problem in my `PATH` variable? how to remove my `Dropbox` path from `PATH` variable? – cnaize Jul 16 '14 at 06:34
  • a simple `export PATH=...` (the all path minus the Dropbox part) would work. But I don't think it is the issue. If you do a `go run` within your *src* project folder `/home/cnaize/gocode/src/advorts/` (`/src/` not `/bin/`), would it work? – VonC Jul 16 '14 at 06:36
  • @VonC `/gocode/src/advorts$ go run advorts` => `go run: no go files listed` – cnaize Jul 16 '14 at 06:47
  • No, just go run it should compile and run (if you have at least a `go` file in the package `main` with a `main` function). That is different from a go install, which would copy the executable in `/home/cnaize/gocode/bin/advorts`, where resource files might not exist. – VonC Jul 16 '14 at 06:48
  • @VonC `/gocode/src/advorts$ go run` => `go run: no go files listed`. my `go version` is `go version go1.3 linux/amd64`. how resources coped to binary? on compile stage? I'm using `Revel`, it's generate my `main` file and this `main.go` is in `app/tmp/` path – cnaize Jul 16 '14 at 06:52
  • So you coulsd you try a go run in your `app/tmp` path? – VonC Jul 16 '14 at 06:54
  • @VonC `/gocode/src/advorts/app/tmp$ go run` => `go run: no go files listed` – cnaize Jul 16 '14 at 06:57
  • Ok, let me check on Revel – VonC Jul 16 '14 at 06:59
  • @VonC how `go run` supposed to work? I've just created simple `main.go` file in `~/gocode/src/test` and `go run` => `go run: no go files listed` – cnaize Jul 16 '14 at 07:12
  • Interesting. And `go run main.go`? I think that `go build` (without listing any go file should work) – VonC Jul 16 '14 at 07:47
  • @VonC `go run main.go` works and `go build` builds binary which I can run. what I supposed to do next? I've tried to run `go build` inside my `/gocode/src/advorts/app/tmp` and it build `tmp` file for me. I think, it's not what I need – cnaize Jul 16 '14 at 08:55
  • When you say go build builds binary which I can run, do you mean you don't see the "`not a directory`" error when running that binary? – VonC Jul 16 '14 at 10:27
  • In your reveal app, where is your snippet? In a controller as in http://revel.github.io/tutorial/requestflow.html? – VonC Jul 16 '14 at 10:36
  • @VonC no, I can't run my `tmp` binary with error `ERROR 2014/07/16 15:34:34 revel.go:256: GOPATH environment variable is not set. Please refer to http://golang.org/doc/code.html to configure your Go environment.` I don't know why – cnaize Jul 16 '14 at 11:36
  • @VonC no, my snippet is in `app/lib/mytools.go` but I call this from my `controller` – cnaize Jul 16 '14 at 11:38
  • Ok, I will monitor https://github.com/revel/revel/issues/666 then. – VonC Jul 16 '14 at 11:45
  • @VonC thx, I really don't know what to do this it – cnaize Jul 16 '14 at 12:17

1 Answers1

3

As per your fix, the right way to refer to the embedded templates/test.txt resource would be (using filepath.Dir):

root_path, err := osext.Executable()
template_path := filepath.Join(filepath.Dir(root_path), "templates", "test")
text_path := template_path + ".txt"

You would find a similar approach in cmd/go/build.go, for getting the package files:

func (gccgoToolchain) pkgpath(basedir string, p *Package) string {
    end := filepath.FromSlash(p.ImportPath + ".a")
    afile := filepath.Join(basedir, end)
    // add "lib" to the final element
    return filepath.Join(filepath.Dir(afile), "lib"+filepath.Base(afile))
}
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250