如何修复PowerShell模块嵌套限制错误?

huangapple go评论61阅读模式
英文:

How do I Fix PowerShell Module Nesting Limit Error?

问题

在我的脚本执行第一行之前,控制台中出现了以下错误信息:

InvalidOperation: 无法加载模块 'C:\Users\Dave\Documents\WebPublisher\modules\WebPublisherUploader\WebPublisherUploader.psm1',因为已超过模块嵌套限制。模块只能嵌套到10个级别。请评估并更改加载模块的顺序,以防止超过嵌套限制,然后再次运行脚本。

在WebPublisherUploader中,我没有看到循环引用,但是如果我追踪链条足够深,另一个模块在导入它导入的模块中似乎指向了WebPublisherUploader。这会导致错误吗?

如何“更改加载模块的顺序”?有没有我不知道的控制加载顺序的方法?

英文:

I get the following error message in the console before the first line of my script executes:

InvalidOperation: Cannot load the module 'C:\Users\Dave\Documents\WebPublisher\modules\WebPublisherUploader\WebPublisherUploader.psm1' because the module nesting limit has been exceeded. Modules can only be nested to 10 levels. Evaluate and change the order in which you are loading modules to prevent exceeding the nesting limit, and then try running your script again.

There’s no circular reference in the WebPublisherUploader that I can see, though if I trace the chain far enough down, another module imported in a module that it imports seems to point back to WebPublisherUploader. Would this be what causes the error?

And how does one “change the order in which you are loading modules”? Is there a way to control load order that I don’t know about?

答案1

得分: 0

正如我所怀疑的那样,存在循环模块导入的问题,但是导入链非常长,很难看到它们。

结果证明,一旦我确定了问题,解决方案就很简单。问题模块有几个枚举定义,许多其他模块都需要这些定义。所以我简单地创建了一个新模块,名为XxxTypes.psm1,没有导入,并将枚举定义移到那里。

在XxxTypes.psm1中没有导入,就不再有意外创建另一个循环导入链的危险。

问题解决了。哇!

英文:

As I suspected, there were circular module imports going on, but the import chain was so long it was hard to see them.

It turned out the solution was simple once I identified the problem. The problem module had a couple of enum definitions which were needed by many other modules. So I simply created a new module, called XxxTypes.psm1, with no imports, and moved the enum definitions there.

With no imports in XxxTypes.psm1, there’s no longer any danger of accidentally creating another circular import chain.

Problem solved. Whew!

huangapple
  • 本文由 发表于 2023年8月9日 02:52:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定