question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
SteveL-MSFTcommented, Jun 28, 2017

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

0reactions
Jaykulcommented, Dec 5, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Statement: Import PowerShell Classes from Modules
I've explicitly exported the function, but Export-ModuleMember doesn't have a Class parameter to export classes. importtester psm1. The ...
Read more >
PowerShell custom module manifest does not expose ...
1 Answer 1 ... FunctionsToExport is like a sieve - it just allows nested modules to export their functions via the manifest, but...
Read more >
How to Write a PowerShell Module Manifest
A module manifest is a PowerShell data file ( .psd1 ) that describes the contents ... To test your module manifest, use Test-ModuleManifest....
Read more >
Using Classes in Modules : r/PowerShell
Now when I load the module I get: test > Import-Module test -Force -Verbose VERBOSE: Loading module from path 'C:\Users\me\PS\Modules\test\ ...
Read more >
Import-Module (Microsoft.PowerShell.Core)
After a module is imported, you can use the module members in your session. ... Import-Module -Name c:\ps-test\modules\test -Verbose VERBOSE: Loading module ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found