Azure devops pipeline CopyFiles@2 stopped working and python function app cannot find module

Lauren Lewis 0 Reputation points
2025-05-21T13:53:09.8666667+00:00

yesterday the task CopyFile@2 started failing with the following error

this is the yaml: # - task: CopyFiles@2 # inputs: # SourceFolder: $(Agent.BuildDirectory)/InvestmentPaperAPI/InvestmentPaperAPI # OverWrite: true # Contents: '**' # TargetFolder: $(Build.SourcesDirectory)/Merged

Error:

##[error]Error: Failed cp: Error: ENOTDIR: not a directory, scandir '/opt/hostedtoolcache/Python/3.10.17/x64/bin/python'
    at Object.readdirSync (node:fs:1506:26)
    at copyDirectoryWithResolvedSymlinks (/home/vsts/work/_tasks/CopyFiles_5bfb729a-a7c8-4a78-a7c3-8d717bb7c13c/2.256.0/node_modules/azure-pipelines-task-lib/task.js:1057:22)
    at retry.retryCount (/home/vsts/work/_tasks/CopyFiles_5bfb729a-a7c8-4a78-a7c3-8d717bb7c13c/2.256.0/node_modules/azure-pipelines-task-lib/task.js:1044:17)
    at retry (/home/vsts/work/_tasks/CopyFiles_5bfb729a-a7c8-4a78-a7c3-8d717bb7c13c/2.256.0/node_modules/azure-pipelines-task-lib/task.js:1148:25)
    at Object.cp (/home/vsts/work/_tasks/CopyFiles_5bfb729a-a7c8-4a78-a7c3-8d717bb7c13c/2.256.0/node_modules/azure-pipelines-task-lib/task.js:999:5)
    at /home/vsts/work/_tasks/CopyFiles_5bfb729a-a7c8-4a78-a7c3-8d717bb7c13c/2.256.0/copyfiles.js:209:65
    at RetryHelper.<anonymous> (/home/vsts/work/_tasks/CopyFiles_5bfb729a-a7c8-4a78-a7c3-8d717bb7c13c/2.256.0/retrylogichelper.js:22:42)
    at Generator.next (<anonymous>)
    at /home/vsts/work/_tasks/CopyFiles_5bfb729a-a7c8-4a78-a7c3-8d717bb7c13c/2.256.0/retrylogichelper.js:8:71
    at new Promise (<anonymous>) {
  errno: -20,
  code: 'ENOTDIR',
  syscall: 'scandir',
  path: '/opt/hostedtoolcache/Python/3.10.17/x64/bin/python'
}
##[debug]Processed: ##vso[task.issue type=error;source=TaskInternal;correlationId=0804e5ac-93db-43e7-8b5c-f309ca024648;]Error: Failed cp: Error: ENOTDIR: not a directory, scandir '/opt/hostedtoolcache/Python/3.10.17/x64/bin/python'
    at Object.readdirSync (node:fs:1506:26)
    at copyDirectoryWithResolvedSymlinks (/home/vsts/work/_tasks/CopyFiles_5bfb729a-a7c8-4a78-a7c3-8d717bb7c13c/2.256.0/node_modules/azure-pipelines-task-lib/task.js:1057:22)
    at retry.retryCount (/home/vsts/work/_tasks/CopyFiles_5bfb729a-a7c8-4a78-a7c3-8d717bb7c13c/2.256.0/node_modules/azure-pipelines-task-lib/task.js:1044:17)
    at retry (/home/vsts/work/_tasks/CopyFiles_5bfb729a-a7c8-4a78-a7c3-8d717bb7c13c/2.256.0/node_modules/azure-pipelines-task-lib/task.js:1148:25)
    at Object.cp (/home/vsts/work/_tasks/CopyFiles_5bfb729a-a7c8-4a78-a7c3-8d717bb7c13c/2.256.0/node_modules/azure-pipelines-task-lib/task.js:999:5)
    at /home/vsts/work/_tasks/CopyFiles_5bfb729a-a7c8-4a78-a7c3-8d717bb7c13c/2.256.0/copyfiles.js:209:65
    at RetryHelper.<anonymous> (/home/vsts/work/_tasks/CopyFiles_5bfb729a-a7c8-4a78-a7c3-8d717bb7c13c/2.256.0/retrylogichelper.js:22:42)
    at Generator.next (<anonymous>)
    at /home/vsts/work/_tasks/CopyFiles_5bfb729a-a7c8-4a78-a7c3-8d717bb7c13c/2.256.0/retrylogichelper.js:8:71
    at new Promise (<anonymous>) {
  errno: -20,
  code: 'ENOTDIR',
  syscall: 'scandir',
  path: '/opt/hostedtoolcache/Python/3.10.17/x64/bin/python'
}

I changed the copy to use bash:

    - script: |
        cp -a \
          '$(Agent.BuildDirectory)/EnhancementPaperAPI/EnhancementPaperAPI' \
          '$(Build.SourcesDirectory)/Merged'
        cp -af \
          '$(Agent.BuildDirectory)/InvestmentPaperAPI/InvestmentPaperAPI' \
          '$(Build.SourcesDirectory)/Merged'
        ls -la '$(Build.SourcesDirectory)/Merged'
      displayName: 'CopyFiles'

which allowed the build to pass - but now the function app is failing because it cnanot find the app module.

WHat's causing the build failure and how do I fix it?

Azure DevOps
{count} votes

2 answers

Sort by: Most helpful
  1. Rolandas Rimkus 5 Reputation points
    2025-05-27T10:21:28.4566667+00:00

    Until this is resolved our workaround is to be specific with the version of the task that we consume. See: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/tasks?view=azure-devops&tabs=yaml#task-versions

    So until this is resolved all the tasks that fail we will be updating to use:

    task: CopyFiles@2.246.0
    

    instead of

    task: CopyFiles@2
    

    as that seems to be what our last successful builds were using since we spotted this.

    With this solution it will also be much easier to test new releases without rewriting the yaml files later. This sadly doesn't work as well for Classic pipelines as through the UI you can only select 1.* or 2.* versions. For those more significant hacks through rest api would need to be used to force a specific task revision to be used.

    Hope this helps

    1 person found this answer helpful.

  2. Siva Pavuluri 490 Reputation points Microsoft External Staff Moderator
    2025-05-22T10:53:19.7566667+00:00

    Hi Lauren Lewis,

    The error you're encountering with the CopyFiles@2 task in your Azure DevOps pipeline is caused by the task mistakenly treating a file as a directory. Specifically, it attempts to scan the following path:

    ##[debug]Processed: ##vso[task.issue type=error;source=TaskInternal;correlationId=0804e5ac-93db-43e7-8b5c-f309ca024648;]Error: Failed cp: Error: ENOTDIR: not a directory, scandir '/opt/hostedtoolcache/Python/3.10.17/x64/bin/python'
    

    This path points to the Python binary, which is a file (or possibly a symlink), not a directory. The CopyFiles@2 task, when performing a recursive copy, is incorrectly including this path and attempting to scan it as if it were a directory. This results in the ENOTDIR (Error: Not a Directory) exception during execution.
    Update your YAML to exclude problematic paths

    • task: CopyFiles@2

      inputs:

        SourceFolder: '$(Agent.BuildDirectory)/InvestmentPaperAPI/InvestmentPaperAPI'

        Contents: '**/*'

        TargetFolder: '$(Build.SourcesDirectory)/Merged'

        OverWrite: true

    If needed, explicitly exclude the Python path:

    • task: CopyFiles@2

      inputs:

        SourceFolder: '$(Agent.BuildDirectory)/InvestmentPaperAPI/InvestmentPaperAPI'

        Contents: |

          */

          !**/python

        TargetFolder: '$(Build.SourcesDirectory)/Merged'

        OverWrite: true

    Please refer the below document:
    https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/reference/copy-files-v2?view=azure-pipelines&tabs=yaml

    If you have any further questions or need additional assistance, please feel free to let me know.

    Thank you.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.