Skip to content

Test-ModuleManifest working sporadically on v6.1.0-preview.2 #6818

Description

If it is a bug report:


Steps to reproduce

PS > pwsh
PS > Test-ModuleManifest -Path /reposDir/MyModule/Builds/BuildOutput/Mymodule/MyModule.psd1 -Verbose

N.B. This is a sporadic issue.

  • As I have been troubleshooting this I worked on it in the morning. Then when I come home, my laptop hibernating, the exact same command might work again.
  • In VS Code I have setup a build task which basically copies the necessary module files, leaving .gitignore, logs and other files I don't want included when publishing the module. If I run the Test-ModuleManifest cmdlet against the files that was written via the "build" task. It might fail. However, if I run Test-ModuleManifest against the .psd1 file in the root of the repo it might work. And vice versa.
  • I have read > https://msdn.microsoft.com/en-us/library/dd878337(v=vs.85).aspx ...
  • I compared my manifest against the manifest of public and solid modules such as Pester and Plaster. I cannot see any issues with my manifest. And! As I've stated, sometimes Test-ModuleManifest works on the MyModule.psd1 manifest. Without me chaning anything in the MyModule.psd1 file.
  • I've tried to execute Import-Module on both the MyModule.psm1 and the MyModule.psd1 files and they successfully import.
  • For good meassure here is the manifest of my module. Which Test-ModuleManifest sometimes an test successfully and sometimes not.
@{

    # Script module or binary module file associated with this manifest.
    RootModule = 'MyModule.psm1'

    # ID used to uniquely identify this module
    GUID = 'c1e187cd-222f-4df6-8238-88f90f0284ff'

    # Version number of this module.
    ModuleVersion = '0.0.12'

    # Author of this module
    Author = 'https://github.com/larssb | https://bengtssondd.it'

    # Company or vendor of this module
    CompanyName = 'Bengtsson Driven Development'

    # Copyright statement for this module
    Copyright = '(C) https://github.com/larssb | https://bengtssondd.it. All rights reserved.'

    # Description of the functionality provided by this module
    Description = 'A project containing helper functions and other utilities to make your life programming PowerShell easier.'

    # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
    FunctionsToExport = @(
        'Add-ScheduledTask'
        'get-environmentOS'
        'get-hostname'
        'Get-PSProgramFilesModulesPath'
        'Get-PublicFunctions'
        'Get-xScheduledTask'
        'initialize-log4net'
        'New-ScheduledJob'
        'out-functionHierarchy'
        'Set-xScheduledTask'
        'start-externalSoftware'
        'test-powershellRunMode'
        'import-jsonFile'
        )

    # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
    CmdletsToExport = @()

    # Variables to export from this module
    #VariablesToExport = '*'

    # Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
    AliasesToExport = @()

    # Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
    PrivateData = @{

        PSData = @{

            # Tags applied to this module. These help with module discovery in online galleries.
            # Tags = @()

            # A URL to the license for this module.
            # LicenseUri = ''

            # A URL to the main website for this project.
            # ProjectUri = ''

            # A URL to an icon representing this module.
            # IconUri = ''

            # ReleaseNotes of this module
            # ReleaseNotes = ''

            # Flag to indicate whether the module requires explicit user acceptance for install/update/save
            # RequireLicenseAcceptance = False

            # External dependent modules of this module
            # ExternalModuleDependencies = ''

        }
    }
}

Expected behavior

That Test-ModuleManifest does not throw (as long as you really have a valid manifest file)

Actual behavior

Throws: Test-ModuleManifest : The module manifest 'MyModule' could not be processed because it is not a valid PowerShell module manifest file. Remove the elements that are not permitted: /reposDir/MyModule/Builds/BuildOutput/Mymodule/MyModule.psd1
At line:1 char:1
+ Test-ModuleManifest -Path /MyModule/Builds/BuildOutput/Mymod ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (/MyModule/MyM...d.psd1:String) [Test-ModuleManifest], ArgumentException
+ FullyQualifiedErrorId : Modules_InvalidRootModuleInModuleManifest,Microsoft.PowerShell.Commands.TestModuleManifestCommand

Environment data

OS: MacOS
OS v.: 10.13.5 Beta

PS > pwsh
PS > $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.1.0-preview.2
PSEdition                      Core
GitCommitId                    v6.1.0-preview.2
OS                             Darwin 17.6.0 Darwin Kernel Version 17.6.0: Fri Apr 13 19:57:44 PDT 2018; root:xnu-4570.60.17.0.1~3/RELEASE_X86_64
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Any help is appreciated. Thank you very much.

Activity

  1. iSazonov commented on May 4, 2018

    @iSazonov
    Collaborator
  2. daxian-dbw commented on May 8, 2018

    @daxian-dbw
    Member

    This is not a recent regression. It repros in PS Core 6.0.1.

  3. daxian-dbw commented on May 11, 2018

    @daxian-dbw
    Member

    Test-ModuleManifest : The module manifest 'MyModule.psm1' could not be processed because it is not a valid PowerShell module manifest file.

    Lars Bing Bong. (@larssb) I tried your .psd1 on my machine, and it failed because it Test-ModuleManifest cannot find the root module file MyModule.psm1. The fact that you observe this failure inconsistently might suggest that the MyModule.psm1 file is not deployed by the "build" task when Test-ModuleManifest runs.

    But I do think the behavior of Test-ModuleManifest is not right in this case, see it's result:

    PS:13> Test-ModuleManifest .\test.psd1
    Test-ModuleManifest : The module manifest 'MyModule.psm1' could not be processed because it is not a valid PowerShell module manifest file. Remove the elements that are not permitted: F:\tmp\test.psd1
    At line:1 char:1
    + Test-ModuleManifest .\test.psd1
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (.\test.psd1:String) [Test-ModuleManifest], ArgumentException
    + FullyQualifiedErrorId : Modules_InvalidRootModuleInModuleManifest,Microsoft.PowerShell.Commands.TestModuleManifestCommand
    
    
    ModuleType Version    Name                                ExportedCommands
    ---------- -------    ----                                ----------------
    Binary     0.0.12     test                                {Add-ScheduledTask, get-environmentOS, get-hostname, Get-P...
    

    The error message should be improved to mention it could be the file is not found. And, should that be an terminating error? The returned result incorectly indicates the module is a binary module ...

  4. larssb commented on May 17, 2018

    @larssb
    Author

    Hi Dongbo Wang (@daxian-dbw),

    Thank you for troubleshooting and sorry for the late reply.

    Hmm...:

    1. The *.psm1 file is included in the build output.

    In order to correctly test it don't you have to:

    • Have a folder named as the module. So have the "MyModule.psm1" file in a folder named MyModule. And also have the .psd1 file named MyModule.psd1 as well.

    And agree any error message improvements, and those can be had, would be great.

    Thank you.

  5. larssb commented on Jun 13, 2018

    @larssb
    Author

    Any news on this?

  6. marckassay commented on Jul 11, 2018

    @marckassay

    Dongbo Wang (@daxian-dbw), I'm experiencing similar behavior, if not the same as Lars Bing Bong. (@larssb) has mentioned. Currently I'm working on PowerShellGet #294 issue for Update-ModuleManifest which uses Test-ModuleManifest internally.

    When I'm stepping thru Update-ModuleManifest, I'll introspect the data going into Test-ModuleManifest and it looks good. But the data that it returns is missing or strangely in some cases, reverted to previous values.

    After glancing into Test-ModuleManifest source code several times I have a couple of questions.

    1. What's the need for checking multiple files in the directory of this cmdlet's Path parameter, while it seems to test just one? These comments and if statements.
    2. Also, Update-ModuleManifest performs several IO operations and in doing so creates a temp file (with .psd1 extension) in the same directory as the file to be updated. Is this something that should be considered risky when using Test-ModuleManifest? Because it seems that Test-ModuleManifest is retrieving data perhaps somewhere else in some cases as Lars Bing Bong. (@larssb) and I have noticed. And risky considering my 1st question.
    3. And finally, what's the intention of Import-PowerShellDataFile command? Should that be used instead of Test-ModuleManifest for accessing data? I see in PowerShellGet source code that Test-ModuleManifest is used to access data for its output. Which seems weird considering its verb name, 'Test'.

    Hopefully that is explained clearly, if not please let me know.

  7. jzabroski commented on Sep 9, 2018

    @jzabroski

    I believe I have run into this issue on PowerShell v5 and I don't think it's a recent regression. I think it's been buggy since PowerShell v2.

    The crux of the problem is the C# code inherits from ModuleCmdletsBase class, which is apparently stateful.

    See my bug report in Powershellget here https://github.com/PowerShell/PowerShellGet/issues/321#issuecomment-419614919

    Suffice to say, this could be a major security vulnerability.

  8. jzabroski commented on Sep 12, 2018

    @jzabroski

    How do I test against older versions of PowerShell? I believe I can 100% reproduce this issue, and that it is not at all "sporadic" in nature. The issue is that ModuleCmdletBase caches stuff stupidly, and is poorly written and/or doesn't make sense when you're doing local development on a new module.

  9. iSazonov commented on Sep 13, 2018

    @iSazonov
    Collaborator

    John Zabroski (@jzabroski) Could you please add simple repo steps?

  10. mklement0 commented on Dec 3, 2018

    @mklement0
    Contributor

    Dongbo Wang (@daxian-dbw), Lars Bing Bong. (@larssb): Re improving the error message: please see #6683

  11. mklement0 commented on Dec 3, 2018

    @mklement0
    Contributor

    In addition to the debatable behavior mentioned by Dongbo Wang (@daxian-dbw) - that a module-info object is still output even if the specified manifest is invalid - here's a simple reproduction that shows the unexpected statefulness of Test-ModuleManifest mentioned by John Zabroski (@jzabroski):

    Setup:

    # Create folder 'tmpModule' with a manifest and module of the same name.
    New-ModuleManifest (Join-Path (New-Item -Force -Type Directory tmpMod) tmpMod.psd1) -RootModule tmpMod.psm1 -function foo
    'function foo { "foo" }' > tmpMod/tmpMod.psm1

    At this point, Test-ModuleManifest succeeds.

    Test-ModuleManifest tmpMod/tmpMod.psd1  # OK

    Now, make the RootModule entry invalid and change the version number, and then run Test-ModuleManifest again:

    # Note that a RootModule entry without .psm1 extension is *currently* invalid,
    # even though it shouldn't be - see https://github.com/PowerShell/PowerShell/issues/8388
    # Using the same *base name*, however, is a prerequisite for 
    # making the caching problem show.
    ((Get-Content ./tmpMod/tmpMod.psd1) -replace '^RootModule = .*', 'RootModule = "tmpMod"' -replace '^ModuleVersion = .*', 'ModuleVersion = "0.0.9"') | Out-File  ./tmpMod/tmpMod.psd1

    Now run Test-ModuleManifest again, which should now fail:

     # !! In the same session: succeeds unexpectedly, due to caching.
    Test-ModuleManifest tmpMod/tmpMod.psd1 

    While the output reflects the changed version number, the fact that the RootModule entry is now "invalid" was missed - see #8388

    To verify that the manifest is now indeed invalid, open a new PowerShell session and run the last command again.

  12. jzabroski commented on Dec 3, 2018

    @jzabroski

    Ilya (@iSazonov) I don't need to outline repro steps. Just look at how BAD this code is:

    if (found)
    {
    // Cache the module's exported commands after importing it, or if the -Refresh flag is used on "Get-Module -List"
    if ((module != null) && !module.HadErrorsLoading)
    {
    AnalysisCache.CacheModuleExports(module, Context);
    }
    }

    The ONLY way to clear your AnalysisCache is to restart PowerShell.exe - good luck getting rid of bug reports that PowerShell sucks if people cannot reliably build modules in it. I realize this was done to speed up powershell, but I keep pointing out to Microsoft engineers they are going about improving performance the wrong way.

  13. jzabroski commented on Dec 3, 2018

    @jzabroski

    Michael Klement (@mklement0) Thank you. I humbly suggest I owe you a beer, for simply doing your job correctly.

  14. 12 remaining items

  15. jzabroski commented on Apr 2, 2019

    @jzabroski

    @TheIncorrigible1 DId you read me notes on this issue?

    Basically, the issue is that the Module Analysis cache cannot be fully disabled. The only solution is to continuously close and re-open your terminal by creating new sessions / runspaces.

  16. morsley commented on Apr 2, 2019

    @morsley

    John Zabroski (@jzabroski)
    DId you read me notes on this issue?

    Yes, I read your responses, but the only thing that worked for me was using Get-Module -Refresh.

  17. jzabroski commented on Apr 2, 2019

    @jzabroski

    The only way to clear your module analysis cache is to close your pwsh.exe process. This will terminate the cache, since the cache is in-process and not global to the OS. I'm not sure why Get-Module -Refresh worked for you - are you able to consistently reproduce that behavior?

  18. morsley commented on Apr 3, 2019

    @morsley

    John Zabroski (@jzabroski) Again, didn't work. None of the remediation worked except what I noted. It wasn't consistent that it resolved the issue; I ended up manually changing the call to the cmdlet to use Import-PowerShellDataFile instead.

  19. jzabroski commented on Apr 3, 2019

    @jzabroski

    @TheIncorrigible1 I think I see the nuance - in your case, you've installed a module locally, and you are using Import-Module YourModule -Force to clear the old module from the local disk cache (as opposed to memory cache)? Do I have it right? I think the painful part for me is that -Verbose on these commands is not helpful

  20. iSazonov commented on Apr 4, 2019

    @iSazonov
    Collaborator

    John Zabroski (@jzabroski) Please follow Code of Conduct Be respectful.

  21. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  22. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  23. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  24. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productResolution-No ActivityIssue has had no activity for 6 months or moreWG-Cmdletsgeneral cmdlet issues

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions