PoshCode / ModuleBuilder

A PowerShell Module to help scripters write, version, sign, package, and publish.
MIT License
445 stars 54 forks source link

The MoveUsingStatements feature doesn't work for using namespace #96

Closed Jaykul closed 4 years ago

Jaykul commented 4 years ago

If I have this, for instance, in a file in classes:

using namespace System.Collections.Generic

class Resolver {
    [Dictionary[string, PSObject]]$Cache = [Dictionary[string, PSObject]]::new()
}

Then if we get the error because the using statement isn't the first thing in the new module, we will always(?) also get a TypeNotFound error about the Dictionary, which will prevent the using statement from being removed.

I thought we already fixed this?

gaelcolas commented 4 years ago

This one wasn't fixed, but I've noticed this and a few other issues. In short, I think we should always try to move using statements if we find UsingMustBeAtStartOfScript error, because if the module is invalid, it sould be caught just after being built.

There are Parsing errors that PowerShell don't mind, and still happily build the modules. There are other types of errors that we'll always have during parsing, like some type not found, because the type is not loaded (maybe in module manifest, or in code).

That happens as well with class attributes, like the YamlDotNet [YamlIgnore()] attribute...

All in all, now I suggest to just move the using statements anyway as soon as we find a UsingMustBeAtStartOfScript.

If anything goes wrong after that, it may or may not be our fault, but for sure if the using is not at the right place it will most likely fail.

What do you think?

Jaykul commented 4 years ago

I guess I can live with that.

Honestly, the reason we're checking is that if the module is busted, we don't want to make it worse (or more confusing to troubleshoot) -- but as you say, right now it means some modules won't build at all. I'll have to think about adding a -IgnoreUsingStatements switch so people can disable it if it's causing them problems.