EEx gripe / Compile Warning?

47 views
Skip to first unread message

Austin Ziegler

unread,
Dec 4, 2020, 12:46:55 PM12/4/20
to elixir-l...@googlegroups.com
I’ve had a bug in my code for the last couple of months caused by the fact that EEx isn’t ERB. Specifically, I had code that looked like:

```eex
<% if @password %>
Temporary Password: <%= @password %>
<% else %>
Sign in with your existing password.
<% end %>
```

Looking at it in isolation, it’s really obvious that the first line should have been `<%= if @password %>`, but in situ…I’ve gotten multiple bug reports on this but have never been able to see what the problem was until today.

Is it possible to modify the EEx compile phase so that code like I wrote above generates at least a warning? As I understand it, there’s no case where a starting EEx block will usefully begin with `<%` instead of `<%=`.

-a
--

Jim Freeze

unread,
Dec 4, 2020, 12:50:06 PM12/4/20
to elixir-l...@googlegroups.com
Yes, those bugs are annoying, but I commonly start *.html.leex docs with

<% somevar = "blah" %>
...
<div class="<%= somevar %>"> ...

Jim
--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAJ4ekQs2G4HUX_y8q-UqYyTY4z5nX6JMOKdMb%3DcZSZwgpDxk0Q%40mail.gmail.com.

Austin Ziegler

unread,
Dec 4, 2020, 12:53:37 PM12/4/20
to elixir-l...@googlegroups.com
That makes sense. Maybe the compiler can detect cases like this:

```eex
<% variable = 'boo' %> <!-- OK -->
<% if @bar do %> <!-- Warning or error, because of the block? -->
```

-a

Floating Front

unread,
Dec 4, 2020, 12:55:32 PM12/4/20
to elixir-l...@googlegroups.com
Apologies to chip in here, I am certainly no expert in this area, but I think your assumption about “no case where a starting EEx block…” - I have used it in generating json from directory traversal, and would not get away without it…
just my five pennies…
R.
Fridrik.
 

Austin Ziegler

unread,
Dec 4, 2020, 1:05:07 PM12/4/20
to elixir-l...@googlegroups.com
Not discounting, just trying to understand. You’re saying you have something like:

```eex
<% for dir <- directories do %>
<%   for file <- File.ls(dir) do %>
{ "file": <%= file %> }
<% end %>
<% end %>
```

Based on what I’ve seen, that JSON bit won’t generate because neither of the for loops uses `<%=`.

Or are you doing something more like Jim’s case where you’re using a for loop to configure a local variable and then using `<%=` forms to generate?

I’m perfectly willing to be wrong in this case, but ultimately this feels like something that the compiler should be able to detect some cases like this, as the documentation calls this out explicitly (https://hexdocs.pm/eex/EEx.html#module-tags; see the paragraph about `if` statements).

If I’m wrong, then I withdraw the request. I’m just annoyed that I was bitten by this for two months and the ease with which I missed it every time I looked at the code.

-a

Unified Front

unread,
Dec 4, 2020, 1:26:49 PM12/4/20
to elixir-l...@googlegroups.com
Hi - please accept my apologies beforehand, for invading this discussion - I realise now that I actually don’t START a eex block anywhere without "<%=…” - this was from memory - I grep-ed through everything and nowhere do I start it like that…

… regarding your JSON bit, I take the output from Path.wildcard and feed it through eex, it is not pretty… :-)
R.
Fridrik.


Jim Freeze

unread,
Dec 4, 2020, 1:30:31 PM12/4/20
to elixir-l...@googlegroups.com
Watching for a block won't catch all cases, but I can't think of any cases where a block is used and the intent would not be to use its content.
My guess is that this could catch quite a few problems.

Jim


Dr. Jim Freeze, Ph.D.


Aaron Ross

unread,
Dec 5, 2020, 12:40:26 PM12/5/20
to elixir-lang-core
Just to play devil's advocate, this is technically valid

```eex
<% if @condition do %>
  <% some_side_effect() %>
<% end %>
```

though that's very contrived and I can't think of a valid use-case where you wouldn't instead want

```eex
<% if @condition do
  some_side_effect()
end %>
```

+1 for adding a warning

José Valim

unread,
Dec 5, 2020, 3:42:12 PM12/5/20
to elixir-l...@googlegroups.com
Yeah, we can try adding a warning if you use a block without `=`. Please open up an issue.

Aaron Ross

unread,
Dec 12, 2020, 7:16:12 PM12/12/20
to elixir-lang-core
Done! The wording for the error message could probably use some work, but I believe this addresses the issue: https://github.com/elixir-lang/elixir/pull/10566.

Thanks all for the input.

Austin Ziegler

unread,
Dec 14, 2020, 9:50:07 AM12/14/20
to elixir-l...@googlegroups.com
Thank you very much, Aaron! My future EEx non-bugs thank you.

Reply all
Reply to author
Forward
0 new messages