Skip to content

Import-Module isn't exporting classes #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
adamdriscoll opened this issue Jan 31, 2020 · 7 comments
Closed

Import-Module isn't exporting classes #69

adamdriscoll opened this issue Jan 31, 2020 · 7 comments
Labels

Comments

@adamdriscoll
Copy link
Owner

There are attributes used for certain cmdlets that throw exceptions when imported by Import-Module in PowerShell v5.1

Version: v3-beta1

@lhoff
Copy link

lhoff commented Feb 5, 2020

There are attributes used for certain cmdlets that throw exceptions when imported by Import-Module in PowerShell v5.1

Version: v3-beta1

Would that be the cause of this error?

Thanks!

Cannot find the type for custom attribute 'ValidateURIAttribute'. Make sure that the assembly that contains this
type is loaded.
At C:\Users\User\selenium-powershell\Selenium.psm1:337 char:9
+         [ValidateURIAttribute()]
+         ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: ([ValidateURIAttribute()]:AttributeAst) [], RuntimeException
    + FullyQualifiedErrorId : CustomAttributeTypeNotFound

@adamdriscoll
Copy link
Owner Author

@lhoff
Copy link

lhoff commented Feb 5, 2020

Sorry, that doesn't work for me:

Install-Module : A parameter cannot be found that matches parameter name 'AllowPrerelease'.
At C:\Users\User\selenium-powershell\BugWorkaround.ps1:4 char:29
+     Install-Module Selenium -AllowPrerelease -Force -Scope CurrentUse ...
+                             ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Install-Module], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Install-Module

And the following is thrown whether or not I include AllowPrerelease:

At C:\Users\User\selenium-powershell\SeleniumClasses.ps1:13 char:32
+ ...     if ($arguments -isnot [OpenQA.Selenium.Remote.RemoteWebDriver]) {
+                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [OpenQA.Selenium.Remote.RemoteWebDriver].
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : TypeNotFound

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.17134.858
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17134.858
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

@jhoneill
Copy link
Contributor

jhoneill commented Mar 9, 2020

@lhoff did you ever find a resolution to this ? If you import the psm1 file instead of the PSD1 file you will get missing class errors.

@vexx32
Copy link
Contributor

vexx32 commented Mar 27, 2020

@adamdriscoll typically what I'd recommend doing here is just double up on your attribute class. The PowerShell class resolver is a bit janky across the module boundary, and I've found I often need to define a TypeName class as well as a TypeNameAttribute class (usually I'll just implement one and then have the other inherit it with no changes, behaving a bit like an alias).

class MyClassAttribute : ValidateEnumeratedArgumentsAttribute {
    $Property = 1
}

class MyClass : MyClassAttribute { }

If that's not sufficient and you really do need to import the classes into the global session (would recommend avoiding it if you can, but sometimes you do need to), you can use ScriptsToProcess to define the class and get it into global scope, but you need to be careful so users can remove and reimport your module -- if you don't hedge your bets, they'll be unable to import the module after removing it once, because the classes don't get removed.

# ScriptsToProcess file

switch ($null) {
    ('ClassName' -as [type]) { 
        # this class hasn't been created yet, create it here
    }
    # rinse and repeat for any number of classes throughout the switch statement
}

@adamdriscoll
Copy link
Owner Author

@vexx32 - Thank you for this info! I haven't taken the time to solve this but I'm going to play with this today and see if I can avoid this issue. I'm seeing it in UD all the time now.

@adamdriscoll
Copy link
Owner Author

This did the trick. Seems to work for me in PSv5.1 and PSv7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants