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

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

    XMLWordPrintable

Details

    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

            jraezrus Javier Raez created issue -
            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.

            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.
            jamesdumay James Dumay made changes -
            Field Original Value New Value
            Remote Link This issue links to "CloudBees internal CD-351 (Web Link)" [ 18310 ]
            jamesdumay James Dumay made changes -
            Labels cloudbees-internal-pipeline
            jamesdumay James Dumay made changes -
            Sprint Pipeline - Candidates [ 381 ]
            abayer Andrew Bayer made changes -
            Assignee Andrew Bayer [ abayer ]
            abayer Andrew Bayer made changes -
            Status Open [ 1 ] In Progress [ 3 ]
            abayer Andrew Bayer made changes -
            Status In Progress [ 3 ] In Review [ 10005 ]
            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.

            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.
            abayer Andrew Bayer made changes -
            Remote Link This issue links to "PR #56 (Web Link)" [ 19766 ]

            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

            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
            abayer Andrew Bayer added a comment -

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

            abayer Andrew Bayer added a comment - That warning will be in the next release of workflow-basic-steps (2.7, I believe).
            abayer Andrew Bayer made changes -
            Resolution Fixed [ 1 ]
            Status In Review [ 10005 ] Resolved [ 5 ]
            vivek Vivek Pandey made changes -
            Sprint Pipeline - Candidates [ 381 ]
            vivek Vivek Pandey made changes -
            Rank Ranked higher

            People

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

              Dates

                Created:
                Updated:
                Resolved: