Skip to content

Test-ModuleManifest should check the manifest / root-module file base name for naming consistency with the enclosing directory #8391

@mklement0

Description

@mklement0

For a module to be picked up merely by the name of its containing directory, its manifest must have the same name as that directory; e.g., a module directory foo must contain a manifest named foo.psd1.

Auto-loading and discovery of not-yet-loaded modules relies on this convention.

There are two pitfalls, both of which Test-ModuleManifest should detect, but currently doesn't:

  • (a) Your *.psd1 file's base name may differ from the enclosing directory name (assuming that your root *.psm1 file doesn't match the name either), so loading the module by its directory name / path will fail.

  • (b) A *.psm1 file that matches the enclosing directory name may be present, but the *.psd1 file may have a different name, in which case loading the module by directory name will use the *.psm1 directly, bypassing the manifest altogether.

Steps to reproduce

Scenario (a)

# Create a manifest in subdir. "tmpMod1", but with the wrong name.
New-ModuleManifest (Join-Path (New-Item -Force -Type Directory tmpMod1) wrongName.psd1) -RootModule wrongName.psm1 -function foo
# Create the associated *.psm1 file with the same wrong name.
'function foo { "foo" }' > tmpMod1/wrongName.psm1

Test-ModuleManifest tmpMod1/wrongName.psd1

Scenario (b)

# Create a manifest in subdir. "tmpMod2", but with the wrong name, but pointing
# to a *.psm1 file with the RIGHT name.
New-ModuleManifest (Join-Path (New-Item -Force -Type Directory tmpMod2) wrongName.psd1) -RootModule tmpMod2.psm1 -function foo
# Create the associated *.psm1 file with the RIGHT Name
'function foo { "foo2" }' > tmpMod2/tmpMod2.psm1

Test-ModuleManifest tmpMod2/wrongName.psd1 -vb

Expected behavior

Scenario (a)

Test-ModuleManifest should report an error that the *.psd1 file's base name doesn't match the name of the enclosing directory.

Scenario (b)

Test-ModuleManifest should report an error or at least warn that no *.psd1 file (manifest) with the expected name was found, only a *.psm1 file.

Actual behavior

Scenario (a)

Test-ModuleManifest succeeds.

Scenario (b)

Test-ModuleManifest succeeds, and the verbose output indicates that it was the *.psm1 file rather than the manifest (*.psd1) that was imported (and would be with Import-Module).

Environment data

PowerShell Core v6.2.0-preview.2 (6.2.0-preview.2) on Ubuntu 18.04.1 LTS

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugResolution-No ActivityIssue has had no activity for 6 months or moreWG-Cmdlets-Corecmdlets in the Microsoft.PowerShell.Core module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions