Skip to content

Commit 463ef43

Browse files
authored
Fix the dot-sourcing behavior of pwsh -file for advanced-function scripts (PowerShell#27727)
1 parent db5f4f3 commit 463ef43

2 files changed

Lines changed: 5 additions & 1 deletion

File tree

src/System.Management.Automation/engine/CommandProcessor.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,9 @@ private void Init(IScriptCommandInfo scriptCommandInfo)
778778
InitCommon();
779779

780780
// If the script has been dotted, throw an error if it's from a different language mode.
781-
if (!this.UseLocalScope)
781+
// Unless it was a script loaded through -File, in which case the danger of dotting other
782+
// language modes (getting internal functions in the user's state) isn't a danger.
783+
if (!this.UseLocalScope && !scriptCmdlet.ShouldRethrowExitException)
782784
{
783785
ValidateCompatibleLanguageMode(scriptCommandInfo.ScriptBlock, _context, Command.MyInvocation);
784786
}

src/System.Management.Automation/engine/runtime/CompiledScriptBlock.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,6 +2191,8 @@ internal sealed class PSScriptCmdlet : PSCmdlet, IDynamicParameters, IDisposable
21912191
private bool _exitWasCalled;
21922192
private bool _anyClauseExecuted;
21932193

2194+
internal bool ShouldRethrowExitException => _rethrowExitException;
2195+
21942196
public PSScriptCmdlet(ScriptBlock scriptBlock, bool useNewScope, bool fromScriptFile, ExecutionContext context)
21952197
{
21962198
_scriptBlock = scriptBlock;

0 commit comments

Comments
 (0)