PowerShell / PowerShell/PSScriptAnalyzer

Rule Proposal: Warn on dot-sourced function

Open
#696 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue - New Rule
Dominant language
C#
Stars
2.2k
Forks
414
Avg merge
13h 1m
Merged PRs (30d)
2

Description

I've inherited a project where everything is in, for lack of a better description, undeclared global scope, spread across several files. Functions don't have always have parameters, and are called with a dot before the function name. Variables are treated as global, though not declared. For example:

c:\Utils.ps1

function Write-MyLog {
$itemToLog | out-file c:\mylog.txt -append
}


c:\Interface.ps1

. c:\Utils.ps1

$itemToLog = "Something happened"
. Write-MyLog


Miraculously, this all works. Of course, I dare not change anything.

I would love it if ". Write-MyLog" came up as a warning, and even more so if there were some indication that "$itemToLog" in C:\Interface.ps1 was being used by the function called from c:\Utils.ps1.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the behavior in the example c:\Utils.ps1 and c:\Interface.ps1 scripts, focusing on the dot-sourced function call and the cross-file $itemToLog variable. Done means the analyzer warns on the requested construct and provides the requested indication of variable usage, with the scope and expected diagnostics agreed.

Written by the indexing model from the issue text.

Assessment

Tech stack
powershell
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.