Keywords: .NET 6 | Publish Error | Duplicate File Detection
Abstract: This article provides an in-depth analysis of the common NETSDK1152 publish error encountered during .NET 6 migration, which stems from the newly introduced duplicate file detection mechanism. It examines the root causes of the error and presents two practical solutions: bypassing the check via the ErrorOnDuplicatePublishOutputFiles property, or excluding conflicting files through project file modifications. Each approach includes complete code examples and configuration instructions to help developers quickly resolve real-world issues.
During the .NET 6 publishing process, many developers encounter a specific error: NETSDK1152: Found multiple publish output files with the same relative path. This error typically appears in projects migrated from earlier .NET versions, particularly in large solutions containing multiple web projects or theme packages.
Error Cause Analysis
.NET 6 introduces a significant security enhancement: during publishing, the SDK now detects and prevents multiple files with the same relative path from being copied to the target directory. This change aims to prevent file overwrites and data loss, but also brings compatibility challenges. In previous versions, if multiple projects contained files with identical names, the last copied file would overwrite previous ones, potentially leading to unpredictable behavior.
The file paths listed in the error message clearly reveal the nature of the problem:
D:\Github\volo\abp\lepton-theme\src\Volo.Abp.AspNetCore.Mvc.UI.Theme.Lepton\compilerconfig.json,
D:\Github\volo\abp\bookstore\src\Acme.BookStore.Theme\compilerconfig.json
These two compilerconfig.json files share the same filename, triggering the conflict detection mechanism when copied to the publish directory.
Solution One: Disable Duplicate File Checking
For scenarios requiring quick resolution while maintaining backward compatibility, you can disable this check by modifying project files. This approach is particularly suitable for legacy projects that rely on previous file copying behavior.
Add the following property group to all publishable web project .csproj files:
<PropertyGroup>
<ErrorOnDuplicatePublishOutputFiles>false</ErrorOnDuplicatePublishOutputFiles>
</PropertyGroup>
This setting instructs the .NET SDK not to check for duplicate files during publishing, reverting to .NET 5 and earlier behavior. It's important to note that while this method is simple, it may mask actual file conflict issues within the project.
Solution Two: Exclude Conflicting Files
A more standardized solution involves excluding the files causing conflicts from the publishing process. This approach not only resolves the immediate issue but also helps identify unnecessary file dependencies in the project.
First, add the following configuration to the common.props file in the solution root directory:
<Content Remove="compilerconfig.json;package.json"/>
<None Include="compilerconfig.json;package.json">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
This configuration accomplishes three things:
- Removes specified files from Content items
- Redefines these files as None items
- Prevents these files from being copied to the publish directory
If the project doesn't have a common.props file, you can add the same configuration directly to each project's .csproj file. The advantage of this method is that it explicitly handles file conflicts rather than simply ignoring them.
Implementation Recommendations
When choosing a solution, consider the specific context of your project:
- For short-term fixes or testing environments, Solution One may be more appropriate
- For production environments or long-term maintenance projects, Solution Two is recommended
- Before excluding files, ensure they are genuinely unnecessary at runtime
- Consider whether project structure refactoring could avoid file conflicts altogether
The emergence of this issue reminds us to carefully examine file dependencies during project migration. While .NET 6's new feature enhances publishing security, it also requires developers to pay closer attention to project file organization.