Tobias Peitzsh help us identifying the root of the issue:
In method
@Override protected Boolean run() throws Exception {
return getContext().get(FilePath.class).child(file).exists();
}
child method is called. Child method creates a new FilePath object. While creating the object, method resolvePathIfRelative is called. This method replaces the relative path with "base.remote+'\\'+rel.replace('/','
')". So "" is interpreted as "." (current directory).
--> If the current directory exists, fileExists returns true otherwise false.
Please see the following script:
node('GF_SI-Z0TTN_SWBUILD'){
step([$class: 'WsCleanup'])
echo fileExists("").toString() // current workspace does not exist
bat "echo test>test.txt"
echo fileExists("").toString() // current workspace exists
}
Output is
false
true
That is the issue why I got true / false on different machines. The script was called on different machines before. On these machines I got true for the others I got false.
The Java behavior of new File("").exists() is as expected:
System.out.println(new File("").exists()); // --> always false
System.out.println(new File(".").exists()); // --> always true
So at least a hint for fileExists() is needed to note the unexpected behavior.
My use case is: A string parameter specifies a file to be used during the build (e.g. special parameters or test to be executed). If the file exists the action shall be done. If the file does not exists the action shall not be done. So I wrote (simplified)
if (fileExists(env.FileParameter)){
// do special action
}
else{
// do nothing
}
So if FileParameter is empty / "" the action is executed although the file does not exist.
Using if (env.FileParameter.trim() !="" && fileExists(env.FileParameter)) is not obvious.
Tobias Peitzsh help us identifying the root of the issue:
In method
@Override protected Boolean run() throws Exception {
return getContext().get(FilePath.class).child(file).exists();
}
child method is called. Child method creates a new FilePath object. While creating the object, method resolvePathIfRelative is called. This method replaces the relative path with "base.remote+'\\'+rel.replace('/','
')". So "" is interpreted as "." (current directory).
--> If the current directory exists, fileExists returns true otherwise false.
Please see the following script:
node('GF_SI-Z0TTN_SWBUILD'){
step([$class: 'WsCleanup'])
echo fileExists("").toString() // current workspace does not exist
bat "echo test>test.txt"
echo fileExists("").toString() // current workspace exists
}
Output is
false
true
That is the issue why I got true / false on different machines. The script was called on different machines before. On these machines I got true for the others I got false.
The Java behavior of new File("").exists() is as expected:
System.out.println(new File("").exists()); // --> always false
System.out.println(new File(".").exists()); // --> always true
So at least a hint for fileExists() is needed to note the unexpected behavior.
My use case is: A string parameter specifies a file to be used during the build (e.g. special parameters or test to be executed). If the file exists the action shall be done. If the file does not exists the action shall not be done. So I wrote (simplified)
if (fileExists(env.FileParameter)){
// do special action
}
else{
// do nothing
}
So if FileParameter is empty / "" the action is executed although the file does not exist.
Using if (env.FileParameter.trim() !="" && fileExists(env.FileParameter)) is not obvious.