Skip to content

text/template: Template redefinition doesn't work when new template content is empty or whitespace-only. #16913

@dmitshur

Description

@dmitshur

This is potentially related to #16912. I couldn't really find explicit documentation on template redefinition, and any limitations/restrictions on it.

From looking around the CL that implemented it and its commit message, it made it sound like it should be possible to simply redefine any template by defining it again, and then have it take on the new template content.

In other words, I expected a template redefined to new content X to behave as if it were a template first created with content X. This turned out not to be the case for some content X (so far, it happens when content is empty or contains whitespace and template comments only).

  1. What version of Go are you using (go version)?

    go version devel +2f679d7 Sun Aug 28 21:38:09 2016 +0000 linux/amd64
    

    (That's the latest tip commit 2f679d7 from a day ago as of posting this.)

    I initially ran into this issue on Go 1.7, but I tried it on latest tip in order to check if it has since been fixed or not. It's not.

  2. What operating system and processor architecture are you using (go env)?

    $ go env
    GOARCH="amd64"
    GOBIN=""
    GOEXE=""
    GOHOSTARCH="amd64"
    GOHOSTOS="linux"
    GOOS="linux"
    GOPATH="/home/ubuntu/GoPath"
    GORACE=""
    GOROOT="/home/ubuntu/go"
    GOTOOLDIR="/home/ubuntu/go/pkg/tool/linux_amd64"
    CC="gcc"
    GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build390318819=/tmp/go-build -gno-record-gcc-switches"
    CXX="g++"
    CGO_ENABLED="1"
    
  3. What did you do?
    If possible, provide a recipe for reproducing the error.
    A complete runnable program is good.
    A link on play.golang.org is best.

    I redefined a template with an empty template, or a template consisting of whitespace (and template comments).

    https://play.golang.org/p/c3K-zyJ4dd

  4. What did you expect to see?

    Names:
    - Gamora
    - Groot
    - Nebula
    - Rocket
    - Star-Lord
    Names:Names:
    
  5. What did you see instead?

    The template redefinition was effectively ignored. No error, and the old template contents were still used.

    Names:
    - Gamora
    - Groot
    - Nebula
    - Rocket
    - Star-Lord
    Names:
    - Gamora
    - Groot
    - Nebula
    - Rocket
    - Star-Lord
    Names:
    - Gamora
    - Groot
    - Nebula
    - Rocket
    - Star-Lord
    

    If you include any non-whitespace character in the redefined template content, such as x, then it works as expected, e.g.:

    Names:
    - Gamora
    - Groot
    - Nebula
    - Rocket
    - Star-Lord
    Names:xNames:x
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions