Skip to content

Keep the module's state in module scope, not global - #19

Merged
Sekers merged 2 commits into
developfrom
feature/module-scope-variables
Sep 24, 2026
Merged

Sekers merged 2 commits into
developfrom
feature/module-scope-variables

Conversation

@Sekers

@Sekers Sekers commented Sep 23, 2026

Copy link
Copy Markdown
Owner

Summary

  • The module's state is no longer global. Set-ScriptMessageConfigFilePath and the cached service context kept their state in global variables ($ScriptMessage_Global_ConfigFilePath and $ScriptMessage_Global_CachedServiceContext), so any script could read or change them, and they outlived Remove-Module. They are now module-scoped variables.
  • New parameter: Get-ScriptMessageConfig -ReturnConfigFilePath adds a ConfigFilePath property holding the full path of the configuration file that was read. It is the supported way to read the path.
  • Importing the module again clears the path. After Import-Module -Force, or removing and importing the module, call Set-ScriptMessageConfigFilePath again.
  • The error for a missing path now names the cmdlet to run rather than a variable.
  • RELEASING.md now says that correcting an unintended side effect of how the module is built is not a breaking change, and AGENTS.md points its BREAKING CHANGE: rule at that definition. That is why removing the global variables is not breaking: in 1.1.1 they appear only in code and in an error message, never in the help, README, changelog, or wiki.
  • Research_Notes/PowerShell-Language-Behavior.md section 12 records the measurements of what module scope survives.
  • A new test checks that importing the module and setting the path create no global variables.
  • The changelog's version separators get the blank line markdownlint's MD022 rule wants, and AGENTS.md's example shows it.

Changelog: the New Parameter entry under Added, and the entry under Changed.

Release classification

  • Major: breaks the public contract (an existing script or configuration file can stop working)
  • Minor: adds backward-compatible functionality
  • Patch: a backward-compatible fix
  • None: no user-visible change (tests, CI, or contributor documentation only)

Checklist

  • Targets develop, or main for a hotfix
  • CHANGELOG.md has an entry in the right section under [Unreleased] (added if missing), or the change is not user-visible
  • .\Tests\Invoke-Tests.ps1 passes
  • Contains no real tenant names, email domains, secrets, or configuration files
  • Any live Microsoft Graph testing was done in a test tenant and is described above (none was needed)

An unintended side effect of how the module is built, such as state it
kept in a global variable where any script could read it, is not
functionality the module ever offered. Correcting one is not a breaking
change and needs no deprecation, so say that in the contract's
definition and in Deciding hard cases. AGENTS.md now points at that
definition for when a changelog entry is a BREAKING CHANGE.
Set-ScriptMessageConfigFilePath and the cached service context kept
their state in global variables, so any script could read or change it,
and it outlived Remove-Module. Both now live in $script: variables that
the .psm1 declares, and VariablesToExport is an empty list.

Callers get a supported way to read the path instead:
Get-ScriptMessageConfig -ReturnConfigFilePath adds a ConfigFilePath
property holding the full path of the file it read. The error for a
missing path names the cmdlet to run rather than a variable.

Importing the module again with -Force now clears the path, which the
changelog records. Research note section 12 has the measurements of
what module scope survives. The changelog's version separators also
get the blank line MD022 wants, and AGENTS.md shows it in its example.
@Sekers
Sekers merged commit 67ce17c into develop Sep 24, 2026
4 checks passed
@Sekers
Sekers deleted the feature/module-scope-variables branch September 24, 2026 01:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant