[ Tool ] Stop generating widget preview scaffold under $TMP#186476
Conversation
There was a problem hiding this comment.
Code Review
This pull request moves the widgetPreviewScaffold from a temporary directory to a local .widget_preview directory and adds a migration to update .gitignore. It also updates the WidgetPreviewScaffoldInspectorService to register the project root directory. Review feedback highlights that the string check for .widget_preview is too broad and may cause false positives. Additionally, there are suggestions to lower the logging level for the gitignore migration and to resolve inconsistencies between the migration logic and the project template regarding the exclusion of the bin/ directory from .gitignore.
Previously, the `widget_preview_scaffold` project was generated under the system temporary directory (`$TMP`) to work around issues with `flutter clean`. However, generating the scaffold in `$TMP` meant that the project was recreated on every run or could be lost, leading to unnecessary regeneration overhead and tracking issues. Move the generated `widget_preview_scaffold` project from `$TMP` to a `.widget_preview` directory located directly under the project root. To support this change, `WidgetPreviewGitignoreMigration` is introduced to automatically add `.widget_preview/` to the host project's `.gitignore`. In addition, `kProjectRootPath` is passed via DTD connection info to ensure `WidgetInspectorService` correctly adds the project root to pub root directories. Finally, file watchers (`LspPreviewDetector` and `PreviewDetector`) are updated to ignore the new `.widget_preview` directory. Fixes flutter#179036
b3d5624 to
408819b
Compare
The template previously ignored `.widget_preview/*` but explicitly allowed `!.widget_preview/bin/*`. Since `.widget_preview` is a generated, ephemeral scaffold project for hosting widget previews, tracking any part of it in the host project repository is unnecessary and inconsistent with the migrator logic. Update `packages/flutter_tools/templates/app/.gitignore.tmpl` to ignore the entire `.widget_preview/` directory, aligning it with `WidgetPreviewGitignoreMigration`. Fixes flutter#179036
- Make `.widget_preview` check in `utils.dart` more specific by checking path segments to avoid false positives. - Lower logging level in `WidgetPreviewGitignoreMigration` from warning to trace. - Update tests to expect trace output. Fixes flutter#179036
The template `widget_preview_inspector_service.dart.tmpl` references `kProjectRootPath`, but `dtd_connection_info.dart.tmpl` was missing its definition. This caused analysis failures when the template or dummy project was analyzed. Added `const String kProjectRootPath = '';` to the template to resolve the error. Fixes flutter#179036
Updates kProjectRootPath generation in PreviewCodeGenerator to produce a raw string (r'...'), ensuring that absolute file paths (especially on Windows) are correctly represented without escape sequence errors. Also updates the initial constant definition in the scaffold template and inflated file to be a raw string. Fixes flutter#179036
…tartWidgetPreview
…nt process termination limitations
…ysis race conditions
| return; | ||
| } | ||
| await _analysisServer?.waitForAnalysis(); | ||
| await _analysisServer?.waitForAnalysis(delay: const Duration(seconds: 5)); |
There was a problem hiding this comment.
Why the new 5 second wait?
| bool get isDartFile => endsWith('.dart'); | ||
| bool get isPubspec => endsWith('pubspec.yaml'); | ||
| bool get doesContainDartTool => contains('.dart_tool'); | ||
| bool get doesContainWidgetPreview => split(RegExp(r'[/\\]')).contains('.widget_preview'); |
There was a problem hiding this comment.
Can we extract out this RegExp into a static field so it is not constructed every time the getter is called?
- Extract path separator RegExp to a static field in StringExtension to avoid recreation. - Revert the custom delay for waitForAnalysis in LspPreviewDetector to the default to see if it is still needed.
Previously, the
widget_preview_scaffoldproject was generated under the system temporary directory ($TMP) to work around issues withflutter clean. However, generating the scaffold in$TMPmeant that the project was recreated on every run or could be lost, leading to unnecessary regeneration overhead and tracking issues.Move the generated
widget_preview_scaffoldproject from$TMPto a.widget_previewdirectory located directly under the project root. To support this change,WidgetPreviewGitignoreMigrationis introduced to automatically add.widget_preview/to the host project's.gitignore. In addition,kProjectRootPathis passed via DTD connection info to ensureWidgetInspectorServicecorrectly adds the project root to pub root directories. Finally, file watchers (LspPreviewDetectorandPreviewDetector) are updated to ignore the new.widget_previewdirectory.Fixes #179036