gaelcolas / Sampler

Module template with build pipeline and examples, including DSC elements.
MIT License
172 stars 42 forks source link

Get-Psm1SchemaName: Fails to parse schema.psm1 correctly #410

Open johlju opened 1 year ago

johlju commented 1 year ago

Problem description

Fails to build the module in the pipeline if there is a composite resource.

Verbose logs

2023-01-10T15:46:43.7511363Z ===============================================================================
2023-01-10T15:46:43.7513500Z           BUILD DSCRESOURCESTOEXPORT MODULEBUILDER
2023-01-10T15:46:43.7532721Z 
2023-01-10T15:46:43.7533635Z -------------------------------------------------------------------------------
2023-01-10T15:46:43.7535504Z   /pack/build/Build_Module_ModuleBuilder/Build_DscResourcesToExport_ModuleBuilder
2023-01-10T15:46:43.7537436Z   /home/vsts/work/1/s/output/RequiredModules/Sampler/0.116.1/tasks/Build-Module.ModuleBuilder.build.ps1:327
2023-01-10T15:46:43.7538670Z 
2023-01-10T15:46:43.7575233Z    Project Name               = 'ConfigMgrCBDsc'
2023-01-10T15:46:43.7576148Z    Source Path                = '/home/vsts/work/1/s/source'
2023-01-10T15:46:43.7578434Z    Output Directory           = '/home/vsts/work/1/s/output'
2023-01-10T15:46:43.7582358Z    Release Notes path         = '/home/vsts/work/1/s/output/ReleaseNotes.md'
2023-01-10T15:46:43.7586043Z    Built Module Subdirectory  = '/home/vsts/work/1/s/output/'
2023-01-10T15:46:43.7595087Z    Module Manifest Path (src) = '/home/vsts/work/1/s/source/ConfigMgrCBDsc.psd1'
2023-01-10T15:46:43.7622945Z    Versioned Output Directory = 'True'
2023-01-10T15:46:43.7687382Z    Built Module Manifest      = '/home/vsts/work/1/s/output/ConfigMgrCBDsc/3.1.0/ConfigMgrCBDsc.psd1'
2023-01-10T15:46:43.7710625Z    Built Module Base          = '/home/vsts/work/1/s/output/ConfigMgrCBDsc/3.1.0'
2023-01-10T15:46:43.7795128Z    Module Version             = '3.1.0-preview0003'
2023-01-10T15:46:43.7801002Z    Module Version Folder      = '3.1.0'
2023-01-10T15:46:43.7807419Z    Pre-release Tag            = 'preview0003'
2023-01-10T15:46:43.7847842Z    Built Module Root Script   = ''
2023-01-10T15:46:43.7848022Z 
2023-01-10T15:46:43.7860075Z    Built DSC Resource Path  = '/home/vsts/work/1/s/output/ConfigMgrCBDsc/3.1.0/DSCResources'
2023-01-10T15:46:43.7863135Z Looking for MOF-based DSC Resources in '/home/vsts/work/1/s/output/ConfigMgrCBDsc/3.1.0/DSCResources'
2023-01-10T15:46:44.0582609Z   Adding 'CMAccounts, CMAdministrativeUser, CMAssetIntelligencePoint, CMBoundaries, CMBoundaryGroups, CMClientPushSettings, CMClientSettings, CMClientSettingsBits, CMClientSettingsClientCache, CMClientSettingsClientPolicy, CMClientSettingsCloudService, CMClientSettingsCompliance, CMClientSettingsComputerAgent, CMClientSettingsComputerRestart, CMClientSettingsDelivery, CMClientSettingsHardware, CMClientSettingsMetered, CMClientSettingsPower, CMClientSettingsRemoteTools, CMClientSettingsSoftwareCenter, CMClientSettingsSoftwareDeployment, CMClientSettingsSoftwareInventory, CMClientSettingsSoftwareMetering, CMClientSettingsSoftwareUpdate, CMClientSettingsStateMessaging, CMClientSettingsUserDeviceAffinity, CMClientStatusSettings, CMCollectionMembershipEvaluationComponent, CMCollections, CMDistributionGroup, CMDistributionPoint, CMDistributionPointGroupMembers, CMEmailNotificationComponent, CMFallbackStatusPoint, CMFileReplication, CMForestDiscovery, CMGroupDiscovery, CMHeartbeatDiscovery, CMHierarchySetting, CMIniFile, CMMaintenanceWindows, CMManagementPoint, CMNetworkDiscovery, CMPullDistributionPoint, CMPxeDistributionPoint, CMReportingServicePoint, CMSecurityRoles, CMSecurityScopes, CMServiceConnectionPoint, CMSiteConfiguration, CMSiteMaintenance, CMSiteSystemServer, CMSoftwareDistributionComponent, CMSoftwareUpdatePoint, CMSoftwareUpdatePointComponent, CMStatusReportingComponent, CMSystemDiscovery, CMUserDiscovery' to the list of DscResource will be write in module manifest.
2023-01-10T15:46:44.0589040Z Looking for DSC Composite Resources in '/home/vsts/work/1/s/output/ConfigMgrCBDsc/3.1.0/DSCResources'
2023-01-10T15:46:44.1196259Z ERROR: It is expected to find only 1 configuration in the file '/home/vsts/work/1/s/output/ConfigMgrCBDsc/3.1.0/DSCResources/xSccmInstall/xSccmInstall.schema.psm1' but found 0
2023-01-10T15:46:44.1197914Z At /home/vsts/work/1/s/output/RequiredModules/Sampler/0.116.1/tasks/Build-Module.ModuleBuilder.build.ps1:399 char:62
2023-01-10T15:46:44.1199003Z + ā€¦ urcesNames = $mofPath.FullName | Get-Psm1SchemaName -ErrorAction Stop
2023-01-10T15:46:44.1200143Z +                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2023-01-10T15:46:44.1205889Z At /home/vsts/work/1/s/output/RequiredModules/Sampler/0.116.1/tasks/Build-Module.ModuleBuilder.build.ps1:327 char:1
2023-01-10T15:46:44.1207016Z + Task Build_DscResourcesToExport_ModuleBuilder {
2023-01-10T15:46:44.1207917Z + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2023-01-10T15:46:44.1367341Z Build FAILED. 6 tasks, 1 errors, 0 warnings 00:00:02.9277358
2023-01-10T15:46:44.1999709Z Get-Psm1SchemaName: /home/vsts/work/1/s/output/RequiredModules/Sampler/0.116.1/tasks/Build-Module.ModuleBuilder.build.ps1:399
2023-01-10T15:46:44.2000511Z Line |
2023-01-10T15:46:44.2001216Z  399 |  ā€¦ urcesNames = $mofPath.FullName | Get-Psm1SchemaName -ErrorAction Stop
2023-01-10T15:46:44.2002006Z      |                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2023-01-10T15:46:44.2002795Z      | It is expected to find only 1 configuration in the file
2023-01-10T15:46:44.2003712Z      | '/home/vsts/work/1/s/output/ConfigMgrCBDsc/3.1.0/DSCResources/xSccmInstall/xSccmInstall.schema.psm1' but found 0
2023-01-10T15:46:44.2004334Z 
2023-01-10T15:46:44.2954842Z ##[error]PowerShell exited with code '1'.
2023-01-10T15:46:44.2988300Z ##[section]Finishing: Build & Package Module

How to reproduce

Run build on https://github.com/dsccommunity/ConfigMgrCBDsc with Sampler v0.116.1.

Fails on:

<#
    .SYNOPSIS
        A DSC composite resource to install SCCM.

    .PARAMETER SetupExePath
        Specifies the path to the setup.exe for SCCM.

    .PARAMETER IniFile
        Specifies the path of the ini file, to include the filename.

    .PARAMETER SccmServerType
        Specifies the SCCM Server type install, CAS or Primary.

    .PARAMETER SccmInstallAccount
        Specifies the credentials to use for the SCCM install.

    .PARAMETER Version
        Specifies the version of SCCM that will be installed.
#>
Configuration xSCCMInstall
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [System.String]
        $SetupExePath,

        [Parameter(Mandatory = $true)]
        [System.String]
        $IniFile,

        [Parameter(Mandatory = $true)]
        [ValidateSet('CAS', 'Primary')]
        [System.String]
        $SccmServerType,

        [Parameter(Mandatory = $true)]
        [System.Management.Automation.PSCredential]
        $SccmInstallAccount,

        [Parameter(Mandatory = $true)]
        [ValidateSet('1902', '1906', '1910', '2002', '2006', '2010')]
        [UInt32]
        $Version
    )

    Import-DscResource -ModuleName PSDesiredStateConfiguration

    if ($Version -lt 1910)
    {
        $prefix = 'System Center'
    }
    else
    {
        $prefix = 'Microsoft Endpoint'
    }

    if ($SccmServerType -eq 'CAS')
    {
        $productName = "$prefix Configuration Manager Central Administration Site Setup"
    }
    if ($SccmServerType -eq 'Primary')
    {
        $productName = "$prefix Configuration Manager Primary Site Setup"
    }

    Package SCCM
    {
        Ensure               = 'Present'
        Path                 = "$SetupExePath\Setup.exe"
        Name                 = $productName
        ProductId            = ''
        Arguments            = "/SCRIPT $IniFile"
        DependsOn            = $dependsOnChain
        PsDscRunAsCredential = $SccmInstallAccount
    }
}

Expected behavior

Build without failing.

Current behavior

Throws exception

ERROR: It is expected to find only 1 configuration in the file '/home/vsts/work/1/s/output/ConfigMgrCBDsc/3.1.0/DSCResources/xSccmInstall/xSccmInstall.schema.psm1' but found 0
At /home/vsts/work/1/s/output/RequiredModules/Sampler/0.116.1/tasks/Build-Module.ModuleBuilder.build.ps1:399 char:62

Suggested solution

Verify that the public command works as expected, and throw if there is an issue using it cross-platform.

Operating system the target node is running

Ubuntu 20.04.5 LTS
macOS

not tested on Windows.

PowerShell version and build the target node is running

Ubuntu: unknown
macOS: 7.3.1

Module version used

v0.116.1
johlju commented 1 year ago

This is a bug on Linux and macOS. It works on Windows. So the command should probably fail if it can't parse configurations on a specifc OS.

raandree commented 1 year ago

The issue happens in Get-Psm1SchemaName when calling $ast.FindAll. The parser fails with this error:

System.IO.DirectoryNotFoundException: Unable to find DSC schema store at /etc/opt/omi/conf/dsc/configuration. Please ensure PS DSC for Linux is installed.

If seems to be a well known issue since quite a while: https://github.com/PowerShell/PowerShell/issues/10782.

johlju commented 1 year ago

Yes it seems that the build worker "ubuntu 20.04" in Azure Pipelines change so that in no longer can parse the configurations. Guessing probably because PS was updated. Quickest solution is to move build phase to run on vm image "windows-latest".

johlju commented 1 year ago

But I think we should catch and throw the parse error so it's clear why it fails to find a configuration statement šŸ¤”

raandree commented 1 year ago

Right, happy to take care of it.

johlju commented 1 year ago

@raandree great! I review as soon I see the PR is there. šŸ™‚