Skip to content

[pigeon] Add support for multiple part files#11664

Open
rohitsangwan01 wants to merge 5 commits intoflutter:mainfrom
Navideck:add-support-for-multiple-part-files
Open

[pigeon] Add support for multiple part files#11664
rohitsangwan01 wants to merge 5 commits intoflutter:mainfrom
Navideck:add-support-for-multiple-part-files

Conversation

@rohitsangwan01
Copy link
Copy Markdown

@rohitsangwan01 rohitsangwan01 commented May 7, 2026

Adds support for defining Pigeon APIs across files using Dart part / part of directives. Pigeon now parses the main input file together with its part files so split definitions are handled correctly.

Partially addresses flutter/flutter#66550.

What changed

  • Detects part directives from the main Pigeon input.
  • Includes referenced part files in analyzer context for parsing/diagnostics.
  • Merges the main file and part file contents for Pigeon AST processing.

Pre-Review Checklist

  • I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
  • I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools.
  • I read the [Tree Hygiene] page, which explains my responsibilities.
  • I read and followed the [relevant style guides] and ran [the auto-formatter].
  • I signed the [CLA].
  • The title of the PR starts with the name of the package surrounded by square brackets, e.g. [shared_preferences]
  • I [linked to at least one issue that this PR fixes] in the description above.
  • I followed [the version and CHANGELOG instructions], using [semantic versioning] and the [repository CHANGELOG style], or I have commented below to indicate which documented exception this PR falls under[^1].
  • I updated/added any relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or I have commented below to indicate which [test exemption] this PR falls under[^1].
  • All existing and new tests are passing.

@flutter-dashboard
Copy link
Copy Markdown

It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging.

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. If you believe this PR qualifies for a test exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!). The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request updates the Pigeon library to support Dart 'part' files by identifying part paths and merging their content for processing. Feedback indicates that the current concatenation approach causes incorrect line number reporting in RootBuilder. Additionally, it is recommended to handle file-reading errors more gracefully to prevent CLI crashes and to refactor the parsing logic to eliminate redundant AST generation across multiple helper methods.

Comment thread packages/pigeon/lib/src/pigeon_lib.dart Outdated
Comment thread packages/pigeon/lib/src/pigeon_lib.dart Outdated
Comment thread packages/pigeon/lib/src/pigeon_lib.dart Outdated
@stuartmorgan-g
Copy link
Copy Markdown
Collaborator

Thanks for the contribution!

In the future, please do not delete the checklist that is in the PR template; it is there for a reason. This PR is missing required elements described in the checklist, which need to be addressed before it moves forward with review.

I am marking the PR as a Draft. Please re-add and complete the checklist, updating the PR as appropriate, and when that’s complete please feel free to mark the PR as ready for review.

@stuartmorgan-g stuartmorgan-g marked this pull request as draft May 8, 2026 12:01
@rohitsangwan01 rohitsangwan01 marked this pull request as ready for review May 8, 2026 15:36
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request enables Pigeon to support Dart part files by merging the contents of the main file and its parts into a single string for parsing. It introduces helper methods for resolving part paths and stripping directives, and adds tests for multi-file configurations. Feedback indicates that the concatenation approach causes a regression in error reporting line numbers, fails to support nested part files, and should include LibraryDirective handling to prevent generating invalid Dart code.

Comment on lines +509 to +515
final rootBuilder = RootBuilder(rootInputString);
final dart_ast.CompilationUnit mergedUnit = parseString(
content: rootInputString,
path: normalizedInputPath,
throwIfDiagnostics: false,
).unit;
mergedUnit.accept(rootBuilder);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Merging the contents of the main file and its parts into a single rootInputString causes a regression in error reporting. Any errors identified by RootBuilder (e.g., invalid types, unsupported constructs) will report line numbers relative to this concatenated string and attribute them to the main file path, making it difficult for users to locate the actual issue in their source files. Consider a strategy that preserves the mapping to original files or avoids string concatenation by visiting multiple CompilationUnits while providing the correct source context for each.

Comment on lines +556 to +574
List<String> _getPartPaths(
Iterable<dart_ast.Directive> directives, {
required String sourcePath,
}) {
final parts = <String>[];
for (final directive in directives) {
if (directive is dart_ast.PartDirective) {
final String? uri = directive.uri.stringValue;
if (uri != null) {
parts.add(
path.absolute(
path.normalize(path.join(path.dirname(sourcePath), uri)),
),
);
}
}
}
return parts;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The current implementation of _getPartPaths only retrieves part files referenced directly in the main Pigeon file. It does not support nested part files (i.e., a part file that itself contains part directives), which is valid Dart. To fully support the part mechanism, this should recursively resolve part paths.

Comment on lines +593 to +596
} else if (directive is dart_ast.PartDirective ||
directive is dart_ast.PartOfDirective) {
removalRanges.add((start: directive.offset, end: directive.end));
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The _stripPartsAndCollectImports function does not handle LibraryDirectives. If the main Pigeon file contains a library directive, it will remain in the body while imports are moved to the top in _getInputString. This results in invalid Dart code (as library must precede import), which may cause the subsequent parseString call or RootBuilder to behave unexpectedly. Consider stripping LibraryDirective as well.

      } else if (directive is dart_ast.PartDirective ||
          directive is dart_ast.PartOfDirective ||
          directive is dart_ast.LibraryDirective) {
        removalRanges.add((start: directive.offset, end: directive.end));
      }

@rohitsangwan01
Copy link
Copy Markdown
Author

@stuartmorgan-g Thank you for the feedback. I’ve re-added the checklist and completed the required items in the PR description. The PR is now updated and ready for review again.
Please let me know if something else is still missing.

@stuartmorgan-g
Copy link
Copy Markdown
Collaborator

[x] I followed [the version and CHANGELOG instructions], using [semantic versioning] and the [repository CHANGELOG style], or I have commented below to indicate which documented exception this PR falls under[^1].

Could you point me to where you've done this step? I'm not seeing either of the two options in the PR.

@rohitsangwan01
Copy link
Copy Markdown
Author

Could you point me to where you've done this step? I'm not seeing either of the two options in the PR.

Ahh, I marked it by mistake. I thought CHANGELOG and Version update would be the last step, I’ve now updated the CHANGELOG and Version

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants