Skip to content

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:

xml
<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)

xml
<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:

  1. Right-click the file in Solution Explorer
  2. Select Properties
  3. Set:
    • Build Action: None
    • Copy to Output Directory: Do not copy

File properties configuration

3. Handle appsettings.json in library projects

For projects that need appsettings.json during development but shouldn't publish it:

xml
<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:

xml
<!-- 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:

  1. Open NuGet Package ManagerConsolidate tab
  2. Identify packages with version conflicts
  3. Update all projects to use the same version

NuGet package consolidation

6. Azure DevOps pipeline solutions

For CI/CD pipelines, you can:

Option A: Remove conflicting files before publish

yaml
- script: |
    find $(Build.SourcesDirectory) -name "appsettings*.json" -exec rm {} \;

Option B: Use specific SDK version

yaml
- 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:

xml
<PropertyGroup>
  <SqlPersistenceGenerateScripts>false</SqlPersistenceGenerateScripts>
</PropertyGroup>

Best Practices

  1. Audit project references - Ensure projects only reference what they actually need
  2. Use appropriate file properties - Set build actions correctly for different file types
  3. Separate concerns - Consider splitting projects into logical libraries and shells
  4. Regularly consolidate dependencies - Keep NuGet package versions consistent across solutions

When to Use Each Solution

ScenarioRecommended Solution
Temporary fixDisable duplicate checking (ErrorOnDuplicatePublishOutputFiles)
Build/config filesExclude from publish
Library projects with appsettingsUse CopyToPublishDirectory>Never
CI/CD pipelinesRemove files before publish or specify SDK version
NuGet version conflictsConsolidate 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.