Skip to content

Commit d11195c

Browse files
authored
[hooks] Add documentation on running the debugger (#2706)
Closes: #2702
1 parent ae2f16a commit d11195c

File tree

6 files changed

+230
-3
lines changed

6 files changed

+230
-3
lines changed

pkgs/hooks/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.20.4
2+
3+
- Improved documentation on debugging and added a documentation index.
4+
15
## 0.20.3
26

37
- Polished README.md, Dartdocs, and examples.

pkgs/hooks/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ example/sqlite/](../code_assets/example/sqlite/).
5252

5353
For more information see [dart.dev/tools/hooks](https://dart.dev/tools/hooks).
5454

55+
## Documentation
56+
57+
For detailed documentation on debugging and the configuration schema, see the
58+
[documentation](./doc/README.md).
59+
5560
## Status: In Preview
5661

5762
**NOTE**: This package is currently in preview and published under the

pkgs/hooks/doc/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# `package:hooks` Documentation
2+
3+
This directory contains detailed documentation for the `package:hooks` API.
4+
5+
## Available Documentation
6+
7+
* **[Debugging](./debugging.md)**: Learn how to debug your build hooks by inspecting intermediate files and running hooks with a debugger.
8+
9+
* **[Schema](./schema/README.md)**: Explore the JSON schema for the configuration files used by build hooks.

pkgs/hooks/doc/debugging.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Debugging Dart Hooks
2+
3+
When a build[^1] hook isn't working as expected, you can investigate the
4+
intermediate files generated by the Dart and Flutter SDK build process. These
5+
files provide insight into the inputs, outputs, and any errors that occurred
6+
during the hook's execution.
7+
8+
## Key Debugging Directory
9+
10+
All debugging artifacts are stored within the `.dart_tool` directory of your
11+
project. The hook runner generates a unique folder for each hook execution,
12+
containing temporary and output files.
13+
14+
You can find the most important debugging files in a subdirectory specific to
15+
your hook's execution. The path is of the form
16+
`.dart_tool/hooks_runner/<package_name>/<some_hash>/`, where `<package_name>`
17+
is the name of the package containing the hook. For example, for a hook in a
18+
package named `sqlite`, it would be
19+
`.dart_tool/hooks_runner/sqlite/221e109fbc/`.
20+
21+
When you run a build, hooks for all dependencies are executed, so you might see
22+
multiple package directories.
23+
24+
The `<some_hash>` is a checksum of the hook's configuration. If you are unsure
25+
which hash directory to inspect within your package's hook directory, you can
26+
delete the `.dart_tool/hooks_runner/<package_name>/` directory and re-run the
27+
command that failed. The newly created directory will be for the latest
28+
invocation.
29+
30+
Inside this directory, you will find:
31+
32+
* `input.json`: Contains the exact configuration and data passed *into* your
33+
build hook. Reviewing this helps confirm if the hook is receiving the expected
34+
build configuration.
35+
* `output.json`: This file stores the JSON data that your build hook *produced*
36+
as its result.
37+
* `stdout.txt`: Captures any standard output from your build hook. This is where
38+
messages from a logger or `print` statements will appear.
39+
* `stderr.txt`: Captures any error messages or exceptions thrown during the
40+
hook's execution.
41+
42+
## Using a Debugger
43+
44+
To step through your code with a debugger, run the build hook from its source
45+
file and provide the `input.json` via the `--config` flag. This allows you to
46+
set breakpoints in your IDE and inspect the hook's execution.
47+
48+
For example:
49+
50+
```sh
51+
dart run hook/build.dart --config .dart_tool/hooks_runner/your_package_name/some_hash/input.json
52+
```
53+
54+
Make sure to replace `hook/build.dart`, `your_package_name`, and
55+
`some_hash` with the actual paths from your project.
56+
57+
## VS Code Launch Configuration
58+
59+
To debug in VS Code, you can create a `launch.json` file in a `.vscode`
60+
directory in your project root. This allows you to run your hook with a
61+
debugger attached.
62+
63+
Here is an example configuration:
64+
65+
```json
66+
{
67+
"version": "0.2.0",
68+
"configurations": [
69+
{
70+
"name": "Debug Hook",
71+
"type": "dart",
72+
"request": "launch",
73+
"program": "path/to/your/hook/build.dart",
74+
"args": [
75+
"--config",
76+
".dart_tool/hooks_runner/your_package_name/some_hash/input.json"
77+
]
78+
}
79+
]
80+
}
81+
```
82+
83+
Again, make sure to replace `path/to/your/hook/build.dart`,
84+
`your_package_name`, and `some_hash` with the actual paths from your project.
85+
After setting this up, you can run the "Debug Hook" configuration from the
86+
"Run and Debug" view in VS Code.
87+
88+
[^1]: This debugging strategy also works for other hooks than build hooks, but
89+
this documentation focuses specifically on build hooks.

pkgs/hooks/lib/src/api/build_and_link.dart

Lines changed: 122 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,66 @@ import '../validation.dart';
8282
/// guaranteed to be invoked with a process invocation and should return a
8383
/// non-zero exit code on failure. Throwing will lead to an uncaught exception,
8484
/// causing a non-zero exit code.
85+
///
86+
/// ## Debugging
87+
///
88+
/// When a build hook doesn't work as expected, you can investigate the
89+
/// intermediate files generated by the Dart and Flutter SDK build process.
90+
///
91+
/// The most important files for debugging are located in a subdirectory
92+
/// specific to your hook's execution. The path is of the form
93+
/// `.dart_tool/hooks_runner/<package_name>/<some_hash>/`, where
94+
/// `<package_name>` is the name of the package containing the hook. Inside, you
95+
/// will find:
96+
///
97+
/// * `input.json`: The configuration and data passed into your build hook.
98+
/// * `output.json`: The JSON data that your build hook produced.
99+
/// * `stdout.txt`: Any standard output from your build hook.
100+
/// * `stderr.txt`: Any error messages or exceptions.
101+
///
102+
/// When you run a build, hooks for all dependencies are executed, so you might
103+
/// see multiple package directories.
104+
///
105+
/// The `<some_hash>` is a checksum of the [BuildConfig] in the `input.json`. If
106+
/// you are unsure which hash directory to inspect within your package's hook
107+
/// directory, you can delete the `.dart_tool/hooks_runner/<package_name>/`
108+
/// directory and re-run the command that failed. The newly created directory
109+
/// will be for the latest invocation.
110+
///
111+
/// You can step through your code with a debugger by running the build hook
112+
/// from its source file and providing the `input.json` via the `--config` flag:
113+
///
114+
/// ```sh
115+
/// dart run hook/build.dart --config .dart_tool/hooks_runner/<package_name>/<some_hash>/input.json
116+
/// ```
117+
///
118+
/// To debug in VS Code, you can create a `launch.json` file in a `.vscode`
119+
/// directory in your project root. This allows you to run your hook with a
120+
/// debugger attached.
121+
///
122+
/// Here is an example configuration:
123+
///
124+
/// ```json
125+
/// {
126+
/// "version": "0.2.0",
127+
/// "configurations": [
128+
/// {
129+
/// "name": "Debug Build Hook",
130+
/// "type": "dart",
131+
/// "request": "launch",
132+
/// "program": "hook/build.dart",
133+
/// "args": [
134+
/// "--config",
135+
/// ".dart_tool/hooks_runner/your_package_name/some_hash/input.json"
136+
/// ]
137+
/// }
138+
/// ]
139+
/// }
140+
/// ```
141+
///
142+
/// Again, make sure to replace `your_package_name`, and `some_hash` with the
143+
/// actual paths from your project. After setting this up, you can run the
144+
/// "Debug Build Hook" configuration from the "Run and Debug" view in VS Code.
85145
Future<void> build(
86146
List<String> arguments,
87147
Future<void> Function(BuildInput input, BuildOutputBuilder output) builder,
@@ -122,8 +182,8 @@ Future<void> build(
122182

123183
/// Links assets in a `hook/link.dart`.
124184
///
125-
/// If a link hook is defined (`hook/link.dart`) then `link` must be called
126-
/// by that hook, to write the [BuildInput.outputFile], even if the [linker]
185+
/// If a link hook is defined (`hook/link.dart`) then `link` must be called by
186+
/// that hook, to write the [BuildInput.outputFile], even if the [linker]
127187
/// function has no work to do.
128188
///
129189
/// Can link native assets which are not already available, or expose existing
@@ -152,6 +212,66 @@ Future<void> build(
152212
/// guaranteed to be invoked with a process invocation and should return a
153213
/// non-zero exit code on failure. Throwing will lead to an uncaught exception,
154214
/// causing a non-zero exit code.
215+
///
216+
/// ## Debugging
217+
///
218+
/// When a link hook doesn't work as expected, you can investigate the
219+
/// intermediate files generated by the Dart and Flutter SDK build process.
220+
///
221+
/// The most important files for debugging are located in a subdirectory
222+
/// specific to your hook's execution. The path is of the form
223+
/// `.dart_tool/hooks_runner/<package_name>/<some_hash>/`, where
224+
/// `<package_name>` is the name of the package containing the hook. Inside, you
225+
/// will find:
226+
///
227+
/// * `input.json`: The configuration and data passed into your link hook.
228+
/// * `output.json`: The JSON data that your link hook produced.
229+
/// * `stdout.txt`: Any standard output from your link hook.
230+
/// * `stderr.txt`: Any error messages or exceptions.
231+
///
232+
/// When you run a build, hooks for all dependencies are executed, so you might
233+
/// see multiple package directories.
234+
///
235+
/// The `<some_hash>` is a checksum of the [LinkConfig] in the `input.json`. If
236+
/// you are unsure which hash directory to inspect within your package's hook
237+
/// directory, you can delete the `.dart_tool/hooks_runner/<package_name>/`
238+
/// directory and re-run the command that failed. The newly created directory
239+
/// will be for the latest invocation.
240+
///
241+
/// You can step through your code with a debugger by running the link hook from
242+
/// its source file and providing the `input.json` via the `--config` flag:
243+
///
244+
/// ```sh
245+
/// dart run hook/link.dart --config .dart_tool/hooks_runner/<package_name>/<some_hash>/input.json
246+
/// ```
247+
///
248+
/// To debug in VS Code, you can create a `launch.json` file in a `.vscode`
249+
/// directory in your project root. This allows you to run your hook with a
250+
/// debugger attached.
251+
///
252+
/// Here is an example configuration:
253+
///
254+
/// ```json
255+
/// {
256+
/// "version": "0.2.0",
257+
/// "configurations": [
258+
/// {
259+
/// "name": "Debug Link Hook",
260+
/// "type": "dart",
261+
/// "request": "launch",
262+
/// "program": "hook/link.dart",
263+
/// "args": [
264+
/// "--config",
265+
/// ".dart_tool/hooks_runner/your_package_name/some_hash/input.json"
266+
/// ]
267+
/// }
268+
/// ]
269+
/// }
270+
/// ```
271+
///
272+
/// Again, make sure to replace `your_package_name`, and `some_hash` with the
273+
/// actual paths from your project. After setting this up, you can run the
274+
/// "Debug Link Hook" configuration from the "Run and Debug" view in VS Code.
155275
Future<void> link(
156276
List<String> arguments,
157277
Future<void> Function(LinkInput input, LinkOutputBuilder output) linker,

pkgs/hooks/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: >-
33
A library that contains a Dart API for the JSON-based protocol for
44
`hook/build.dart` and `hook/link.dart`.
55
6-
version: 0.20.3
6+
version: 0.20.4
77

88
repository: https://github.com/dart-lang/native/tree/main/pkgs/hooks
99

0 commit comments

Comments
 (0)