Title: This apparently acyclic combination of Import-Module calls results in stack overflow · Issue #12039 · PowerShell/PowerShell · GitHub
Open Graph Title: This apparently acyclic combination of Import-Module calls results in stack overflow · Issue #12039 · PowerShell/PowerShell
X Title: This apparently acyclic combination of Import-Module calls results in stack overflow · Issue #12039 · PowerShell/PowerShell
Description: This came up after #12036 and #12014 while trying to implement the proposed solution for those issues. The solution to those issues will, ostensibly, involve liberal calls to Import-Module within modules during module loading to shadow t...
Open Graph Description: This came up after #12036 and #12014 while trying to implement the proposed solution for those issues. The solution to those issues will, ostensibly, involve liberal calls to Import-Module within m...
X Description: This came up after #12036 and #12014 while trying to implement the proposed solution for those issues. The solution to those issues will, ostensibly, involve liberal calls to Import-Module within m...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/12039
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"This apparently acyclic combination of Import-Module calls results in stack overflow","articleBody":"This came up after #12036 and #12014 while trying to implement [the proposed solution for those issues](https://github.com/PowerShell/PowerShell/issues/12036#issuecomment-595425489). The solution to those issues will, ostensibly, involve liberal calls to `Import-Module` within modules during module loading to shadow the names of commands such that name resolution occurs in module and not global scope. In practice I have found that there are scenarios where such liberal use of `Import-Module` causes stack overflows. Some such stack overflows are the result of straightforward cyclic calls to `Import-Module`. This one is slightly different as it is apparently acyclic. \r\n\r\nOne module affected by this is (a later, internal version of) [StructuredResource](https://github.com/alx9r/StructuredResource). The culprit seems to be [these lines which validate that the DSC resource module imports without error](https://github.com/alx9r/StructuredResource/blob/2efcdfceb4760b500344034e847accad9059d0bf/Functions/tests_.ps1#L194-L198). A DSC resource that requires `StructuredResource` is also tested by and therefore imported by `StructuredResource`. I suspect this is an unusual perhaps inadvisable use case, but I'd still like to understand what is happening so I can conclusively work around the stack overflow.\r\n\r\nThis leaves me with the following questions:\r\n\r\n1. Why isn't this acyclic? Why does this cause a stack overflow?\r\n2. Is there some other way to cross-import without stack overflow?\r\n---\r\n\r\nReferring to the repro below, here is what I would have expected to happen:\r\n\r\n1. `p1` is called which results in imported module `p` then `p1` is invoked\r\n2. `p1` outputs `p1` then imports module `m` into `p1`'s scope\r\n3. import of module `m` includes a call to `Import-Module p` so module `p` is imported a second time, this time into module `m`'s scope\r\n4. the second import of module `p` does not result in any more imports or calls and the whole stack unwinds (*)\r\n\r\n(*) In (4) the call to `Import-Module m` is within the `p1` scriptblock and so should not be invoked by the module import.\r\n\r\nThis is not, however, what happens. Stack overflow seems to occur after (3) but before (4) is reached.\r\n\r\n# Steps to reproduce\r\n\r\nCreate two well-formed modules `m` and `p` in `$Env:PSModulePath`:\r\n\r\n```powershell\r\n# m.psm1\r\nWrite-Host 'import m'\r\nImport-Module p\r\n```\r\n```powershell\r\n# p.psm1\r\nWrite-Host 'import p'\r\nfunction p1 { Write-Host 'p1'; Import-Module m}\r\n```\r\n\r\nInvoke the following:\r\n\r\n```PowerShell\r\np1\r\n```\r\n\r\n# Expected behavior\r\n\r\n```none\r\nimport p\r\np1\r\nimport m\r\nimport p\r\n```\r\n\r\n# Actual behavior\r\n\r\n```none\r\nimport p\r\np1\r\nimport m\r\nStack overflow.\r\n```\r\n\r\n\r\n\r\n# Environment data\r\n\r\n\u003c!-- provide the output of $PSVersionTable --\u003e\r\n\r\n```none\r\n\r\nName Value\r\n---- -----\r\nPSVersion 7.0.0\r\nPSEdition Core\r\nGitCommitId 7.0.0\r\nOS Microsoft Windows 6.3.9600\r\nPlatform Win32NT\r\nPSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}\r\nPSRemotingProtocolVersion 2.3\r\nSerializationVersion 1.1.0.1\r\nWSManStackVersion 3.0\r\n```\r\n","author":{"url":"https://github.com/alx9r","@type":"Person","name":"alx9r"},"datePublished":"2020-03-05T22:04:42.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":6},"url":"https://github.com/12039/PowerShell/issues/12039"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:1b5e136e-f8f8-eab9-8e6d-1b9adedac082 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CEBE:3FCE56:1CC3E39:2A01D81:6A56467C |
| html-safe-nonce | 4abd2e30cfc74e94488e798f89095903ff6b42d9e99d249fcf61bce4948f041b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRUJFOjNGQ0U1NjoxQ0MzRTM5OjJBMDFEODE6NkE1NjQ2N0MiLCJ2aXNpdG9yX2lkIjoiMjI5Nzk5NDQzMDcxMDY5NTU0OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 222cba661c3c14156e69a6c148e680d339ebfc6763ede07e4e24b56db71e1767 |
| hovercard-subject-tag | issue:576559252 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/12039/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7d8c2830ee00915f571b2d4eb9534ebf59020e8a598ae1787ab1e2e7947d827c/PowerShell/PowerShell/issues/12039 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7d8c2830ee00915f571b2d4eb9534ebf59020e8a598ae1787ab1e2e7947d827c/PowerShell/PowerShell/issues/12039 |
| og:image:alt | This came up after #12036 and #12014 while trying to implement the proposed solution for those issues. The solution to those issues will, ostensibly, involve liberal calls to Import-Module within m... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | alx9r |
| hostname | github.com |
| expected-hostname | github.com |
| None | 96d96c6ad1e3234907759a5e634942d2ca0aded8a6ec1693a59d0f2c59c0dac9 |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f0d7fddec181d430daaf48494eb476abffad4d37 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width