Skip to content

A -Depth parameter would be usefull #81

Closed

Description

@x86Since8088

I would like to dump data for human review in YAML, but some objects have seemingly endless recursion. A -Depth parameter would help here.

Exception calling "Serialize" with "2" argument(s): "Too much recursion when traversing the object graph"
At C:\Users\my\Documents\WindowsPowerShell\Modules\powershell-yaml\0.4.2\powershell-yaml.psm1:398 char:13

  •         $serializer.Serialize($wrt, $norm)
    
  •         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    • FullyQualifiedErrorId : InvalidOperationException

Activity

  1. Serpen commented on Mar 16, 2025

    @Serpen

    +1 for that, even standard thing fails quite quick

    gi C:\Windows\ | ConvertTo-Yaml
  2. gabriel-samfira commented on Jan 5, 2026

    @gabriel-samfira
    Member

    So it took less time for Frodo to take the ring to Mount Doom than it took me to get around to adding a -Depth flag, but would you mind giving this PR a try:

    It adds far more than a -Depth flag, but I have no idea when I will have enough time again, so it's a dump of many changes that were requested.

    For this use case:

    PS /home/gabriel/work/cloudbase/powershell-yaml> import-module ./powershell-yaml.psd1   
    PS /home/gabriel/work/cloudbase/powershell-yaml> gi . | ConvertTo-Yaml -Depth 1
    Name: powershell-yaml
    Parent:
      Name: cloudbase
      Parent: {}
      Root: {}
      Exists: true
      FullName: /home/gabriel/work/cloudbase
      Extension: ""
      CreationTime: 2026-01-02T00:03:27.1986124+02:00
      CreationTimeUtc: 2026-01-01T22:03:27.1986124Z
      LastAccessTime: 2026-01-05T03:02:03.3350369+02:00
      LastAccessTimeUtc: 2026-01-05T01:02:03.3350369Z
      LastWriteTime: 2026-01-02T00:03:27.1986124+02:00
      LastWriteTimeUtc: 2026-01-01T22:03:27.1986124Z
      LinkTarget: 
      UnixFileMode: OtherExecute, OtherRead, GroupExecute, GroupWrite, GroupRead, UserExecute, UserWrite, UserRead
      Attributes: Directory
    Root:
      Name: /
      Parent: 
      Root: {}
      Exists: true
      FullName: /
      Extension: ""
      CreationTime: 2025-05-02T20:51:31.2748649+03:00
      CreationTimeUtc: 2025-05-02T17:51:31.2748649Z
      LastAccessTime: 2026-01-05T00:06:56.2591474+02:00
      LastAccessTimeUtc: 2026-01-04T22:06:56.2591474Z
      LastWriteTime: 2025-05-02T20:51:31.2748649+03:00
      LastWriteTimeUtc: 2025-05-02T17:51:31.2748649Z
      LinkTarget: 
      UnixFileMode: OtherExecute, OtherRead, GroupExecute, GroupRead, UserExecute, UserWrite, UserRead
      Attributes: ReadOnly, Directory
    Exists: true
    FullName: /home/gabriel/work/cloudbase/powershell-yaml
    Extension: ""
    CreationTime: 2026-01-05T03:50:34.7428167+02:00
    CreationTimeUtc: 2026-01-05T01:50:34.7428167Z
    LastAccessTime: 2026-01-05T03:50:39.5861251+02:00
    LastAccessTimeUtc: 2026-01-05T01:50:39.5861251Z
    LastWriteTime: 2026-01-05T03:50:34.7428167+02:00
    LastWriteTimeUtc: 2026-01-05T01:50:34.7428167Z
    LinkTarget: 
    UnixFileMode: OtherExecute, OtherRead, GroupExecute, GroupWrite, GroupRead, UserExecute, UserWrite, UserRead
    Attributes: Directory
    
  3. gabriel-samfira commented on Jan 5, 2026

    @gabriel-samfira
    Member

    note, the absolute max recursion is 1000. Default recursion is 100. Most yaml won't ever get close to 100 let alone 1000, but I have learned not to doubt human creativity. To be fair, the stable version is limited at 50 as it is, so these values should be fine.

  4. gabriel-samfira commented on Jan 5, 2026

    @gabriel-samfira
    Member

    The PR was merged. There is now a -Depth flag for ConvertTo-Yaml

  5. beedell-roke commented on Jan 5, 2026

    @beedell-roke

    @gabriel-samfira, is -Depth also limited (to 1000)?

  6. gabriel-samfira commented on Jan 5, 2026

    @gabriel-samfira
    Member

    yes. The default is 100 (which is higher than the old default), but you can raise it up to 1000 if you need to.

  7. beedell-roke commented on Jan 5, 2026

    @beedell-roke
  8. gabriel-samfira commented on Jan 5, 2026

    @gabriel-samfira
    Member

    There are self referencing, serializable objects like the Root object in the Get-Item . command you see above. Without a depth limiter, you will consume CPU forever, recursing to infinity. YamlDotNet itself has a limit of 50 by default. We raised that a bit and added the flag to the commandlet to allow a customizable range up to 1000. It is unlikely you will ever need more. If there are more than 1000 levels to a YAML, then YAML is probably the wrong tool for the job.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions