Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-48138

filexist('') return an incorrect result in windows machines

    XMLWordPrintable

    Details

    • Similar Issues:

      Description

      If you execute this code in a windows agent or master:

       

      node('master'){

         def test = fileExists ''

         echo test.toString()

      }

       

      The result is true while in linux machines the result is false.

      The result should be always false.

        Attachments

          Issue Links

            Activity

            Hide
            jraezrus Javier Raez added a comment -

            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.

            Show
            jraezrus Javier Raez added a comment - 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.
            Hide
            abayer Andrew Bayer added a comment -

            PR up at https://github.com/jenkinsci/workflow-basic-steps-plugin/pull/56 that adds a log message when fileExists is called with an empty string informing the user that the current directory is checked instead.

            Show
            abayer Andrew Bayer added a comment - PR up at https://github.com/jenkinsci/workflow-basic-steps-plugin/pull/56 that adds a log message when fileExists is called with an empty string informing the user that the current directory is checked instead.
            Hide
            scm_issue_link SCM/JIRA link daemon added a comment -

            Code changed in jenkins
            User: Andrew Bayer
            Path:
            pom.xml
            src/main/java/org/jenkinsci/plugins/workflow/steps/FileExistsStep.java
            src/main/resources/org/jenkinsci/plugins/workflow/steps/Messages.properties
            src/test/java/org/jenkinsci/plugins/workflow/steps/CoreWrapperStepTest.java
            src/test/java/org/jenkinsci/plugins/workflow/steps/FileExistsStepTest.java
            http://jenkins-ci.org/commit/workflow-basic-steps-plugin/35c716699dc87bf0a1188e91c9c7fa9c61603ea2
            Log:
            [FIXED JENKINS-48138] Log a warning when fileExists called with empty string

            Show
            scm_issue_link SCM/JIRA link daemon added a comment - Code changed in jenkins User: Andrew Bayer Path: pom.xml src/main/java/org/jenkinsci/plugins/workflow/steps/FileExistsStep.java src/main/resources/org/jenkinsci/plugins/workflow/steps/Messages.properties src/test/java/org/jenkinsci/plugins/workflow/steps/CoreWrapperStepTest.java src/test/java/org/jenkinsci/plugins/workflow/steps/FileExistsStepTest.java http://jenkins-ci.org/commit/workflow-basic-steps-plugin/35c716699dc87bf0a1188e91c9c7fa9c61603ea2 Log: [FIXED JENKINS-48138] Log a warning when fileExists called with empty string
            Hide
            abayer Andrew Bayer added a comment -

            That warning will be in the next release of workflow-basic-steps (2.7, I believe).

            Show
            abayer Andrew Bayer added a comment - That warning will be in the next release of workflow-basic-steps (2.7, I believe).

              People

              • Assignee:
                abayer Andrew Bayer
                Reporter:
                jraezrus Javier Raez
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: