Skip to content

Commit

Permalink
Fixes unhandled exception with GetRootedPath #1251 (#1252)
Browse files Browse the repository at this point in the history
* Fixes unhandled exception with GetRootedPath #1251

* Update build version
  • Loading branch information
BernieWhite authored Aug 31, 2022
1 parent cb799e1 commit 7906d68
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 24 deletions.
26 changes: 16 additions & 10 deletions PSRule.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PSRule.Badges", "src\PSRule
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PSRule.BuildTool", "src\PSRule.BuildTool\PSRule.BuildTool.csproj", "{20DDCC65-8A9A-4BDC-91EC-C3BE6F32E52E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PSRule.SDK", "src\PSRule.SDK\PSRule.SDK.csproj", "{6B21D558-BFC3-4BC6-963E-83B65353196F}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PSRule.SDK", "src\PSRule.SDK\PSRule.SDK.csproj", "{07A84E67-1CA3-4766-B9EA-1FDD9DF6516F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PSRule.Tool", "src\PSRule.Tool\PSRule.Tool.csproj", "{F6CFCA60-72D5-474E-8B8B-1AB973434569}"
EndProject
Expand All @@ -22,7 +22,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
src\PSRule.Common.props = src\PSRule.Common.props
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PSRule.Types", "src\PSRule.Types\PSRule.Types.csproj", "{6E48E1BA-9A94-4412-BB3F-F5F569E05A4E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PSRule.Types", "src\PSRule.Types\PSRule.Types.csproj", "{5FE4DB0B-63D1-4DDB-9762-9C0D29168BC9}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PSRule.BuildTask", "src\PSRule.BuildTask\PSRule.BuildTask.csproj", "{872D2648-2F00-475E-84B5-F08BE07385B7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -50,18 +52,22 @@ Global
{20DDCC65-8A9A-4BDC-91EC-C3BE6F32E52E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{20DDCC65-8A9A-4BDC-91EC-C3BE6F32E52E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{20DDCC65-8A9A-4BDC-91EC-C3BE6F32E52E}.Release|Any CPU.Build.0 = Release|Any CPU
{6B21D558-BFC3-4BC6-963E-83B65353196F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6B21D558-BFC3-4BC6-963E-83B65353196F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6B21D558-BFC3-4BC6-963E-83B65353196F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6B21D558-BFC3-4BC6-963E-83B65353196F}.Release|Any CPU.Build.0 = Release|Any CPU
{07A84E67-1CA3-4766-B9EA-1FDD9DF6516F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{07A84E67-1CA3-4766-B9EA-1FDD9DF6516F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{07A84E67-1CA3-4766-B9EA-1FDD9DF6516F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{07A84E67-1CA3-4766-B9EA-1FDD9DF6516F}.Release|Any CPU.Build.0 = Release|Any CPU
{F6CFCA60-72D5-474E-8B8B-1AB973434569}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F6CFCA60-72D5-474E-8B8B-1AB973434569}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F6CFCA60-72D5-474E-8B8B-1AB973434569}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F6CFCA60-72D5-474E-8B8B-1AB973434569}.Release|Any CPU.Build.0 = Release|Any CPU
{6E48E1BA-9A94-4412-BB3F-F5F569E05A4E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6E48E1BA-9A94-4412-BB3F-F5F569E05A4E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6E48E1BA-9A94-4412-BB3F-F5F569E05A4E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6E48E1BA-9A94-4412-BB3F-F5F569E05A4E}.Release|Any CPU.Build.0 = Release|Any CPU
{5FE4DB0B-63D1-4DDB-9762-9C0D29168BC9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5FE4DB0B-63D1-4DDB-9762-9C0D29168BC9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5FE4DB0B-63D1-4DDB-9762-9C0D29168BC9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5FE4DB0B-63D1-4DDB-9762-9C0D29168BC9}.Release|Any CPU.Build.0 = Release|Any CPU
{872D2648-2F00-475E-84B5-F08BE07385B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{872D2648-2F00-475E-84B5-F08BE07385B7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{872D2648-2F00-475E-84B5-F08BE07385B7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{872D2648-2F00-475E-84B5-F08BE07385B7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 2 additions & 0 deletions docs/CHANGELOG-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ What's changed since v2.4.0:
- Fixed could not load Microsoft.Management.Infrastructure by @BernieWhite.
[#1249](https://github.com/microsoft/PSRule/issues/1249)
- To use minimal initial session state set `Execution.InitialSessionState` to `Minimal`.
- Fixed unhandled exception with GetRootedPath by @BernieWhite.
[#1251](https://github.com/microsoft/PSRule/issues/1251)

## v2.4.0

Expand Down
5 changes: 3 additions & 2 deletions pipeline.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,12 @@ task TestDotNet {
task BuildCLI BuildModule, {
exec {
# Build library
dotnet publish src/PSRule.Tool -c $Configuration --no-self-contained -r win-x64 -o ./out/cli/win-x64/ -p:version=$Build
dotnet publish src/PSRule.Tool -c $Configuration --self-contained /p:DebugType=None /p:DebugSymbols=false -r win-x64 -o ./out/cli/win-x64/ -p:version=$Build
# dotnet publish src/PSRule.Tool -c $Configuration --no-self-contained -r win-x64 -o ./out/cli/win-x64/ -p:version=$Build
# dotnet publish --self-contained true -p:PublishTrimmed=true -p:PublishSingleFile=true -r win-x64 .\src\PSRule.Tool\PSRule.Tool.csproj -o .\out\cli
}

Copy-Item -Path out/modules/PSRule/ -Destination out/cli/win-x64/modules/ -Recurse -Force;
# Copy-Item -Path out/modules/PSRule/ -Destination out/cli/win-x64/modules/ -Recurse -Force;
}

task CopyModule {
Expand Down
5 changes: 5 additions & 0 deletions src/PSRule.BuildTask/Generator.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project>
<ItemGroup>
<CompilerVisibleProperty Include="PSRule_Version" />
</ItemGroup>
</Project>
49 changes: 49 additions & 0 deletions src/PSRule.BuildTask/Generators/EngineVersionGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using Microsoft.CodeAnalysis;

namespace PSRule.BuildTask.Generators
{
/// <summary>
/// Generator contants for PSRule engine version.
/// </summary>
[Generator]
public sealed class EngineVersionGenerator : ISourceGenerator
{
// Detailed from Roslyn SDK: https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/source-generators-overview

/// <inheritdoc/>
public void Execute(GeneratorExecutionContext context)
{
var result = GetPartialContent(context);
context.AddSource("EngineVersion.g.cs", result);
}

/// <inheritdoc/>
public void Initialize(GeneratorInitializationContext context)
{
// Not required.
}

private static string GetPartialContent(GeneratorExecutionContext context)
{
if (!context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.PSRule_Version", out var productVersion))
productVersion = "0.0.1";

// Build up the source code
return $@"// <auto-generated/>
using System;
namespace PSRule
{{
internal static partial class Engine
{{
private const string _Version = ""{productVersion}"";
}}
}}
";
}
}
}
17 changes: 17 additions & 0 deletions src/PSRule.BuildTask/PSRule.BuildTask.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\PSRule.Common.props" />
<PropertyGroup>
<AssemblyTitle>PSRule.BuildTask</AssemblyTitle>
<EnableNuget>false</EnableNuget>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.2.0" />
</ItemGroup>

<!--<ItemGroup>
<None Include="Generator.props" Pack="true" PackagePath="build" Visible="false" />
</ItemGroup>-->

</Project>
4 changes: 0 additions & 4 deletions src/PSRule.BuildTool/PSRule.BuildTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
</ItemGroup>

<PropertyGroup Condition="'$(OS)' == 'Windows_NT'">
<DefineConstants>Windows</DefineConstants>
</PropertyGroup>

<ItemGroup>
<Compile Update="Resources\CmdStrings.Designer.cs">
<DependentUpon>CmdStrings.resx</DependentUpon>
Expand Down
1 change: 1 addition & 0 deletions src/PSRule.Tool/ClientHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ private static void InstallVersion(PowerShell pwsh, string name, string version)
private static PSRuleOption GetOption()
{
var option = PSRuleOption.FromFileOrEmpty();
option.Execution.InitialSessionState = Configuration.SessionState.Minimal;
option.Input.Format = InputFormat.File;
option.Output.Style = OutputStyle.Client;
return option;
Expand Down
22 changes: 22 additions & 0 deletions src/PSRule/Common/Engine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.IO;
using PSRule.Configuration;

namespace PSRule
{
internal static partial class Engine
{
internal static string GetLocalPath()
{
return PSRuleOption.GetRootedBasePath(Path.GetDirectoryName(AppContext.BaseDirectory));
}

internal static string GetVersion()
{
return _Version;
}
}
}
12 changes: 12 additions & 0 deletions src/PSRule/PSRule.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@
<PackageReference Include="Sarif.Sdk" Version="2.4.16" />
</ItemGroup>

<!-- Add code generators -->
<ItemGroup>
<CompilerVisibleProperty Include="PSRule_Version" />
<ProjectReference Include="..\PSRule.BuildTask\PSRule.BuildTask.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

<PropertyGroup>
<PSRule_Version>$(version)</PSRule_Version>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>

<!-- Add PSRule types -->
<ItemGroup>
<ProjectReference Include="..\PSRule.Types\PSRule.Types.csproj" />
<ProjectReference Include="..\PSRule.Badges\PSRule.Badges.csproj" />
Expand Down
10 changes: 5 additions & 5 deletions src/PSRule/Pipeline/Formatters/AssertFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Management.Automation;
using System.Reflection;
using System.Threading;
using PSRule.Configuration;
using PSRule.Definitions;
Expand Down Expand Up @@ -137,7 +135,7 @@ internal abstract class AssertFormatterBase : PipelineLoggerBase, IAssertFormatt
private bool _UnbrokenInfo;
private bool _UnbrokenObject;

private static readonly TerminalSupport DefaultTerminalSupport = new TerminalSupport(4);
private static readonly TerminalSupport DefaultTerminalSupport = new(4);

protected AssertFormatterBase(Source[] source, IPipelineWriter writer, PSRuleOption option)
{
Expand Down Expand Up @@ -369,8 +367,10 @@ private void Source(Source[] source)
if (!Option.Output.Banner.GetValueOrDefault(BannerFormat.Default).HasFlag(BannerFormat.Source))
return;

var version = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion;
WriteLineFormat(FormatterStrings.PSRuleVersion, version);
var version = Engine.GetVersion();
if (!string.IsNullOrEmpty(version))
WriteLineFormat(FormatterStrings.PSRuleVersion, version);

var list = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
for (var i = 0; source != null && i < source.Length; i++)
{
Expand Down
2 changes: 1 addition & 1 deletion src/PSRule/Pipeline/Output/SarifOutputWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ private static FailureLevel GetLevel(RuleRecord record)

private Tool GetTool(Source[] source)
{
var version = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion;
var version = Engine.GetVersion();
return new Tool
{
Driver = new ToolComponent
Expand Down
2 changes: 1 addition & 1 deletion src/PSRule/Pipeline/PipelineBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ protected bool RequireModules()
var result = true;
if (Option.Requires.TryGetValue(ENGINE_MODULE_NAME, out var requiredVersion))
{
var engineVersion = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion;
var engineVersion = Engine.GetVersion();
if (GuardModuleVersion(ENGINE_MODULE_NAME, engineVersion, requiredVersion))
result = false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/PSRule/Pipeline/SourcePipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ internal SourcePipelineBuilder(IHostContext hostContext, PSRuleOption option)
_Writer = new HostPipelineWriter(hostContext, option);
_Writer.EnterScope("[Discovery.Source]");
_UseDefaultPath = option == null || option.Include == null || option.Include.Path == null;
_LocalPath = PSRuleOption.GetRootedBasePath(Path.GetDirectoryName(typeof(SourcePipelineBuilder).Assembly.Location));
_LocalPath = Engine.GetLocalPath();

// Include paths from options
if (!_UseDefaultPath)
Expand Down

0 comments on commit 7906d68

Please sign in to comment.