spockframework / spockframework/spock
Regression from 1.3: compilation fails with “current scope already contains a variable of the name”
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 3.6k
- Forks
- 483
- PR merge metrics
- No merged PRs in 30d
Description
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'
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the reproducer in MySpec.groovy and its build.gradle dependencies, comparing Spock 1.3 with the reported 2.x versions under Groovy 2.5.14 or 3.0.8. Investigate compilation when closure variables coexist with cleanup, thrown, or notThrown sections. Done means the specification compiles successfully without the duplicate-variable errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- groovy, java
- Domain
- compilers, testing
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100