Regression from 1.3: compilation fails with “current scope already contains a variable of the name”
Describe the bug
Compiling a simple Spock specification which works with Spock 1.3 fails with 2.0 and 2.1-SNAPSHOT.
To reproduce
The following specification can be successfully compiled with Spock 1.3 but fails to compile with newer Spock versions (tested with both Groovy 2.5.14 and Groovy 3.0.8):
import spock.lang.Specification
class MySpec extends Specification {
def foo1() {
expect:
'whatever'.with { foo -> !foo.isEmpty() }
def foo = null
!foo
cleanup:
'irrelevant'.toString()
}
def foo2() {
when:
'whatever'.with { foo -> !foo.isEmpty() }
def foo = null
then:
notThrown(Exception)
}
}
The issue doesn’t occur if there is no notThrown(…), no thrown(…) or no cleanup section.
Expected behavior
The specification can be compiled sucessfully.
Actual behavior
The compiler throws the following errors (for Spock 2.+):
startup failed:
/tmp/foo/src/test/groovy/MySpec.groovy: 7: The current scope already contains a variable of the name foo
@ line 7, column 25.
'whatever'.with { foo -> !foo.isEmpty() }
^
/tmp/foo/src/test/groovy/MySpec.groovy: 17: The current scope already contains a variable of the name foo
@ line 17, column 25.
'whatever'.with { foo -> !foo.isEmpty() }
^
Environment:
Java/JDK
- openjdk 11.0.11
- Groovy 2.5.14 or Groovy 3.0.8
Build tool version
Gradle 7.1.1
Operating System
Ubuntu 20.04.2 LTS
Build-tool dependencies used
Complete build.gradle (tried with different versions as indicated above):
plugins {
id 'groovy'
}
repositories {
mavenCentral()
maven {
url = 'https://oss.sonatype.org/content/repositories/snapshots'
}
}
dependencies {
testImplementation 'org.codehaus.groovy:groovy:2.5.14'
testImplementation 'org.spockframework:spock-core:2.1-groovy-2.5-SNAPSHOT'
}
This is the same root cause as #1266 see my comment but this will be trickier to fix. From the user perspective it looks like a regression, but it actually just got more strict with its validations.
I'll have to look at it, for now I would suggest to simply use different names.