Skip to content

using module test doesn't load the powershell class defined in 'test' if 'test.psd1' has 'FunctionToExport' or 'CmdletToExport' specified #4112

@daxian-dbw

Description

@daxian-dbw

Summary

When test.psd1 file has 'FunctionToExport' or 'CmdletToExport' specified, the module script analyzer will skip parsing the module scripts and thus no type definitions would be retrieved back. and PSModuleInfo.GetExportedTypeDefinitions doesn't handle this scenario well.

Repro

Structure of the module 'test'

PS:146> tree.com F:\tmp\test /F
F:\TMP\TEST
    test.psd1
    test.psm1

Content of 'test.psd1'

@{
ModuleVersion = '1.0'
GUID = 'e812e452-6a43-4fe7-9738-375c2fbdd577'
NestedModules = @("test.psm1")
FunctionsToExport = @('Get-Name')
CmdletsToExport = @()
VariablesToExport = '*'
AliasesToExport = @()
}

Content of 'test.psm1'

class Foo
{
    [string] $Name = "Foo"
}

function Get-Name
{
    return "Name"
}

Content of run.ps1

using module F:\tmp\test

class Bar{
    [Foo] $foo = [Foo]::new()

    [void] Print()
    {
        Write-Host ($this.foo.Name)
    }
}

Run run.ps1

.\run.ps1

Expected Behavior

Running run.ps1 should be successful.

Actual Behavior

It failed

PS:14> .\run.ps1
At F:\tmp\run.ps1:4 char:6
+     [Foo] $foo = [Foo]::new()
+      ~~~
Unable to find type [Foo].
At F:\tmp\run.ps1:4 char:19
+     [Foo] $foo = [Foo]::new()
+                   ~~~
Unable to find type [Foo].
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : TypeNotFound

Environment

PS:15> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.0.0-beta
PSEdition                      Core
GitCommitId                    v6.0.0-beta.2-48-g598cebf7b2f0b5bb0b0add97c6393ccfaaa792f5-dirty
OS                             Microsoft Windows 10.0.15063
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution-No ActivityIssue has had no activity for 6 months or moreWG-Languageparser, language semantics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions