Skip to content

Commit 3ce1316

Browse files
authored
[compiler][snap] Fixes to relative path resolution; compile subcommand (#35688)
More snap improvements for use with agents: * `yarn snap compile [--debug] <path>` for compiling any file, optionally with debug logs * `yarn snap minimize <path>` now accepts path as a positional param for consistency w 'compile' command * Both compile/minimize commands properly handle paths relative to the compiler/ directory. When using `yarn snap` the current working directory is compiler/packages/snap, but you're generally running it from the compiler directory so this matches expectations of callers better.
1 parent cd0c487 commit 3ce1316

File tree

9 files changed

+238
-24
lines changed

9 files changed

+238
-24
lines changed

compiler/.claude/agents/investigate-error.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ You are an expert React Compiler debugging specialist with deep knowledge of com
1313
Create a new fixture file at `packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/<fixture-name>.js` containing the problematic code. Use a descriptive name that reflects the issue (e.g., `bug-optional-chain-in-effect.js`).
1414

1515
### Step 2: Run Debug Compilation
16-
Execute `yarn snap -d -p <fixture-name>` to compile the fixture with full debug output. This shows the state of the program after each compilation pass.
16+
Execute `yarn snap -d -p <fixture-name>` to compile the fixture with full debug output. This shows the state of the program after each compilation pass. You can also use `yarn snap compile -d <path-to-fixture>`.
1717

1818
### Step 3: Analyze Compilation Results
1919

compiler/CLAUDE.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,31 @@ yarn snap -p <file-basename> -d
3535
yarn snap -u
3636
```
3737

38+
## Compiling Arbitrary Files
39+
40+
Use `yarn snap compile` to compile any file (not just fixtures) with the React Compiler:
41+
42+
```bash
43+
# Compile a file and see the output
44+
yarn snap compile <path>
45+
46+
# Compile with debug logging to see the state after each compiler pass
47+
# This is an alternative to `yarn snap -d -p <pattern>` when you don't have a fixture file yet
48+
yarn snap compile --debug <path>
49+
```
50+
51+
## Minimizing Test Cases
52+
53+
Use `yarn snap minimize` to automatically reduce a failing test case to its minimal reproduction:
54+
55+
```bash
56+
# Minimize a file that causes a compiler error
57+
yarn snap minimize <path>
58+
59+
# Minimize and update the file in-place with the minimized version
60+
yarn snap minimize --update <path>
61+
```
62+
3863
## Version Control
3964

4065
This repository uses Sapling (`sl`) for version control. Sapling is similar to Mercurial: there is not staging area, but new/deleted files must be explicitlyu added/removed.

compiler/docs/DEVELOPMENT_GUIDE.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,32 @@ yarn snap:build
1717
yarn snap --watch
1818
```
1919

20-
`snap` is our custom test runner, which creates "golden" test files that have the expected output for each input fixture, as well as the results of executing a specific input (or sequence of inputs) in both the uncompiled and compiler versions of the input.
20+
`snap` is our custom test runner, which creates "golden" test files that have the expected output for each input fixture, as well as the results of executing a specific input (or sequence of inputs) in both the uncompiled and compiler versions of the input.
21+
22+
### Compiling Arbitrary Files
23+
24+
You can compile any file (not just fixtures) using:
25+
26+
```sh
27+
# Compile a file and see the output
28+
yarn snap compile <path>
29+
30+
# Compile with debug output to see the state after each compiler pass
31+
# This is an alternative to `yarn snap -d -p <pattern>` when you don't have a fixture file yet
32+
yarn snap compile --debug <path>
33+
```
34+
35+
### Minimizing Test Cases
36+
37+
To reduce a failing test case to its minimal reproduction:
38+
39+
```sh
40+
# Minimize a file that causes a compiler error
41+
yarn snap minimize <path>
42+
43+
# Minimize and update the file in-place
44+
yarn snap minimize --update <path>
45+
```
2146

2247
When contributing changes, we prefer to:
2348
* Add one or more fixtures that demonstrate the current compiled output for a particular combination of input and configuration. Send this as a first PR.

compiler/packages/babel-plugin-react-compiler/docs/passes/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,15 @@ yarn snap -p <fixture-name>
294294
# Run with debug output (shows all passes)
295295
yarn snap -p <fixture-name> -d
296296

297+
# Compile any file (not just fixtures) and see output
298+
yarn snap compile <path>
299+
300+
# Compile any file with debug output (alternative to yarn snap -d -p when you don't have a fixture)
301+
yarn snap compile --debug <path>
302+
303+
# Minimize a failing test case to its minimal reproduction
304+
yarn snap minimize <path>
305+
297306
# Update expected outputs
298307
yarn snap -u
299308
```

compiler/packages/snap/src/constants.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@
77

88
import path from 'path';
99

10+
export const PROJECT_ROOT = path.join(process.cwd(), '..', '..');
11+
1012
// We assume this is run from `babel-plugin-react-compiler`
11-
export const PROJECT_ROOT = path.normalize(
12-
path.join(process.cwd(), '..', 'babel-plugin-react-compiler'),
13+
export const BABEL_PLUGIN_ROOT = path.normalize(
14+
path.join(PROJECT_ROOT, 'packages', 'babel-plugin-react-compiler'),
1315
);
1416

15-
export const PROJECT_SRC = path.normalize(
16-
path.join(PROJECT_ROOT, 'dist', 'index.js'),
17+
export const BABEL_PLUGIN_SRC = path.normalize(
18+
path.join(BABEL_PLUGIN_ROOT, 'dist', 'index.js'),
1719
);
1820
export const PRINT_HIR_IMPORT = 'printFunctionWithOutlined';
1921
export const PRINT_REACTIVE_IR_IMPORT = 'printReactiveFunction';
2022
export const PARSE_CONFIG_PRAGMA_IMPORT = 'parseConfigPragmaForTests';
2123
export const FIXTURES_PATH = path.join(
22-
PROJECT_ROOT,
24+
BABEL_PLUGIN_ROOT,
2325
'src',
2426
'__tests__',
2527
'fixtures',

compiler/packages/snap/src/minimize.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import traverse from '@babel/traverse';
1212
import * as t from '@babel/types';
1313
import type {parseConfigPragmaForTests as ParseConfigPragma} from 'babel-plugin-react-compiler/src/Utils/TestUtils';
1414
import {parseInput} from './compiler.js';
15-
import {PARSE_CONFIG_PRAGMA_IMPORT, PROJECT_SRC} from './constants.js';
15+
import {PARSE_CONFIG_PRAGMA_IMPORT, BABEL_PLUGIN_SRC} from './constants.js';
1616

1717
type CompileSuccess = {kind: 'success'};
1818
type CompileParseError = {kind: 'parse_error'; message: string};
@@ -1919,7 +1919,7 @@ export function minimize(
19191919
sourceType: 'module' | 'script',
19201920
): MinimizeResult {
19211921
// Load the compiler plugin
1922-
const importedCompilerPlugin = require(PROJECT_SRC) as Record<
1922+
const importedCompilerPlugin = require(BABEL_PLUGIN_SRC) as Record<
19231923
string,
19241924
unknown
19251925
>;

compiler/packages/snap/src/runner-watch.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import watcher from '@parcel/watcher';
99
import path from 'path';
1010
import ts from 'typescript';
11-
import {FIXTURES_PATH, PROJECT_ROOT} from './constants';
11+
import {FIXTURES_PATH, BABEL_PLUGIN_ROOT} from './constants';
1212
import {TestFilter, getFixtures} from './fixture-utils';
1313
import {execSync} from 'child_process';
1414

@@ -17,7 +17,7 @@ export function watchSrc(
1717
onComplete: (isSuccess: boolean) => void,
1818
): ts.WatchOfConfigFile<ts.SemanticDiagnosticsBuilderProgram> {
1919
const configPath = ts.findConfigFile(
20-
/*searchPath*/ PROJECT_ROOT,
20+
/*searchPath*/ BABEL_PLUGIN_ROOT,
2121
ts.sys.fileExists,
2222
'tsconfig.json',
2323
);
@@ -166,7 +166,7 @@ function subscribeTsc(
166166
let isCompilerBuildValid = false;
167167
if (isTypecheckSuccess) {
168168
try {
169-
execSync('yarn build', {cwd: PROJECT_ROOT});
169+
execSync('yarn build', {cwd: BABEL_PLUGIN_ROOT});
170170
console.log('Built compiler successfully with tsup');
171171
isCompilerBuildValid = true;
172172
} catch (e) {

compiler/packages/snap/src/runner-worker.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {codeFrameColumns} from '@babel/code-frame';
98
import type {PluginObj} from '@babel/core';
109
import type {parseConfigPragmaForTests as ParseConfigPragma} from 'babel-plugin-react-compiler/src/Utils/TestUtils';
1110
import type {printFunctionWithOutlined as PrintFunctionWithOutlined} from 'babel-plugin-react-compiler/src/HIR/PrintHIR';
@@ -15,7 +14,7 @@ import {
1514
PARSE_CONFIG_PRAGMA_IMPORT,
1615
PRINT_HIR_IMPORT,
1716
PRINT_REACTIVE_IR_IMPORT,
18-
PROJECT_SRC,
17+
BABEL_PLUGIN_SRC,
1918
} from './constants';
2019
import {TestFixture, getBasename, isExpectError} from './fixture-utils';
2120
import {TestResult, writeOutputToString} from './reporter';
@@ -65,7 +64,7 @@ async function compile(
6564
let compileResult: TransformResult | null = null;
6665
let error: string | null = null;
6766
try {
68-
const importedCompilerPlugin = require(PROJECT_SRC) as Record<
67+
const importedCompilerPlugin = require(BABEL_PLUGIN_SRC) as Record<
6968
string,
7069
unknown
7170
>;

0 commit comments

Comments
 (0)