When working with Cordova applications in Visual Studio 2015, developers often encounter a frustrating issue related to file permissions. This problem typically manifests when trying to build a Cordova project that’s under source control, particularly with Team Foundation Server (TFS). Understanding and resolving this issue is crucial for maintaining a smooth development workflow.

Understanding the Problem

The core issue stems from file permissions in the platforms directory of your Cordova project. Even though this directory is typically excluded from source control (as it should be), the files within it become marked as read-only. This creates a conflict when the build process attempts to modify these files, resulting in the “EPERM, operation not permitted” error.

This situation commonly occurs in the following scenario:

  1. You create a new Cordova application in Visual Studio 2015
  2. The project is added to TFS source control
  3. The platforms folder is correctly excluded from source control
  4. After checking out the project and making changes, the build fails
  5. The error occurs in clean.bat within the platforms folder

Root Cause Analysis

The read-only attribute is being applied to the platforms directory and its contents, even though these files aren’t under source control. This is a known behavior in Visual Studio 2015’s Cordova tools, where the build process expects to have full write access to these files. When the permissions are restricted, the build process cannot modify the necessary files, leading to the EPERM error.

Solution Implementation

To resolve this issue, we need to ensure that the read-only attribute is removed from the platforms directory before the build process begins. This can be achieved by modifying the project file to include a pre-build step.

Step 1: Modify Project File

First, you’ll need to edit the project file:

  1. Right-click the project in Solution Explorer
  2. Select “Unload Project”
  3. Right-click the unloaded project and choose “Edit Project File”

Step 2: Add Build Configuration

Add the following configuration to your project file:

<Project ToolsVersion="14.0" DefaultTargets="BeforeBuild;Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

Step 3: Implement Pre-Build Target

Add the following target section at the bottom of your project file:

<Target Name="BeforeBuild">
  <Exec Command="attrib -R &quot;$(SolutionDir)YourProjectName\platforms\*.*&quot; /S" IgnoreExitCode="true" />
</Target>

This configuration will:

  • Run before the build process starts
  • Remove the read-only attribute from all files in the platforms directory
  • Apply the changes recursively to all subdirectories
  • Continue even if the command encounters any issues

Best Practices

To prevent similar issues in the future:

  1. Always exclude the platforms directory from source control
  2. Consider adding the platforms directory to your .gitignore or .tfignore file
  3. Document the read-only attribute issue in your team’s development guidelines
  4. Implement the solution as a standard part of your Cordova project setup

Additional Considerations

While this solution effectively resolves the immediate issue, it’s worth noting that:

  • The solution needs to be implemented for each Cordova project
  • The command will run before every build
  • The IgnoreExitCode attribute ensures the build continues even if the command fails
  • You may need to adjust the path in the command based on your project structure

Conclusion

The EPERM error in Visual Studio 2015 Cordova projects can be frustrating, but it’s a solvable issue. By implementing the pre-build step to remove read-only attributes, you can ensure a smooth development experience. This solution maintains the integrity of your source control while allowing the build process to function correctly.

Note: Always test the solution in your specific development environment, as project structures and paths may vary. Consider adding this configuration to your project templates to prevent the issue from occurring in future projects.