jenkinsci / jenkinsci/workflow-cps-plugin
[JENKINS-47014] Groovy Pipeline Use an assignment within a "while" condition error
- Dominant language
- Java
- Stars
- 186
- Forks
- 213
- Avg merge
- 12h 12m
- Merged PRs (30d)
- 8
Description
I use @Grab annotation to load Java jar from maven center. Then I wrote java language in my groovy file. I have some code pieces like the following:
while ((read = fInputStream.read(buffer)) != -1) {
fOptStream.write(buffer, 0, read);
}
I read a file and then save to another location, if the file ends, then return -1.
This code works fine on my local groovy 2.4.12. but when I execute the groovy file in Jenkins. I got endless loop in while. I printed the log and found the read value is correct(-1), but it won't break the while loop. It seems that the return value is the result of the assignment. So I modify the code as follows:
read = fInputStream.read(buffer)
while (read != -1) {
fOptStream.write(buffer, 0, read);
read = fInputStream.read(buffer)
}
It can work normally.
I guess there might be something wrong in Jenkins embedded groovy engine.
It doesn't support an assignment within a while condition.
---
Originally reported by jackyim, imported from: Groovy Pipeline Use an assignment within a "while" condition error
Raw content of original issue
I use @Grab annotation to load Java jar from maven center. Then I wrote java language in my groovy file. I have some code pieces like the following:
while ((read = fInputStream.read(buffer)) != -1) {
fOptStream.write(buffer, 0, read);
}I read a file and then save to another location, if the file ends, then return -1.
This code works fine on my local groovy 2.4.12. but when I execute the groovy file in Jenkins. I got endless loop in while. I printed the log and found the read value is correct(-1), but it won't break the while loop. It seems that the return value is the result of the assignment. So I modify the code as follows:
read = fInputStream.read(buffer)
while (read != -1) {
fOptStream.write(buffer, 0, read);
read = fInputStream.read(buffer)
}It can work normally.
I guess there might be something wrong in Jenkins embedded groovy engine.
It doesn't support an assignment within a while condition.
environment
```
Jenkins version: 2.73.1
Pipeline: Shared Groovy Libraries 2.9
```
Contributor guide
Research direction
Start by reproducing the reported assignment-in-a-while-condition example against workflow-cps-plugin using the stated Jenkins 2.73.1 and Pipeline Shared Groovy Libraries 2.9 environment. Compare its behavior with local Groovy 2.4.12 and verify that end-of-file causes the loop to terminate; the issue does not name a source file or test to update.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- groovy, java
- Domain
- ci-cd
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100