# A JS backend bug... workaround?

**URL:** <https://discuss.kotlinlang.org/t/a-js-backend-bug-workaround/507>\
**Category:** Uncategorized\
**Created:** [August 19, 2014, 7:49pm UTC](https://discuss.kotlinlang.org/t/a-js-backend-bug-workaround/507 "2014-08-19T19:49:02Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![RobNik](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@RobNik](https://discuss.kotlinlang.org/u/RobNik)\
**Post date:** [August 19, 2014, 7:49pm UTC](https://discuss.kotlinlang.org/t/a-js-backend-bug-workaround/507/1 "2014-08-19T19:49:02Z")

</div>

Any idea what might be causing this? I'll work on trying to narrow it down.

I have code like :

&nbsp;&nbsp;for (p in people) {  
 &nbsp;&nbsp;…  
 &nbsp;&nbsp;td {  
 &nbsp;&nbsp;a { &nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;href = “doSomething?id=${p.id}”  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;className = “red”  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;+p.name  
 &nbsp;&nbsp;}  
 &nbsp;&nbsp;}

Those functions `td` and `a` are builders, taking extension function/closures as their last argument.&nbsp;&nbsp;The generated js is doubled, as you see below, and it crashes.&nbsp;&nbsp;`p_0` is undefined.

&nbsp;&nbsp;f\_18: function (p) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;return function () {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;this.href = ‘doSomething?id=’ + p.id;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;this.className = ‘red’;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;_.mymod.plus(p.name);_  
 _&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;this.href = ‘doSomething?id=’ + p\_0.id;  
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;this.className = ‘red’;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;  
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;_.mymod.plus(p\_0.name);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;};  
 &nbsp;&nbsp;},

Rob

---

<div class="post-metadata">

**Author:** ![bashor](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/bashor/32/1431_2.png) [@bashor](https://discuss.kotlinlang.org/u/bashor)\
**Post date:** [August 19, 2014, 8:11pm UTC](https://discuss.kotlinlang.org/t/a-js-backend-bug-workaround/507/2 "2014-08-19T20:11:25Z")

</div>

Could you provide more informations? &nbsp;&nbsp;- your project or minimal code for reproduce; &nbsp;&nbsp;- compiler version.

It looks like bug, feel free to [create issue in our tracker](http://youtrack.jetbrains.com/newIssue?project=KT) with details.

Thanks!

---

<div class="post-metadata">

**Author:** ![bashor](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/bashor/32/1431_2.png) [@bashor](https://discuss.kotlinlang.org/u/bashor)\
**Post date:** [August 19, 2014, 8:18pm UTC](https://discuss.kotlinlang.org/t/a-js-backend-bug-workaround/507/3 "2014-08-19T20:18:40Z")

</div>

I could not reproduce it on [kotlin-demo](http://kotlin-demo.jetbrains.com/) with:

```kotlin
trait People {
  val id: Int
  val name: String
}

trait A {
  var href: String
  var className: String
  fun String.plus()
}
trait TD {
  fun a(f: A.() -> Unit)
}
fun td(f: TD.() -> Unit) {}
fun test(people: List<People>) {
  for (p in people) {
  td {
  a {   
           href = “doSomething?id=${p.id}”
           className = “red”
           +p.name
  }
  }
  }
}
 
```

---

<div class="post-metadata">

**Author:** ![RobNik](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@RobNik](https://discuss.kotlinlang.org/u/RobNik)\
**Post date:** [August 20, 2014, 12:49am UTC](https://discuss.kotlinlang.org/t/a-js-backend-bug-workaround/507/4 "2014-08-20T00:49:23Z")

</div>

I sent you some code via email. Didn't want to post here in case I accidentally include too much.

Rob

---

<div class="post-metadata">

**Author:** ![bashor](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/bashor/32/1431_2.png) [@bashor](https://discuss.kotlinlang.org/u/bashor)\
**Post date:** [August 20, 2014, 11:52am UTC](https://discuss.kotlinlang.org/t/a-js-backend-bug-workaround/507/5 "2014-08-20T11:52:56Z")

</div>

I can't reproduce problem with your project too.

Please tell us your Kotlin compiler version.  
And please check the code that I wrote above on your environment.

Thanks.

---

<div class="post-metadata">

**Author:** ![RobNik](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@RobNik](https://discuss.kotlinlang.org/u/RobNik)\
**Post date:** [August 20, 2014, 12:12pm UTC](https://discuss.kotlinlang.org/t/a-js-backend-bug-workaround/507/6 "2014-08-20T12:12:53Z")

</div>

I'm running IDEA 13 CE, Kotlin plugin 0.8.11.&nbsp;&nbsp;Your example code above compiles fine and does not produce the duplicate JS.

Rob

---

<div class="post-metadata">

**Author:** ![RobNik](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@RobNik](https://discuss.kotlinlang.org/u/RobNik)\
**Post date:** [August 20, 2014, 12:45pm UTC](https://discuss.kotlinlang.org/t/a-js-backend-bug-workaround/507/7 "2014-08-20T12:45:11Z")

</div>

There are some warnings about core/javautil.kt that always appear when I run the JS backend. I figured I could ignore them, but just in case there is something is wrong with my installation, here is the compiler output:

Information:Kotlin: Kotlin JPS plugin version 0.8.11  
Information:Kotlin: Using kotlinHome=/Users/rob/Library/Application Support/IdeaIC13/Kotlin/kotlinc  
Information:Kotlin: Invoking K2JSCompiler with arguments -tags -verbose -version -printArgs -output /Users/rob/Workspace/KotlinBugs/Bug1/out/production/Bug1/Bug1.js -libraryFiles /Users/rob/Workspace/KotlinBugs/Bug1/lib/kotlin-jslib.jar -sourceFiles /Users/rob/Workspace/KotlinBugs/Bug1/src/kotlinbug1/HtmlBuilders.kt,/Users/rob/Workspace/KotlinBugs/Bug1/src/kotlinbug1/Misc.kt,/Users/rob/Workspace/KotlinBugs/Bug1/src/kotlinbug1/ProjectList.kt,/Users/rob/Workspace/KotlinBugs/Bug1/src/kotlinbug1/Theirs.kt,/Users/rob/Workspace/KotlinBugs/Bug1/src/kotlinbug1/bugs/Bugs.kt  
Information:Kotlin: Kotlin Compiler version 0.8.11  
Information:Module “Bug1” was fully rebuilt due to project configuration/dependencies changes  
Information:Compilation completed successfully with 8 warnings in 9 sec  
Information:0 errors  
Information:8 warnings  
Warning:Kotlin: ‘E’ has a nullable upper bound. This means that a value of this type may be null. Using ‘E?’ is likely to mislead the reader (core/javautil.kt (70:25))  
Warning:Kotlin: ‘E’ has a nullable upper bound. This means that a value of this type may be null. Using ‘E?’ is likely to mislead the reader (core/javautil.kt (71:25))  
Warning:Kotlin: ‘V’ has a nullable upper bound. This means that a value of this type may be null. Using ‘V?’ is likely to mislead the reader (core/javautil.kt (97:42))  
Warning:Kotlin: Projection is redundant: the corresponding type parameter of Map has the same variance (core/javautil.kt (100:46))  
Warning:Kotlin: ‘V’ has a nullable upper bound. This means that a value of this type may be null. Using ‘V?’ is likely to mislead the reader (core/javautil.kt (101:45))  
/Users/rob/Workspace/KotlinBugs/Bug1/src/kotlinbug1/ProjectList.kt  
Warning:(22, 17) Kotlin: Variable ‘row’ is never used  
Warning:(41, 9) Kotlin: Variable ‘pl’ is never used  
/Users/rob/Workspace/KotlinBugs/Bug1/src/kotlinbug1/Theirs.kt  
Warning:(22, 8) Kotlin: Parameter ‘f’ is never used

---

<div class="post-metadata">

**Author:** ![bashor](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/bashor/32/1431_2.png) [@bashor](https://discuss.kotlinlang.org/u/bashor)\
**Post date:** [August 20, 2014, 12:49pm UTC](https://discuss.kotlinlang.org/t/a-js-backend-bug-workaround/507/8 "2014-08-20T12:49:33Z")

</div>

You are right it's not your problem, it's problem in stdlib.

---

<div class="post-metadata">

**Author:** ![bashor](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/bashor/32/1431_2.png) [@bashor](https://discuss.kotlinlang.org/u/bashor)\
**Post date:** [August 20, 2014, 7:54pm UTC](https://discuss.kotlinlang.org/t/a-js-backend-bug-workaround/507/9 "2014-08-20T19:54:08Z")

</div>

I simplified your code to:

```kotlin
...

fun test(flag: Boolean) {
  buildTr {
  td {
           a {
           global += “1”
           }
  }
  td {
           a {
           href = “something” + if(flag) “a” else “b”
// workaround:
//           val tmp = if(flag) “a” else “b” 
//           href = “something” + tmp
           }
  }
  }
}
```

This and your code works fine on current master. So You can apply workaround or update your compiler.

Thanks for your report!

---

<div class="post-metadata">

**Author:** ![RobNik](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@RobNik](https://discuss.kotlinlang.org/u/RobNik)\
**Post date:** [August 20, 2014, 7:57pm UTC](https://discuss.kotlinlang.org/t/a-js-backend-bug-workaround/507/10 "2014-08-20T19:57:31Z")

</div>

Thank you.&nbsp;&nbsp;I'm curious, so you were able to reproduce the bug in the older version?

Rob

---

<div class="post-metadata">

**Author:** ![bashor](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/bashor/32/1431_2.png) [@bashor](https://discuss.kotlinlang.org/u/bashor)\
**Post date:** [August 20, 2014, 8:06pm UTC](https://discuss.kotlinlang.org/t/a-js-backend-bug-workaround/507/11 "2014-08-20T20:06:59Z")

</div>

Yes,I reproduced it on IDEA 13 with Kotlin M8.
