Skip to content

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

Closed

Description

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

Activity

  1. added
    WG-Language
    on Jun 27, 2017
  2. SteveL-MSFT commented on Jun 27, 2017

    @SteveL-MSFT
    Member

    I believe this was part of a perf optimization Jason Shirk (@lzybkr) made to avoid parsing scripts. We need a TypesToExport to continue to enforce explicit exports and maintain perf

  3. added this to the milestone on Jun 27, 2017
  4. lzybkr commented on Jun 27, 2017

    @lzybkr
    Contributor

    Actually this was a design issue - the intent was to not require specifying exported types in the manifest at all because, well, it's annoying.

    I didn't implement this aspect of classes, but I thought we intended to compute exported types on demand, and cache the results to avoid redundant parsing.

  5. iSazonov commented on Jun 28, 2017

    @iSazonov
    Collaborator

    Was it a breaking change? Was it ported to Windows PowerShell? If so and it was not documented we should mark the Issue as breaking change and enhancement.

  6. SteveL-MSFT commented on Jun 28, 2017

    @SteveL-MSFT
    Member

    Ilya (@iSazonov) I believe the changes being discussed happened during Windows PowerShell and ended up in PSCore6

  7. mklement0 commented on Jul 14, 2018

    @mklement0
    Contributor

    Since it may not be obvious, let me point out that the problem only surfaces if you use the NestedModules entry to reference the *.psm1 file.

    In the given scenario it would be more typical to use the RootModule entry, however, in which case everything works as expected.

  8. Jaykul commented on Dec 5, 2018

    @Jaykul
    Contributor

    Isn't the root cause that only classes in the RootModule are supposed to be available.
    I don't think this is a separate bug. It's either a design choice, or a duplicate of #2964

  9. 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.

  10. 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.

  11. added
    Resolution-No Activity
    on Nov 16, 2023
  12. 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.

  13. 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

    Resolution-No ActivityWG-Language

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions