Skip to content

feat: add --target flag for target-specific visualization#810

Merged
patrickhoefler merged 5 commits intomainfrom
feat/target-flag
Mar 13, 2026
Merged

feat: add --target flag for target-specific visualization#810
patrickhoefler merged 5 commits intomainfrom
feat/target-flag

Conversation

@patrickhoefler
Copy link
Copy Markdown
Owner

Adds a --target flag that limits the graph to stages transitively needed to build the specified targets, eliding everything else.

Also includes three refactorings that fit naturally with this change:

  • Introduce ParseOptions and BuildOptions structs to replace the growing positional parameter lists on LoadAndParseDockerfile and BuildDotFile
  • Move ScratchModeFromString into the dockerfile2dot package next to the ScratchMode type it converts to
  • Extract a shared findStageIndex helper to eliminate duplicate stage lookup logic between convert.go and build.go

Closes #686

Adds a --target flag that limits the graph to stages transitively needed
to build the specified targets, eliding everything else.

Also includes three refactorings that fit naturally with this change:
- Introduce ParseOptions and BuildOptions structs to replace the growing
  positional parameter lists on LoadAndParseDockerfile and BuildDotFile
- Move ScratchModeFromString into the dockerfile2dot package next to the
  ScratchMode type it converts to
- Extract a shared findStageIndex helper to eliminate duplicate stage
  lookup logic between convert.go and build.go

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds target-scoped graph generation so users can visualize only the stages needed to build one or more specified targets, instead of always rendering the full multi-stage Dockerfile graph.

Changes:

  • Introduces --target CLI flag and target filtering in the Dockerfile parsing pipeline.
  • Refactors parsing/rendering APIs to use ParseOptions / BuildOptions structs instead of long parameter lists.
  • Consolidates stage lookup logic via findStageIndex and relocates ScratchModeFromString into dockerfile2dot.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
internal/dockerfile2dot/structs.go Adds option structs + shared helpers (findStageIndex, ScratchModeFromString).
internal/dockerfile2dot/load.go Threads ParseOptions through parsing and applies filterToTargets when Targets are set.
internal/dockerfile2dot/load_test.go Updates tests to call LoadAndParseDockerfile with ParseOptions.
internal/dockerfile2dot/filter.go Implements reachability-based stage filtering + numeric WaitFor remapping + external image filtering.
internal/dockerfile2dot/filter_test.go Adds focused unit tests for target filtering behavior and remapping.
internal/dockerfile2dot/convert.go Updates converter to accept ParseOptions; refactors external-image resolution to use shared stage lookup.
internal/dockerfile2dot/convert_test.go Adds tests for ScratchModeFromString and findStageIndex; updates converter tests for ParseOptions.
internal/dockerfile2dot/build.go Updates DOT builder to accept BuildOptions; refactors stage node ID resolution using findStageIndex.
internal/dockerfile2dot/build_test.go Updates tests to call BuildDotFile with BuildOptions.
internal/cmd/root.go Adds --target flag wiring; switches to ParseOptions/BuildOptions and package-level ScratchModeFromString.
internal/cmd/root_test.go Updates help output and adds CLI integration coverage for --target.
README.md Documents the new --target flag in examples and the full flags list.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

patrickhoefler and others added 2 commits March 13, 2026 14:16
… from external image filter

Two correctness fixes from code review:
- Trim whitespace from --target values so "--target a, b" works (consistent
  with how --separate normalizes its inputs)
- In filterExternalImages, skip WaitFor IDs that resolve to an internal stage
  so a kept stage named "alpine" doesn't incorrectly retain an unrelated
  external image with the same ID from an elided stage

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The comment claimed -1 is returned for out-of-range numeric indices, but
the implementation returns the parsed index value. Update the comment to
accurately describe both the numeric out-of-range and name-not-found cases.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a --target CLI flag to visualize only the stages transitively required to build selected Dockerfile targets, aligning the graph output with target-focused workflows (issue #686). This is supported by internal parsing/build API refactors and shared helpers.

Changes:

  • Add --target flag and stage filtering logic (filterToTargets) to elide unrelated stages and external images.
  • Refactor dockerfile2dot entrypoints to use ParseOptions / BuildOptions structs instead of long positional parameter lists.
  • Consolidate shared helpers (findStageIndex, ScratchModeFromString, stage node ID building) to reduce duplicated stage lookup logic.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
internal/dockerfile2dot/structs.go Adds shared helpers and introduces ParseOptions/BuildOptions.
internal/dockerfile2dot/load.go Plumbs ParseOptions through parsing and applies optional target filtering.
internal/dockerfile2dot/load_test.go Updates tests for the new ParseOptions API.
internal/dockerfile2dot/filter.go Implements filtering to requested targets, including index remapping and external image pruning.
internal/dockerfile2dot/filter_test.go Adds comprehensive unit tests for target filtering behavior and edge cases.
internal/dockerfile2dot/convert.go Updates parsing pipeline to accept ParseOptions and uses shared stage lookup for dependency handling.
internal/dockerfile2dot/convert_test.go Adds tests for ScratchModeFromString and findStageIndex; updates parse tests for ParseOptions.
internal/dockerfile2dot/build.go Updates build API to BuildOptions and refactors stage node ID resolution.
internal/dockerfile2dot/build_test.go Updates tests for the new BuildOptions API.
internal/cmd/root.go Wires --target into CLI and passes options structs to dockerfile2dot APIs.
internal/cmd/root_test.go Updates CLI usage text and adds end-to-end tests covering --target behavior.
README.md Documents the new --target flag and updates the flags list.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- resolveTargetIndices now returns an error when all target entries are
  empty or whitespace-only, preventing a silent empty graph
- Fix grammar in ScratchModeFromString doc comment ("returns" → "return")

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds target-specific Dockerfile graph rendering so users can visualize only the stages required to build one or more selected targets, aligning with #686.

Changes:

  • Add --target CLI flag and implement stage graph filtering to only keep stages transitively required by the specified targets.
  • Refactor dockerfile2dot APIs to use ParseOptions / BuildOptions structs and centralize helper logic (ScratchModeFromString, findStageIndex).
  • Add comprehensive unit/integration tests for target filtering behavior and the new helpers.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
internal/dockerfile2dot/structs.go Adds ParseOptions/BuildOptions, findStageIndex, and ScratchModeFromString.
internal/dockerfile2dot/load.go Plumbs ParseOptions and applies filterToTargets when targets are provided.
internal/dockerfile2dot/filter.go Implements target reachability filtering + WaitFor ID remapping + external image pruning.
internal/dockerfile2dot/convert.go Refactors parsing to take ParseOptions; updates external image resolution logic.
internal/dockerfile2dot/build.go Refactors rendering to take BuildOptions; deduplicates stage lookup via findStageIndex.
internal/dockerfile2dot/load_test.go Updates tests to call LoadAndParseDockerfile with ParseOptions.
internal/dockerfile2dot/convert_test.go Adds tests for ScratchModeFromString and findStageIndex; updates parse tests for ParseOptions.
internal/dockerfile2dot/build_test.go Updates tests to call BuildDotFile with BuildOptions.
internal/dockerfile2dot/filter_test.go Adds unit tests covering target filtering, remapping, and external image pruning.
internal/cmd/root.go Adds --target flag; plumbs new options structs into dockerfile2dot calls.
internal/cmd/root_test.go Extends CLI tests to validate --target output and error behavior.
README.md Documents the new --target flag in usage + options list.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Add a NOTE comment explaining that findStageIndex searches all stages
regardless of definition order, which differs from Docker's behavior of
only allowing references to previously-defined stages. This is a
pre-existing limitation affecting multiple call sites across the
codebase, not introduced by the --target feature.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@patrickhoefler patrickhoefler enabled auto-merge (squash) March 13, 2026 13:40
@patrickhoefler patrickhoefler merged commit d639788 into main Mar 13, 2026
9 checks passed
@patrickhoefler patrickhoefler deleted the feat/target-flag branch March 13, 2026 13:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Visualization of specific targets

2 participants