Skip to content

Commit 0f9c85c

Browse files
docs: add documentation for declaring module dependencies using #Requires statements
Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
1 parent fc86f85 commit 0f9c85c

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

README.md

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ How the module is built.
919919
│ ├── formats/ # Formatting metadata registered during build
920920
│ │ ├── CultureInfo.Format.ps1xml # Example format included in manifest
921921
│ │ └── Mygciview.Format.ps1xml # Additional format loaded at import
922-
│ ├── functions/ # Function scripts exported by the module
922+
│ ├── functions/ # Function scripts exported by the module; #Requires -Modules statements here are compiled into RequiredModules
923923
│ │ ├── private/ # Helper functions scoped to the module
924924
│ │ │ ├── Get-InternalPSModule.ps1 # Sample internal helper
925925
│ │ │ └── Set-InternalPSModule.ps1 # Sample internal helper
@@ -949,10 +949,58 @@ How the module is built.
949949
│ │ └── SolarSystems.ps1 # Example variable surfaced in generated docs
950950
│ ├── finally.ps1 # Cleanup script appended to the root module
951951
│ ├── header.ps1 # Optional header injected at the top of the module
952-
│ ├── manifest.psd1 (optional) # Source manifest reused when present
952+
│ ├── manifest.psd1 (optional) # Source manifest reused when present; RequiredModules is not propagated — use #Requires -Modules in function files instead (see "Declaring module dependencies" below)
953953
│ └── README.md # Module-level docs ingested by Document-PSModule
954954
```
955955

956+
### Declaring module dependencies
957+
958+
Module dependencies are declared using [`#Requires -Modules`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_requires)
959+
statements at the top of individual PowerShell function files under `src/functions/public/` and `src/functions/private/`.
960+
The build system ([Build-PSModule](https://github.com/PSModule/Build-PSModule)) collects all `#Requires -Modules`
961+
declarations across every source file, de-duplicates them, and writes the merged result into the `RequiredModules` field
962+
of the compiled module manifest.
963+
964+
> [!IMPORTANT]
965+
> Adding `RequiredModules` to `src/manifest.psd1` is **not** supported for this purpose. Those entries are silently
966+
> ignored by the build — they will not appear in the built manifest. Use `#Requires -Modules` in function files instead.
967+
968+
#### Supported syntax variants
969+
970+
```powershell
971+
# Bare module name — any version is acceptable
972+
#Requires -Modules Microsoft.Graph.Authentication
973+
974+
# Minimum version constraint
975+
#Requires -Modules @{ ModuleName = 'Microsoft.Graph.Authentication'; ModuleVersion = '2.28.0' }
976+
977+
# Exact version constraint
978+
#Requires -Modules @{ ModuleName = 'PSSemVer'; RequiredVersion = '1.1.4' }
979+
```
980+
981+
#### Example
982+
983+
A function that calls into `Microsoft.Graph.Authentication` declares its dependency at the top of the file:
984+
985+
```powershell
986+
#Requires -Modules @{ ModuleName = 'Microsoft.Graph.Authentication'; ModuleVersion = '2.28.0' }
987+
988+
function Get-GraphToken {
989+
...
990+
}
991+
```
992+
993+
When the module is built, the compiled manifest automatically includes:
994+
995+
```powershell
996+
RequiredModules = @(
997+
@{ ModuleName = 'Microsoft.Graph.Authentication'; ModuleVersion = '2.28.0' }
998+
)
999+
```
1000+
1001+
This co-location approach keeps dependency declarations next to the functions that need them, making it immediately
1002+
clear which commands drive each external dependency.
1003+
9561004
## Principles and practices
9571005

9581006
The contribution and release process is based on the idea that a PR is a release, and we only maintain a single linear ancestry of versions, not going

0 commit comments

Comments
 (0)