Skip to content

A mechanism to allow types in other files to be seen by VS Code #1617

@nathan-alden-hp

Description

@nathan-alden-hp

Summary of the new feature/enhancement

In the programming world, developers are encouraged to follow a "one type per file" rule. I am trying to apply this rule to classes in PowerShell. Unfortunately, I can't find a way to allow my editor (VS Code in my case) to "see" types in other files. Consider this trivial case where I have a module consisting of two classes:

Class1.ps1

class Class1 {
    [void]Method() {}
}

Class2.ps1

class Class2 {
    Class2() {
        $c = [Class1]::new() # VS Code reports "unable to find type [Class1]"
        $c. # Typing the dot does not display any members
    }
}

image

I know why the warning occurs, but this effectively forces me to declare all my types in a single file in order to benefit from VS Code's PowerShell analyzer. That means with a big enough number of types, the single file can easy reach thousands of lines, making it harder to maintain and making it harder to tell via Git diffs what types were changed.

Is there a way around this problem?

Proposed technical implementation details (optional)

I'm not sure what can be done to improve the developer experience, but as it stands now this is preventing me from using classes to their full extent in my more complex PowerShell modules. I know I could always create a full-blown C# project, but that introduces build pipeline and CI dependencies I'm trying to avoid. It also means developers who work on my module need to be more aware of .NET tools like Visual Studio than they otherwise would need to be.

Some kind of special comment used by the analyzer to temporarily import other files for the purposes of parsing, perhaps?

#analyze Class1.ps1

What is the latest version of PSScriptAnalyzer at the point of writing

Not sure, but I'm using the 2020.6.0 version of the VS Code PowerShell extension.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions