gobuffalo / buffalo

Rapid Web Development w/ Go
http://gobuffalo.io
MIT License
8.08k stars 578 forks source link

Resource Generators build T.*i18n definitions without import support #1601

Closed ricktimmis closed 5 years ago

ricktimmis commented 5 years ago

Description

version 0.14.0 Creating a new buffalo application, followed by generating a subsequent resource results in failure of 'buffalo dev' to run. This is due to model files being generated with 'undefined T' i18n translation, because support for i18n has been removed from app.go imports

Steps to Reproduce the Problem

  1. Create a new buffalo app -> buffalo new testapp
  2. Generate a resource -> buffalo g resource users username firstname lastname
  3. Run migration -> buffalo pop migrate up
  4. Run dev server -> buffalo dev

Expected Behavior

Expect the development server to run, and give you the default screen with various /user endpoints

Actual Behaviour

Build fails with

actions/users.go:112:27: undefined: T actions/users.go:172:27: undefined: T actions/users.go:199:27: undefined: T

Info

Please run buffalo info and paste the information below where it says "PASTE_HERE".

``` ### Buffalo Version v0.14.0 ### App Information Pwd=/home/ricktimmis/Go/src/github.com/ricktimmis/motorhomeclub Root=/home/ricktimmis/Go/src/github.com/ricktimmis/motorhomeclub GoPath=/home/ricktimmis/Go PackagePkg=github.com/ricktimmis/motorhomeclub ActionsPkg=github.com/ricktimmis/motorhomeclub/actions ModelsPkg=github.com/ricktimmis/motorhomeclub/models GriftsPkg=github.com/ricktimmis/motorhomeclub/grifts WithModules=false Name=motorhomeclub Bin=bin/motorhomeclub VCS=git WithPop=true WithSQLite=false WithDep=false WithWebpack=true WithNodeJs=true WithYarn=true WithDocker=true WithGrifts=true AsWeb=true AsAPI=false PackageJSON={map[]} ### Go Version go version go1.10.4 linux/amd64 ### Go Env GOARCH="amd64" GOBIN="/home/ricktimmis/Go/bin" GOCACHE="/home/ricktimmis/.cache/go-build" GOEXE="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOOS="linux" GOPATH="/home/ricktimmis/Go" GORACE="" GOROOT="/usr/lib/go-1.10" GOTMPDIR="" GOTOOLDIR="/usr/lib/go-1.10/pkg/tool/linux_amd64" GCCGO="gccgo" CC="gcc" CXX="g++" CGO_ENABLED="1" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build737045765=/tmp/go-build -gno-record-gcc-switches" ### Node Version v8.11.4 ### NPM Version 5.8.0 ### Yarn Version 1.7.0 ### PostgreSQL Version PostgreSQL Not Found ### MySQL Version mysql Ver 14.14 Distrib 5.7.25, for Linux (x86_64) using EditLine wrapper ### SQLite Version 3.24.0 2018-06-04 19:24:41 c7ee0833225bfd8c5ec2f9bf62b97c4e04d03bd9566366d5221ac8fb199aalt1 ### Dep Version could not find a Gopkg.toml file ### Dep Status could not find a Gopkg.toml file ```
markbates commented 5 years ago

I'm unable to reproduce this. My generated actions/app.go file has T in it:

package actions

import (
    "github.com/gobuffalo/buffalo"
    "github.com/gobuffalo/envy"
    forcessl "github.com/gobuffalo/mw-forcessl"
    paramlogger "github.com/gobuffalo/mw-paramlogger"
    "github.com/unrolled/secure"

    "github.com/gobuffalo/buffalo-pop/pop/popmw"
    csrf "github.com/gobuffalo/mw-csrf"
    i18n "github.com/gobuffalo/mw-i18n"
    "github.com/gobuffalo/packr/v2"
    "github.com/markbates/coke/models"
)

// ENV is used to help switch settings based on where the
// application is being run. Default is "development".
var ENV = envy.Get("GO_ENV", "development")
var app *buffalo.App
var T *i18n.Translator

// App is where all routes and middleware for buffalo
// should be defined. This is the nerve center of your
// application.
//
// Routing, middleware, groups, etc... are declared TOP -> DOWN.
// This means if you add a middleware to `app` *after* declaring a
// group, that group will NOT have that new middleware. The same
// is true of resource declarations as well.
//
// It also means that routes are checked in the order they are declared.
// `ServeFiles` is a CATCH-ALL route, so it should always be
// placed last in the route declarations, as it will prevent routes
// declared after it to never be called.
func App() *buffalo.App {
    if app == nil {
        app = buffalo.New(buffalo.Options{
            Env:         ENV,
            SessionName: "_coke_session",
        })

        // Automatically redirect to SSL
        app.Use(forceSSL())

        // Log request parameters (filters apply).
        app.Use(paramlogger.ParameterLogger)

        // Protect against CSRF attacks. https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)
        // Remove to disable this.
        app.Use(csrf.New)

        // Wraps each request in a transaction.
        //  c.Value("tx").(*pop.Connection)
        // Remove to disable this.
        app.Use(popmw.Transaction(models.DB))

        // Setup and use translations:
        app.Use(translations())

        app.GET("/", HomeHandler)

        app.Resource("/widgets", WidgetsResource{})
        app.ServeFiles("/", assetsBox) // serve files from the public directory
    }

    return app
}

// translations will load locale files, set up the translator `actions.T`,
// and will return a middleware to use to load the correct locale for each
// request.
// for more information: https://gobuffalo.io/en/docs/localization
func translations() buffalo.MiddlewareFunc {
    var err error
    if T, err = i18n.New(packr.New("app:locales", "../locales"), "en-US"); err != nil {
        app.Stop(err)
    }
    return T.Middleware()
}

// forceSSL will return a middleware that will redirect an incoming request
// if it is not HTTPS. "http://example.com" => "https://example.com".
// This middleware does **not** enable SSL. for your application. To do that
// we recommend using a proxy: https://gobuffalo.io/en/docs/proxy
// for more information: https://github.com/unrolled/secure/
func forceSSL() buffalo.MiddlewareFunc {
    return forcessl.Middleware(secure.Options{
        SSLRedirect:     ENV == "production",
        SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
    })
}

I generated a resource, started the app, and everything is fine.

Please try again, and if the problem persists, please re-open the ticket with any more details you might have.

stanislas-m commented 5 years ago

@markbates If I read correctly, the problem occurs when the var T *i18n.Translator and i18n stuff are removed.

markbates commented 5 years ago

This is due to model files being generated with 'undefined T' i18n translation, because support for i18n has been removed from app.go imports

I read it as the translation stuff wasn't generated.