How to prevent long files in TFS

TFS file path too long

While focusing on the development at hand, it might be hard to keep in mind the resulting path of the files we’re adding.

Besides being annoying whenever using the command line and when trying to copy paste a file path, long file paths can cause issues with build agents.

A solution I found is use the TFS API during a PR to get files with long file paths to get merged.

GetPrDiffsAndCheckForExceedinglyLongFilePaths.ps1

Here’s a script explanation bit by bit:

The required input parameters are validated and checked for empty.

if([string]::IsNullOrEmpty($projectId)){
throw "The parameter $projectId is required";
}

The source and target branches can be determined in a number of ways, depending on the input parameters:

  • directly as input parameters (when the script is called directly)
  • from the TFS PR message (when triggered from within a build)
  • from a call to the TFS Pull requests API (as sometimes the build variable BUILD_SOURCEVERSIONMESSAGE is not populated due to a TFS bug)
$tfsUrlBit="https://mtTfsUrl/tfs/myCollection/"
##E.g. projectId
#daad9574-2d95-4363-a5e1-886ae9549690

The script than calls the TFS API asking for the delta differences between the source and target branches:

Write-Host "Using $($prTargetBranch) as the target branch and $($prSourceBranch) as the source branch"

With the resulting changes, a simple iteration over the result would allow to check the actual path for each non deleted files

$filesWithFilePathTooLong = @();

TFS does group the file changes done to a file within the changeType element, so checking if the resulting string does not contain delete, will allow us to not include those files in the check (after all, if a file is removed its length constraints are too).

In order to call the scripts, you either pass in the branches name directly, as in:

.\GetPrDiffsAndCheckForExceedinglyLongFilePaths.ps1 -projectId "daad9574-2d95-4363-a5e1-886ae9549690" -repositoryId "060b25a7-9d74-4897-9fdc-ac29ba8f3b8b" -prSourceBranch "myBranch/mpi_installation_guide_jobs" -prTargetBranch "dev_myBranch" -acceptedPathLength 160

Or, pass in the PR message (that is generated by TFS during a PR in the BUILD_SOURCEVERSIONMESSAGE variable), as in

.\GetPrDiffsAndCheckForExceedinglyLongFilePaths.ps1 -projectId "daad9574-2d95-4363-a5e1-886ae9549690" -repositoryId "060b25a7-9d74-4897-9fdc-ac29ba8f3b8b" -BUILD_SOURCEVERSIONMESSAGE  "Merge pull request 909 from myBranch/mpi_installation_guide_jobs into dev_myBranch" -acceptedPathLength 160

Just make sure you use one of them, and you’ll be good to go.

You’ll need to define the following variables as part of the build:

  • ProjectId
  • RepositoryId
  • -prTargetBranch (allowed at queue time to allow the same level of enforcing if the build is queued manually)
  • AcceptedPathLength

You can then call it from a TFS powershell step with the following arguments:

-projectId $(ProjectId) -repositoryId $(RepositoryId) -prSourceBranch $(Build.SourceBranch) -prTargetBranch $(DefaultPRTargetBranch) -BUILD_SOURCEVERSIONMESSAGE $(Build.SourceVersionMessage) -acceptedPathLength $(AcceptedPathLength)