Azure / bicep

Bicep is a declarative language for describing and deploying Azure resources
MIT License
3.2k stars 742 forks source link

How to force recompiling, rewriting output files when calling Bicep from MSBuild? #11847

Closed abatishchev closed 11 months ago

abatishchev commented 11 months ago

Here's my Resources.msbuildproject file:

<Project Sdk="Microsoft.Build.NoTargets">
  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Azure.Bicep.CommandLine.win-x64" Version="0.21.1" />
    <PackageReference Include="Azure.Bicep.MSBuild" Version="0.21.1" />
  </ItemGroup>

  <ItemGroup>
    <Bicep Include="main.bicep" OutputFile="ServiceGroupRoot\Templates\resources.json" />
  </ItemGroup>

  <ItemGroup>
    <Content Include="*.bicep" />
</Project>

With the following folder structure:

main.bicep
storage.bicep
logs.bicep
\ServiceGroupRoot\Templates\
  resources.json

In order for the output files to be recompiled and rewritten, I have to delete them. Otherwise building from VS does nothing:

Build started...
Failed to resolve all items referenced by 'src\Application\Application.sfproj'. This message can typically be ignored. The issue may be resolved by fully restoring and building the solution. If the unresolved item is a project reference this can lead to an incomplete NuGet restore result and missing package references. To ensure that restore is able to find all projects verify that all projects are referenced correctly and exist on disk.
1>------ Build started: Project: Resources, Configuration: Debug Any CPU ------
1>Build started 09/15/2023 10:24:16.
1>Target _CollectTargetFrameworkForTelemetry:
1>  Task "AllowEmptyTelemetry"
1>  Done executing task "AllowEmptyTelemetry".
1>Target _CheckForUnsupportedNETCoreVersion:
1>Target _CheckForUnsupportedCppNETCoreVersion:
1>Target _CheckForUnsupportedHostingUsage:
1>Target _CheckAndUnsetUnsupportedPrefer32Bit:
1>Target _CheckForLanguageAndFeatureCombinationSupport:
1>Target _GetProjectJsonPath:
1>Target UpdateAspNetToFrameworkReference:
1>  Task "CheckIfPackageReferenceShouldBeFrameworkReference"
1>  Done executing task "CheckIfPackageReferenceShouldBeFrameworkReference".
1>  Task "CheckIfPackageReferenceShouldBeFrameworkReference"
1>  Done executing task "CheckIfPackageReferenceShouldBeFrameworkReference".
1>Target CheckForImplicitPackageReferenceOverrides:
1>  Task "CheckForImplicitPackageReferenceOverrides"
1>  Done executing task "CheckForImplicitPackageReferenceOverrides".
1>Target ApplyImplicitVersions:
1>  Task "ApplyImplicitVersions"
1>  Done executing task "ApplyImplicitVersions".
1>Target _AddMicrosoftNetCompilerToolsetFrameworkPackage:
1>Target _CheckForObsoleteDotNetCliToolReferences:
1>Target CollectPackageReferences:
1>  Task "CheckForDuplicateNuGetItemsTask"
1>  Done executing task "CheckForDuplicateNuGetItemsTask".
1>Target _GetRestoreProjectStyle:
1>  Task "GetRestoreProjectStyleTask"
1>  Done executing task "GetRestoreProjectStyleTask".
1>Target EnableIntermediateOutputPathMismatchWarning:
1>Target CheckForDuplicateItems:
1>  Task "CheckForDuplicateItems"
1>  Done executing task "CheckForDuplicateItems".
1>  Task "CheckForDuplicateItems"
1>  Done executing task "CheckForDuplicateItems".
1>  Task "CheckForDuplicateItems"
1>  Done executing task "CheckForDuplicateItems".
1>Target _CheckForInvalidConfigurationAndPlatform:
1>  Task "Message"
1>  Done executing task "Message".
1>  Task "Message"
1>  Done executing task "Message".
1>Target ValidateCommandLineProperties:
1>Target BeforeBuild:
1>Target BuildOnlySettings:
1>Target GetFrameworkPaths:
1>Target GetReferenceAssemblyPaths:
1>Target PrepareForBuild:
1>  Task "FindAppConfigFile"
1>  Done executing task "FindAppConfigFile".
1>  Task "MakeDir"
1>  Done executing task "MakeDir".
1>Target BeforeResolveReferences:
1>Target _DefaultMicrosoftNETPlatformLibrary:
1>Target _ComputePackageReferencePublish:
1>Target ResolvePackageAssets:
1>  Task "ResolvePackageAssets"
1>  Done executing task "ResolvePackageAssets".
1>Target ResolveLockFileReferences:
1>  Task "JoinItems"
1>  Done executing task "JoinItems".
1>Target ResolveLockFileAnalyzers:
1>Target ResolveLockFileCopyLocalFiles:
1>Target IncludeTransitiveProjectReferences:
1>Target ResolvePackageDependenciesForBuild:
1>Target AssignProjectConfiguration:
1>  Task "AssignProjectConfiguration"
1>  Done executing task "AssignProjectConfiguration".
1>Target _SplitProjectReferencesByFileExistence:
1>Target _AddOutputPathToGlobalPropertiesToRemove:
1>Target _GetProjectReferenceTargetFrameworkProperties:
1>  Task "MSBuild"
1>  Done executing task "MSBuild".
1>  Task "SetRidAgnosticValueForProjects"
1>  Done executing task "SetRidAgnosticValueForProjects".
1>Target ValidateExecutableReferences:
1>  Task "ValidateExecutableReferences"
1>  Done executing task "ValidateExecutableReferences".
1>Target PrepareProjectReferences:
1>Target ResolveProjectReferences:
1>Target ResolveSDKReferences:
1>Target ExpandSDKReferences:
1>Target _HandlePackageFileConflicts:
1>  Task "ResolvePackageFileConflicts"
1>  Done executing task "ResolvePackageFileConflicts".
1>Target ResolveAssemblyReferences:
1>Target BeforeGenerateFakesAssemblies:
1>Target GenerateFakes:
1>Target AfterResolveReferences:
1>Target ResolveReferences:
1>Target _BlockWinMDsOnUnsupportedTFMs:
1>  Task "CheckForUnsupportedWinMDReferences"
1>  Done executing task "CheckForUnsupportedWinMDReferences".
1>Target _SetEmbeddedWin32ManifestProperties:
1>  Task "GetFrameworkPath"
1>  Done executing task "GetFrameworkPath".
1>Target SetWin32ManifestProperties:
1>Target FindReferenceAssembliesForReferences:
1>Target _GenerateCompileInputs:
1>Target BeforeCompile:
1>Target _GenerateCompileDependencyCache:
1>Target GenerateCompiledExpressionsTempFile:
1>Target DesignTimeXamlMarkupCompilation:
1>Target GenerateNETCompatibleDefineConstants:
1>Target AddImplicitDefineConstants:
1>Target CoreCompile:
1>Target BeforeBicepCompile:
1>  Task "MakeDir"
1>  Done executing task "MakeDir".
1>Target BicepCompile:
1>Target AfterCompile:
1>Target Compile:
1>Target GetTargetPath:
1>Target ComputeIntermediateSatelliteAssemblies:
1>Target _ComputeNETCoreBuildOutputFiles:
1>Target AssignTargetPaths:
1>  Task "AssignTargetPath"
1>  Done executing task "AssignTargetPath".
1>  Task "AssignTargetPath"
1>  Done executing task "AssignTargetPath".
1>  Task "AssignTargetPath"
1>  Done executing task "AssignTargetPath".
1>  Task "AssignTargetPath"
1>  Done executing task "AssignTargetPath".
1>Target _PopulateCommonStateForGetCopyToOutputDirectoryItems:
1>Target GetCopyToOutputDirectoryXamlAppDefs:
1>  Task "AssignTargetPath"
1>  Done executing task "AssignTargetPath".
1>Target GetCopyToOutputDirectoryItems:
1>  Task "CallTarget"
1>    Target _GetCopyToOutputDirectoryItemsFromTransitiveProjectReferences:
1>      Task "AssignTargetPath"
1>      Done executing task "AssignTargetPath".
1>  Done executing task "CallTarget".
1>  Task "CallTarget"
1>    Target _GetCopyToOutputDirectoryItemsFromThisProject:
1>      Task "AssignTargetPath"
1>      Done executing task "AssignTargetPath".
1>  Done executing task "CallTarget".
1>Target _CopySourceItemsToOutputDirectory:
1>Target _CheckForCompileOutputs:
1>Target CopyFilesToOutputDirectory:
1>Target PrepareForRun:
1>Target _CleanGetCurrentAndPriorFileWrites:
1>  Task "ReadLinesFromFile"
1>  Done executing task "ReadLinesFromFile".
1>  Task "ConvertToAbsolutePath"
1>  Done executing task "ConvertToAbsolutePath".
1>  Task "FindUnderPath"
1>  Done executing task "FindUnderPath".
1>  Task "FindUnderPath"
1>  Done executing task "FindUnderPath".
1>  Task "FindUnderPath"
1>  Done executing task "FindUnderPath".
1>  Task "RemoveDuplicates"
1>  Done executing task "RemoveDuplicates".
1>Target IncrementalClean:
1>  Task "FindUnderPath"
1>  Done executing task "FindUnderPath".
1>  Task "FindUnderPath"
1>  Done executing task "FindUnderPath".
1>  Task "Delete"
1>  Done executing task "Delete".
1>  Task "RemoveDuplicates"
1>  Done executing task "RemoveDuplicates".
1>Target CoreBuild:
1>Target AfterBuild:
1>Target Build:
1>Target Package:
1>  Task "Copy"
1>  Done executing task "Copy".
1>  Task "Copy"
1>  Done executing task "Copy".
1>  Task "Copy"
1>  Done executing task "Copy".
1>  Task "Copy"
1>  Done executing task "Copy".
1>  Task "Copy"
1>  Done executing task "Copy".
1>  Task "Copy"
1>  Done executing task "Copy".
1>  Task "Copy"
1>  Done executing task "Copy".
1>  Task "Copy"
1>  Done executing task "Copy".
1>  Task "Copy"
1>  Done executing task "Copy".
1>
1>Done building project "Resources.msbuildproj".
1>
1>Build succeeded.
1>    0 Warning(s)
1>    0 Error(s)
1>
1>Time Elapsed 00:00:00.13
========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========
========== Build started at 10:24 and took 00.515 seconds ==========

> Build started at 10:24 and took 0.247 seconds

Environment:

Visual Studio version 17.7,4
Bicep for Visual Studio version 0.21.1.54444 (from 9/11)

Microsoft.Build.NoTargets version 3.7.0
abatishchev commented 11 months ago

Here's what I found:

If my project includes only the main file:

<ItemGroup>
  <Bicep Include="main.bicep" OutputFile="ServiceGroupRoot\Templates\resources.json" />
</ItemGroup>

Athen build/MSBuild/VS doesn't detect any changes in other Bicep files, e.g. modules.

But if it includes all files like this:

<ItemGroup>
  <Bicep Include="*.bicep" OutputFile="ServiceGroupRoot\Templates\%(RecursiveDir)\%(FileName).json" />
</ItemGroup>

Then it does detect the changes any of the files but then generates a separate ARM template per each module, what's not what I need.

majastrz commented 11 months ago

I think the issue is that we need to update our MSBuild package to provide a hint to Visual Studio not to skip the project. VS runs its own heuristics to determine whether a project has changed and they are different from MSBuild's.

majastrz commented 11 months ago

You add this to your project and it should start behaving normally:

  <ItemGroup>
    <AvailableItemName Include="Bicep" />
  </ItemGroup>

This will also cause *.bicep files to show up in Solution Explorer in VS. I'm going to add this to our target files as well.