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

File.eachFileRecurse(FileType.FILES) and File.eachDirRecurse() only loops for first element

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved (View Workflow)
    • Priority: Major
    • Resolution: Duplicate
    • Component/s: workflow-cps-plugin
    • Labels:
      None
    • Environment:
      Jenkins ver. 2.77
      Pipeline: Groovy ver. 2.39
      Windows Server 2012 R2
      Java: Java(TM) SE Runtime Environment, 1.8.0_66-b18
      JVM: Java HotSpot(TM) Client VM, 25.66-b18, mixed mode
    • Similar Issues:

      Description

      Problem

      Both methods File.eachFileRecurse(FileType.FILES) and File.eachDirRecurse() only call the closure with the first element.

      Example pipeline script

      (I run this without groovy sandbox)

      import groovy.io.FileType
      
      node {
      	stage('test') {
      		def tmpDirPath = "C:\\temp\\test"
      		def tmpDir = new File(tmpDirPath)
      		
      		// Create test folders
      		new File(tmpDirPath+"\\dir1\\dir11\\dir111").mkdirs()
      		new File(tmpDirPath+"\\dir1\\dir12").mkdirs()
      		new File(tmpDirPath+"\\dir2\\dir21").mkdirs()
      		new File(tmpDirPath+"\\dir2\\dir22").mkdirs()
      		new File(tmpDirPath+"\\dir3").mkdirs()
      		
      		// Create test files
      		new File(tmpDirPath+"\\dir1\\dir11\\dir111\\file111") << "dummy"
      		new File(tmpDirPath+"\\dir1\\dir11\\file11") << "dummy"
      		new File(tmpDirPath+"\\dir1\\dir12\\file12") << "dummy"
      		new File(tmpDirPath+"\\dir2\\dir21\\file21") << "dummy"
      		new File(tmpDirPath+"\\dir2\\dir22\\file22") << "dummy"
      		new File(tmpDirPath+"\\dir3\\file3") << "dummy"
      		
      		// Output what eachDirRecurse does
      		tmpDir.eachDirRecurse() { dir ->
      			echo "Dir: ${dir}"
      		}
      		
      		// Output what eachFileRecurse does
      		tmpDir.eachFileRecurse(FileType.FILES) { file ->
      			echo "File: ${file}"
      		}
      	}
      }
      

      Output

      [Pipeline] node
      Running on master in C:\Program Files (x86)\Jenkins\workspace\Support\test
      [Pipeline] {
      [Pipeline] stage
      [Pipeline] { (test)
      [Pipeline] echo
      Dir: C:\temp\test\dir1
      [Pipeline] echo
      File: C:\temp\test\dir1\dir11\dir111\file111
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] }
      [Pipeline] // node
      [Pipeline] End of Pipeline
      Finished: SUCCESS
      

        Attachments

          Issue Links

            Activity

            Hide
            swsbb Benjamin Buchfink added a comment - - edited

            Here are two workarounds for both methods on Windows:

            eachFileRecurse

            def ret=bat(script: 'dir /s /b /a:-D "'+tmpDir.absolutePath+'" 2>nul')
            ret.trim().tokenize('\r\n').each {
            	echo "File: "+it
            }
            

            eachDirRecurse

            def ret=bat(script: 'dir /s /b /a:D "'+tmpDir.absolutePath+'" 2>nul')
            ret.trim().tokenize('\r\n').each {
            	echo "File: "+it
            }
            
            Show
            swsbb Benjamin Buchfink added a comment - - edited Here are two workarounds for both methods on Windows: eachFileRecurse def ret=bat(script: 'dir /s /b /a:-D "'+tmpDir.absolutePath+'" 2>nul') ret.trim().tokenize('\r\n').each { echo "File: "+it } eachDirRecurse def ret=bat(script: 'dir /s /b /a:D "'+tmpDir.absolutePath+'" 2>nul') ret.trim().tokenize('\r\n').each { echo "File: "+it }
            Hide
            jglick Jesse Glick added a comment -

            Wrong plugin.

            Could presumably be fixed in groovy-cps by adding support for ResourceGroovyMethods to the translator. But you do not want to be doing this anyway. Pipeline scripts must not access java.io.File methods. Read the docs.

            Show
            jglick Jesse Glick added a comment - Wrong plugin. Could presumably be fixed in groovy-cps by adding support for ResourceGroovyMethods to the translator. But you do not want to be doing this anyway. Pipeline scripts must not access java.io.File methods. Read the docs.
            Hide
            jglick Jesse Glick added a comment -

            (Those “workarounds” are in fact the only way to do it correctly.)

            Show
            jglick Jesse Glick added a comment - (Those “workarounds” are in fact the only way to do it correctly.)

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              swsbb Benjamin Buchfink
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: