NETSDK1152: Multiple publish output files with same relative path
This error occurs when publishing .NET applications that contain multiple projects with files sharing the same relative path in the output directory. The .NET 6+ SDK introduced stricter validation to prevent file conflicts during publishing.
Problem Overview
The NETSDK1152
error appears when your solution contains multiple projects that would generate files with identical relative paths in the publish output. Common conflicting files include:
compilerconfig.json
package.json
appsettings.json
libman.json
packages.lock.json
Example error message:
error NETSDK1152: Found multiple publish output files with the same relative path:
D:\project1\compilerconfig.json,
D:\project2\compilerconfig.json
Root Cause
This error was introduced as a breaking change in .NET 6 SDK to prevent accidental file overwrites during publish operations. The SDK now explicitly checks for and blocks files that would conflict in the output directory.
Solutions
1. Disable duplicate file checking (Temporary workaround)
Add this property to your publishable project's .csproj
file:
<PropertyGroup>
<ErrorOnDuplicatePublishOutputFiles>false</ErrorOnDuplicatePublishOutputFiles>
</PropertyGroup>
WARNING
This approach reinstates the pre-.NET 6 behavior and may lead to unintended file overwrites. Use cautiously.
2. Exclude conflicting files from publish
For files that aren't needed at runtime (like build configuration files), exclude them from publishing:
Option A: Via .csproj (Recommended)
<ItemGroup>
<Content Remove="compilerconfig.json;package.json" />
<None Include="compilerconfig.json;package.json">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
Option B: File properties in Visual Studio
For individual files:
- Right-click the file in Solution Explorer
- Select Properties
- Set:
- Build Action: None
- Copy to Output Directory: Do not copy
3. Handle appsettings.json in library projects
For projects that need appsettings.json
during development but shouldn't publish it:
<ItemGroup>
<Content Include="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</Content>
</ItemGroup>
4. Remove unnecessary project references
Often this error occurs when projects reference other projects that shouldn't be published:
<!-- Remove project references that cause file conflicts -->
<ItemGroup>
<ProjectReference Remove="..\OtherProject\OtherProject.csproj" />
</ItemGroup>
5. Consolidate NuGet package versions
Conflicting NuGet package versions across projects can sometimes cause this issue:
- Open NuGet Package Manager → Consolidate tab
- Identify packages with version conflicts
- Update all projects to use the same version
6. Azure DevOps pipeline solutions
For CI/CD pipelines, you can:
Option A: Remove conflicting files before publish
- script: |
find $(Build.SourcesDirectory) -name "appsettings*.json" -exec rm {} \;
Option B: Use specific SDK version
- task: UseDotNet@2
displayName: 'Install .NET SDK version'
inputs:
packageType: sdk
version: 6.0.300
installationPath: $(Agent.ToolsDirectory)/dotnet
7. NServiceBus specific solution
If using NServiceBus, disable SQL script generation:
<PropertyGroup>
<SqlPersistenceGenerateScripts>false</SqlPersistenceGenerateScripts>
</PropertyGroup>
Best Practices
- Audit project references - Ensure projects only reference what they actually need
- Use appropriate file properties - Set build actions correctly for different file types
- Separate concerns - Consider splitting projects into logical libraries and shells
- Regularly consolidate dependencies - Keep NuGet package versions consistent across solutions
When to Use Each Solution
Scenario | Recommended Solution |
---|---|
Temporary fix | Disable duplicate checking (ErrorOnDuplicatePublishOutputFiles ) |
Build/config files | Exclude from publish |
Library projects with appsettings | Use CopyToPublishDirectory>Never |
CI/CD pipelines | Remove files before publish or specify SDK version |
NuGet version conflicts | Consolidate package versions |
TIP
For long-term maintainability, prefer solutions that properly exclude unnecessary files rather than disabling the duplicate file check entirely.
This error, while frustrating, helps prevent subtle deployment issues by ensuring your publish output contains exactly what you expect. By understanding the root cause and applying the appropriate solution, you can resolve the conflict while maintaining a clean deployment structure.