Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use MSBuildProjectExtensionsPath for RestoreOutputPath #2056

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ private static string[] SplitArgs(string unsplitArguments)
{
if (string.IsNullOrWhiteSpace(unsplitArguments))
{
return new string[0];
return Array.Empty<string>();
}

var ptrToSplitArgs = CommandLineToArgvW(unsplitArguments, out int numberOfArgs);
if (ptrToSplitArgs == IntPtr.Zero)
{
return new string[0];
return Array.Empty<string>();
}

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public override async Task<string> GetAssetsFilePathAsync()
public override async Task<string> GetCacheFilePathAsync()
{
await _threadingService.JoinableTaskFactory.SwitchToMainThreadAsync();
return NoOpRestoreUtilities.GetProjectCacheFilePath(cacheRoot: GetBaseIntermediatePath(), projectPath: _projectFullPath);
return NoOpRestoreUtilities.GetProjectCacheFilePath(cacheRoot: GetMSBuildProjectExtensionsPath(), projectPath: _projectFullPath);
}

public override async Task<string> GetAssetsFilePathOrNullAsync()
Expand All @@ -91,14 +91,23 @@ private async Task<string> GetAssetsFilePathAsync(bool shouldThrow)
{
await _threadingService.JoinableTaskFactory.SwitchToMainThreadAsync();

var baseIntermediatePath = GetBaseIntermediatePath(shouldThrow);
var msbuildProjectExtensionsPath = GetMSBuildProjectExtensionsPath(shouldThrow);

if (baseIntermediatePath == null)
if (msbuildProjectExtensionsPath == null)
{
return null;
}

return Path.Combine(baseIntermediatePath, LockFileFormat.AssetsFileName);
return Path.Combine(msbuildProjectExtensionsPath, LockFileFormat.AssetsFileName);
}

private async Task<string> GetAssetsCacheFolderAsync(bool shouldThrow)
{
await _threadingService.JoinableTaskFactory.SwitchToMainThreadAsync();

var msbuildProjectExtensionsPath = GetMSBuildProjectExtensionsPath(shouldThrow);

return msbuildProjectExtensionsPath;
}

#endregion BuildIntegratedNuGetProject
Expand Down Expand Up @@ -164,25 +173,26 @@ public override async Task<bool> UninstallPackageAsync(

#endregion

private string GetBaseIntermediatePath(bool shouldThrow = true)
private string GetMSBuildProjectExtensionsPath(bool shouldThrow = true)
{
ThreadHelper.ThrowIfNotOnUIThread();

var baseIntermediatePath = _vsProjectAdapter.BaseIntermediateOutputPath;
var msbuildProjectExtensionsPath = _vsProjectAdapter.MSBuildProjectExtensionsPath;

if (string.IsNullOrEmpty(baseIntermediatePath))
if (string.IsNullOrEmpty(msbuildProjectExtensionsPath))
{
if (shouldThrow)
{
throw new InvalidDataException(string.Format(
Strings.BaseIntermediateOutputPathNotFound,
Strings.MSBuildPropertyNotFound,
ProjectBuildProperties.MSBuildProjectExtensionsPath,
_vsProjectAdapter.ProjectDirectory));
}

return null;
}

return baseIntermediatePath;
return msbuildProjectExtensionsPath;
}

private string GetPackagesPath(ISettings settings)
Expand Down Expand Up @@ -337,7 +347,7 @@ private async Task<PackageSpec> GetPackageSpecAsync(ISettings settings)
RestoreMetadata = new ProjectRestoreMetadata
{
ProjectStyle = ProjectStyle.PackageReference,
OutputPath = GetBaseIntermediatePath(),
OutputPath = GetMSBuildProjectExtensionsPath(),
ProjectPath = _projectFullPath,
ProjectName = projectName,
ProjectUniqueName = _projectFullPath,
Expand All @@ -352,7 +362,7 @@ private async Task<PackageSpec> GetPackageSpecAsync(ISettings settings)
}
},
SkipContentFileWrite = true,
CacheFilePath = await GetCacheFilePathAsync(),
AssetsCacheFolder = await GetAssetsCacheFolderAsync(true),
PackagesPath = GetPackagesPath(settings),
Sources = GetSources(settings),
FallbackFolders = GetFallbackFolders(settings),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ internal class VsProjectAdapter : IVsProjectAdapter

#region Properties

public string BaseIntermediateOutputPath
public string MSBuildProjectExtensionsPath
{
get
{
var baseIntermediateOutputPath = BuildProperties.GetPropertyValue(ProjectBuildProperties.BaseIntermediateOutputPath);
var msbuildProjectExtensionsPath = BuildProperties.GetPropertyValue(ProjectBuildProperties.MSBuildProjectExtensionsPath);

if (string.IsNullOrEmpty(baseIntermediateOutputPath))
if (string.IsNullOrEmpty(msbuildProjectExtensionsPath))
{
return null;
}

return Path.Combine(ProjectDirectory, baseIntermediateOutputPath);
return Path.Combine(ProjectDirectory, msbuildProjectExtensionsPath);
}
}

Expand Down Expand Up @@ -278,7 +278,7 @@ public async Task<string[]> GetProjectTypeGuidsAsync()
return new string[] { _projectTypeGuid };
}

return new string[0];
return Array.Empty<string>();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,24 @@ public VsProjectJsonNuGetProject(
ProjectServices = projectServices;
}

protected override async Task<string> GetBaseIntermediatePathAsync()
protected override async Task<string> GetBaseIntermediateOutputPathAsync()
{
var baseIntermediatePath = await ProjectServices.BuildProperties.GetPropertyValueAsync(ProjectBuildProperties.BaseIntermediateOutputPath);
var baseIntermediateOutputPath = await ProjectServices.BuildProperties.GetPropertyValueAsync(ProjectBuildProperties.BaseIntermediateOutputPath);

if (string.IsNullOrEmpty(baseIntermediatePath))
if (string.IsNullOrEmpty(baseIntermediateOutputPath))
{
throw new InvalidDataException(string.Format(
Strings.BaseIntermediateOutputPathNotFound,
Strings.MSBuildPropertyNotFound,
ProjectBuildProperties.BaseIntermediateOutputPath,
MSBuildProjectPath));
}

return UriUtility.GetAbsolutePathFromFile(MSBuildProjectPath, baseIntermediatePath);
return UriUtility.GetAbsolutePathFromFile(MSBuildProjectPath, baseIntermediateOutputPath);
}

public override async Task<string> GetCacheFilePathAsync()
{
return NoOpRestoreUtilities.GetProjectCacheFilePath(await GetBaseIntermediatePathAsync(), MSBuildProjectPath);
return NoOpRestoreUtilities.GetProjectCacheFilePath(await GetBaseIntermediateOutputPathAsync(), MSBuildProjectPath);
}

protected override async Task UpdateInternalTargetFrameworkAsync()
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@
<data name="ProjectNotLoaded_RestoreFailed" xml:space="preserve">
<value>The operation failed as details for project {0} could not be loaded.</value>
</data>
<data name="BaseIntermediateOutputPathNotFound" xml:space="preserve">
<value>The BaseIntermediateOutputPath MSBuild property could not be found for project '{0}'.</value>
<comment>{0} is the full path to the project.</comment>
<data name="MSBuildPropertyNotFound" xml:space="preserve">
<value>The {0} MSBuild property could not be found for project '{1}'.</value>
<comment>{0} is the name of the property that was not set. {1} is the full path to the project.</comment>
</data>
<data name="ProjectCouldNotBeCastedToBuildPropertyStorage" xml:space="preserve">
<value>The project '{0}' could not be cast to a build property storage interface, which is required to get MSBuild properties inside Visual Studio.</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,11 @@ private static PackageSpec ToPackageSpec(ProjectNames projectNames, IVsProjectRe
treatWarningsAsErrors: GetNonEvaluatedPropertyOrNull(projectRestoreInfo.TargetFrameworks, TreatWarningsAsErrors, e => e),
warningsAsErrors: GetNonEvaluatedPropertyOrNull(projectRestoreInfo.TargetFrameworks, WarningsAsErrors, e => e),
noWarn: GetNonEvaluatedPropertyOrNull(projectRestoreInfo.TargetFrameworks, NoWarn, e => e)),
CacheFilePath = NoOpRestoreUtilities.GetProjectCacheFilePath(cacheRoot: outputPath, projectPath: projectFullPath)
AssetsCacheFolder = outputPath
},
RuntimeGraph = GetRuntimeGraph(projectRestoreInfo),
RestoreSettings = new ProjectRestoreSettings() { HideWarningsAndErrors = true }

};

return packageSpec;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public interface IVsProjectAdapter
string AssetTargetFallback { get; }

/// <summary>
/// BaseIntermediateOutputPath project property (e.g. c:\projFoo\obj)
/// MSBuildProjectExtensionsPath project property (e.g. c:\projFoo\obj)
/// </summary>
string BaseIntermediateOutputPath { get; }
string MSBuildProjectExtensionsPath { get; }

IProjectBuildProperties BuildProperties { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static string[] GetProjectTypeGuids(IVsHierarchy hierarchy, string defaul
return new[] { defaultType };
}

return new string[0];
return Array.Empty<string>();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public string ActivePackageSource

public string[] GetPackageSources()
{
return new string[0];
return Array.Empty<string>();
}

public string DefaultProject
Expand All @@ -60,7 +60,7 @@ public void SetDefaultProjectIndex(int index)

public string[] GetAvailableProjects()
{
return new string[0];
return Array.Empty<string>();
}

public void SetDefaultRunspace()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ Copyright (c) .NET Foundation. All rights reserved.
</PropertyGroup>
<PropertyGroup>
<PackageOutputPath Condition=" '$(PackageOutputPath)' == '' ">$(OutputPath)</PackageOutputPath>
<RestoreOutputPath Condition=" '$(RestoreOutputPath)' == '' " >$(BaseIntermediateOutputPath)</RestoreOutputPath>
<RestoreOutputPath Condition=" '$(RestoreOutputPath)' == '' " >$(MSBuildProjectExtensionsPath)</RestoreOutputPath>
</PropertyGroup>

<ConvertToAbsolutePath Paths="$(NuspecOutputPath)">
Expand Down
4 changes: 2 additions & 2 deletions src/NuGet.Core/NuGet.Build.Tasks/GetRestoreSettingsTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public override bool Execute()
// Sources
var currentSources = RestoreSettingsUtils.GetValue(
() => RestoreSourcesOverride?.Select(MSBuildRestoreUtility.FixSourcePath).Select(e => GetGlobalAbsolutePath(e)).ToArray(),
() => MSBuildRestoreUtility.ContainsClearKeyword(RestoreSources) ? new string[0] : null,
() => MSBuildRestoreUtility.ContainsClearKeyword(RestoreSources) ? Array.Empty<string>() : null,
() => RestoreSources?.Select(MSBuildRestoreUtility.FixSourcePath).Select(e => UriUtility.GetAbsolutePathFromFile(ProjectUniqueName, e)).ToArray(),
() => (new PackageSourceProvider(settings)).LoadPackageSources().Where(e => e.IsEnabled).Select(e => e.Source).ToArray());

Expand All @@ -148,7 +148,7 @@ public override bool Execute()
// Fallback folders
var currentFallbackFolders = RestoreSettingsUtils.GetValue(
() => RestoreFallbackFoldersOverride?.Select(e => GetGlobalAbsolutePath(e)).ToArray(),
() => MSBuildRestoreUtility.ContainsClearKeyword(RestoreFallbackFolders) ? new string[0] : null,
() => MSBuildRestoreUtility.ContainsClearKeyword(RestoreFallbackFolders) ? Array.Empty<string>() : null,
() => RestoreFallbackFolders?.Select(e => UriUtility.GetAbsolutePathFromFile(ProjectUniqueName, e)).ToArray(),
() => SettingsUtility.GetFallbackPackageFolders(settings).ToArray());

Expand Down
43 changes: 39 additions & 4 deletions src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,25 @@ Copyright (c) .NET Foundation. All rights reserved.
</PropertyGroup>
</Target>

<!--
============================================================
EnableIntermediateOutputPathMismatchWarning
If using PackageReference, enable an MSBuild warning if BaseIntermediateOutputPath is set to something different
than MSBuildProjectExtensionsPath, because it may be unexpected that the assets and related files wouldn't be written
to the BaseIntermediateOutputPath.
============================================================
-->

<Target Name="EnableIntermediateOutputPathMismatchWarning" DependsOnTargets="_GetRestoreProjectStyle"
BeforeTargets="_CheckForInvalidConfigurationAndPlatform"
Condition="'$(RestoreProjectStyle)' == 'PackageReference'">

<PropertyGroup Condition="'$(EnableBaseIntermediateOutputPathMismatchWarning)' == ''">
<EnableBaseIntermediateOutputPathMismatchWarning>true</EnableBaseIntermediateOutputPathMismatchWarning>
</PropertyGroup>

</Target>

<!--
============================================================
_GetRestoreTargetFrameworksOutput
Expand Down Expand Up @@ -569,13 +588,27 @@ Copyright (c) .NET Foundation. All rights reserved.
Returns="@(_RestoreGraphEntry)">

<!-- Determine the restore output path -->
<PropertyGroup Condition=" '$(PackageReferenceCompatibleProjectStyle)' == 'true' OR '$(RestoreProjectStyle)' == 'ProjectJson' ">
<RestoreOutputPath Condition=" '$(RestoreOutputPath)' == '' " >$(BaseIntermediateOutputPath)</RestoreOutputPath>
<PropertyGroup Condition=" '$(PackageReferenceCompatibleProjectStyle)' == 'true' ">
<!-- NuGet writes .props and .targets files to the RestoreOutputPath for PackageReference projects. MSBuild common
targets import these via the MSBuildProjectExtensionsPath, so that's what the RestoreOutputPath should be. -->
<RestoreOutputPath Condition=" '$(RestoreOutputPath)' == '' " >$(MSBuildProjectExtensionsPath)</RestoreOutputPath>
</PropertyGroup>

<ConvertToAbsolutePath Paths="$(RestoreOutputPath)" Condition=" '$(PackageReferenceCompatibleProjectStyle)' == 'true' OR '$(RestoreProjectStyle)' == 'ProjectJson'">
<ConvertToAbsolutePath Paths="$(RestoreOutputPath)" Condition=" '$(PackageReferenceCompatibleProjectStyle)' == 'true' ">
<Output TaskParameter="AbsolutePaths" PropertyName="RestoreOutputAbsolutePath" />
</ConvertToAbsolutePath>

<PropertyGroup Condition=" '$(RestoreProjectStyle)' == 'ProjectJson' ">
<!-- For project.json projects, the restore output path is the project directory. The assets file cache
goes in BaseIntermediateOutputPath. -->
<RestoreAssetsCacheFolder Condition=" '$(RestoreOutputPath)' == '' " >$(BaseIntermediateOutputPath)</RestoreAssetsCacheFolder>
</PropertyGroup>

<ConvertToAbsolutePath Paths="$(RestoreAssetsCacheFolder)" Condition=" '$(RestoreProjectStyle)' == 'ProjectJson'">
<Output TaskParameter="AbsolutePaths" PropertyName="RestoreAssetsCacheFolder" />
</ConvertToAbsolutePath>



<!--
Determine project name for the assets file.
Expand Down Expand Up @@ -626,6 +659,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<PackagesPath>$(_OutputPackagesPath)</PackagesPath>
<ProjectStyle>$(RestoreProjectStyle)</ProjectStyle>
<OutputPath>$(RestoreOutputAbsolutePath)</OutputPath>
<AssetsCacheFolder>$(RestoreOutputAbsolutePath)</AssetsCacheFolder>
<TargetFrameworks>@(_RestoreTargetFrameworksOutputFiltered)</TargetFrameworks>
<RuntimeIdentifiers>$(RuntimeIdentifiers);$(RuntimeIdentifier)</RuntimeIdentifiers>
<RuntimeSupports>$(RuntimeSupports)</RuntimeSupports>
Expand All @@ -648,7 +682,8 @@ Copyright (c) .NET Foundation. All rights reserved.
<ProjectPath>$(MSBuildProjectFullPath)</ProjectPath>
<ProjectName>$(_RestoreProjectName)</ProjectName>
<Sources>$(_OutputSources)</Sources>
<OutputPath>$(RestoreOutputAbsolutePath)</OutputPath>
<OutputPath>$(MSBuildProjectDirectory)</OutputPath>
<AssetsCacheFolder>$(RestoreAssetsCacheFolder)</AssetsCacheFolder>
<FallbackFolders>$(_OutputFallbackFolders)</FallbackFolders>
<PackagesPath>$(_OutputPackagesPath)</PackagesPath>
<ProjectJsonPath>$(_CurrentProjectJsonPath)</ProjectJsonPath>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
Expand Down Expand Up @@ -28,8 +29,8 @@ public override bool Execute()
var log = new MSBuildLogger(Log);

// item -> string
var all = AllProjects?.Select(e => e.ItemSpec).ToArray() ?? new string[0];
var valid = ValidProjects?.Select(e => e.ItemSpec).ToArray() ?? new string[0];
var all = AllProjects?.Select(e => e.ItemSpec).ToArray() ?? Array.Empty<string>();
var valid = ValidProjects?.Select(e => e.ItemSpec).ToArray() ?? Array.Empty<string>();

// log inputs
BuildTasksUtility.LogInputParam(log, nameof(AllProjects), all);
Expand Down
Loading