惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
爱范儿
爱范儿
博客园 - 三生石上(FineUI控件)
Microsoft Security Blog
Microsoft Security Blog
Google DeepMind News
Google DeepMind News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
GbyAI
GbyAI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
V2EX
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
博客园_首页
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
A
About on SuperTechFans
T
The Blog of Author Tim Ferriss
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
P
Proofpoint News Feed
博客园 - 司徒正美
有赞技术团队
有赞技术团队
Engineering at Meta
Engineering at Meta
Last Week in AI
Last Week in AI
MongoDB | Blog
MongoDB | Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
How PSScriptBuilder Analyzes PowerShell Dependencies — an...
Tim Hartling · 2026-04-30 · via DEV Community

If you've read the first article in this series, you already know that PSScriptBuilder resolves PowerShell class load order automatically. This article goes one level deeper: what exactly does PSScriptBuilder analyze, how does it build a dependency graph, and what happens when that graph contains a cycle?

What PSScriptBuilder Extracts

When PSScriptBuilder processes your source files, it runs AST analysis on every collected component and extracts the following dependency information:

Component Dependencies extracted
Class Base class, type references (properties, method parameters), static initializer references, called functions
Function Called functions, type references
Enum None — enums have no outgoing dependencies and always appear first

Built-in types (string, int, bool, System.*, etc.) are automatically excluded — only types that are part of your project create graph edges.

The Dependency Graph

The extracted dependencies form a directed graph where each node is a component and each edge represents a dependency. An edge from Employee to Person means: Employee depends on Person, so Person must appear first in the output.

Consider this example:

class Employee : Person {
    [Address] $Address
    [decimal] $Salary
}

Enter fullscreen mode Exit fullscreen mode

PSScriptBuilder extracts two edges from this class:

  • Employee --[inherits]--> Person
  • Employee --[type reference]--> Address

Both Person and Address must appear before Employee in the output. PSScriptBuilder applies topological sorting to the full graph and determines the correct order for all components automatically — regardless of file or directory order.

You can inspect the dependency graph at any time:

$contentCollector = New-PSScriptBuilderContentCollector |
    Add-PSScriptBuilderCollector -Type Class -IncludePath "src/Classes"

$analysis = Get-PSScriptBuilderDependencyAnalysis -ContentCollector $contentCollector

Write-Host "Components : $($analysis.TotalComponents)"
Write-Host "Has cycles : $($analysis.HasCycles)"

Enter fullscreen mode Exit fullscreen mode

Not All Cycles Are Equal

PSScriptBuilder distinguishes two fundamentally different types of cycles — and only one of them is a problem.

Fatal Cycles

A cycle is fatal when it creates an unresolvable ordering constraint at PowerShell load time. PowerShell processes type definitions from top to bottom before any code runs. Two dependency types create this hard constraint:

Inheritance — a base class must be fully defined before any class that inherits from it can be loaded. If two classes inherit from each other — directly or through a chain — there is no valid order:

class ServiceA : ServiceB { }  # requires ServiceB first
class ServiceB : ServiceC { }  # requires ServiceC first
class ServiceC : ServiceA { }  # requires ServiceA first — impossible

Enter fullscreen mode Exit fullscreen mode

Static property initializers — static initializers run immediately when a class is loaded, not when a method is called. A type referenced in a static initializer must already be defined at load time — the same hard requirement as inheritance:

class ClassA {
    static [ClassB] $Default = [ClassB]::new()  # runs when ClassA loads
}

class ClassB {
    static [ClassA] $Default = [ClassA]::new()  # runs when ClassB loads
}

Enter fullscreen mode Exit fullscreen mode

Neither class can be loaded first. PSScriptBuilder catches both cycle types before writing any output.

Type Reference Cycles — Not Fatal

A type reference cycle occurs when two classes reference each other inside method bodies:

class ClassA {
    [void] Process() { $b = [ClassB]::new() }
}

class ClassB {
    [void] Process() { $a = [ClassA]::new() }
}

Enter fullscreen mode Exit fullscreen mode

This looks like a cycle — but it isn't a problem. Method bodies are not executed when the class is loaded; they only run when the method is called. By that time, all class definitions in the script have already been loaded. PSScriptBuilder recognizes this and handles it automatically — no error, no manual intervention required.

When a Fatal Cycle Is Detected

When the $contentCollector points at classes that form a fatal cycle, Get-PSScriptBuilderDependencyAnalysis reports it immediately:

$analysis = Get-PSScriptBuilderDependencyAnalysis -ContentCollector $contentCollector

if ($analysis.HasCycles) {
    Write-Host "Cycle path: $($analysis.CyclePath -join ' -> ')"
}

Enter fullscreen mode Exit fullscreen mode

Output:

Cycle path: ServiceA -> ServiceB -> ServiceC -> ServiceA

Enter fullscreen mode Exit fullscreen mode

If you call Invoke-PSScriptBuilderBuild directly, it fails fast with the same information:

Build failed: Circular dependency detected: ServiceA -> ServiceB -> ServiceC -> ServiceA

Enter fullscreen mode Exit fullscreen mode

The full cycle path tells you exactly where to look. A fatal cycle always represents a structural problem in the code — PSScriptBuilder surfaces it clearly so you can fix it at the source.

Whether your project has only a few components or over a hundred — like PSScriptBuilder itself — PSScriptBuilder figures out the correct order and tells you immediately when a cycle makes that impossible.

For the full reference, see the Dependency Analysis Guide.

Get Started

Install-Module -Name PSScriptBuilder

Enter fullscreen mode Exit fullscreen mode

New to PSScriptBuilder? Start here: Stop Manually Sorting PowerShell Class Files — PSScriptBuilder Does It For You


Have you run into dependency issues in your PowerShell projects? I'd be curious to hear how you've been handling them — or whether PSScriptBuilder covers your use case.