Skip to content

Commit 6a695d8

Browse files
Clean up native Codegen staging directories
## Summary: Fixes #60057. The Codegen executor stages each library's generated files in a temporary directory and copies them to the configured output. Repeated invocations leave those staging directories behind. Remove the fresh staging directory in a `finally` block so it is cleaned up after both successful generation and a generator error. Thanks to @YevheniiKotyrlo for the CLI reproducer and the build observations. I used Codex to help implement and verify the fix. ## Changelog: [GENERAL] [FIXED] - Clean up temporary native Codegen artifacts after generation. ## Test Plan: - Both new regression tests failed on the base revision and passed with the change. They cover successful generation and a generator error. - Ran `scripts/generate-codegen-artifacts.js` for a minimal iOS module fixture on Windows. The three generated files were byte-for-byte identical before and after the fix, and the temporary directory went from one leftover entry to zero. - The broader Codegen Jest run on Windows passed 53 tests. The same two podspec snapshots fail on the base and on this branch because they expect POSIX path separators. - ESLint and Prettier passed for both changed files. Differential Revision: D132532185 Pull Request resolved: #60058
1 parent bfea95f commit 6a695d8

2 files changed

Lines changed: 104 additions & 21 deletions

File tree

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
*/
10+
11+
'use strict';
12+
13+
const {
14+
generateCode,
15+
} = require('../generate-artifacts-executor/generateNativeCode');
16+
const fs = require('node:fs');
17+
const os = require('node:os');
18+
const path = require('node:path');
19+
20+
jest.mock('../codegen-utils', () => ({
21+
getCodegen: () => ({
22+
generate: ({outputDirectory}: {outputDirectory: string}) => {
23+
const {writeFileSync} = require('node:fs');
24+
const {join} = require('node:path');
25+
writeFileSync(join(outputDirectory, 'TestSpec.h'), 'generated code');
26+
},
27+
}),
28+
}));
29+
30+
let root: string;
31+
let temp: string;
32+
let output: string;
33+
34+
beforeEach(() => {
35+
root = fs.mkdtempSync(path.join(os.tmpdir(), 'codegen-cleanup-test-'));
36+
temp = path.join(root, 'temp');
37+
output = path.join(root, 'output');
38+
fs.mkdirSync(temp);
39+
jest.spyOn(os, 'tmpdir').mockReturnValue(temp);
40+
});
41+
42+
afterEach(() => {
43+
jest.restoreAllMocks();
44+
fs.rmSync(root, {recursive: true, force: true});
45+
});
46+
47+
it('removes temporary artifacts after copying the generated files', () => {
48+
generateCode(
49+
output,
50+
{
51+
library: {config: {name: 'TestSpec', type: 'modules'}},
52+
schema: {modules: {}},
53+
},
54+
false,
55+
'ios',
56+
);
57+
58+
expect(fs.readFileSync(path.join(output, 'TestSpec.h'), 'utf8')).toBe(
59+
'generated code',
60+
);
61+
expect(fs.readdirSync(temp)).toEqual([]);
62+
});
63+
64+
it('removes the temporary directory when code generation throws', () => {
65+
expect(() =>
66+
generateCode(
67+
output,
68+
{
69+
library: {config: {name: 'TestSpec', type: 'invalid'}},
70+
schema: {modules: {}},
71+
},
72+
false,
73+
'ios',
74+
),
75+
).toThrow('Invalid library type. invalid');
76+
expect(fs.readdirSync(temp)).toEqual([]);
77+
});

‎packages/react-native/scripts/codegen/generate-artifacts-executor/generateNativeCode.js‎

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,35 @@ function generateCode(
5151
}
5252

5353
const libraryName = schemaInfo.library.config.name;
54-
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), libraryName));
55-
const tmpOutputDir = path.join(tmpDir, 'out');
56-
fs.mkdirSync(tmpOutputDir, {recursive: true});
57-
58-
codegenLog(`Generating Native Code for ${libraryName} - ${platform}`);
59-
const useLocalIncludePaths = includesGeneratedCode;
60-
generateSpecsCLIExecutor.generateSpecFromInMemorySchema(
61-
platform,
62-
schemaInfo.schema,
63-
tmpOutputDir,
64-
libraryName,
65-
'com.facebook.fbreact.specs',
66-
schemaInfo.library.config.type,
67-
useLocalIncludePaths,
54+
const tmpDir = fs.mkdtempSync(
55+
path.join(os.tmpdir(), 'react-native-codegen-'),
6856
);
57+
try {
58+
const tmpOutputDir = path.join(tmpDir, 'out');
59+
fs.mkdirSync(tmpOutputDir, {recursive: true});
6960

70-
// Finally, copy artifacts to the final output directory.
71-
const outputDir = forceOutputPath
72-
? outputPath
73-
: (reactNativeCoreLibraryOutputPath(libraryName, platform) ?? outputPath);
74-
fs.mkdirSync(outputDir, {recursive: true});
75-
cpSyncRecursiveIfChanged(tmpOutputDir, outputDir);
76-
codegenLog(`Generated artifacts: ${outputDir}`);
61+
codegenLog(`Generating Native Code for ${libraryName} - ${platform}`);
62+
const useLocalIncludePaths = includesGeneratedCode;
63+
generateSpecsCLIExecutor.generateSpecFromInMemorySchema(
64+
platform,
65+
schemaInfo.schema,
66+
tmpOutputDir,
67+
libraryName,
68+
'com.facebook.fbreact.specs',
69+
schemaInfo.library.config.type,
70+
useLocalIncludePaths,
71+
);
72+
73+
// Finally, copy artifacts to the final output directory.
74+
const outputDir = forceOutputPath
75+
? outputPath
76+
: (reactNativeCoreLibraryOutputPath(libraryName, platform) ?? outputPath);
77+
fs.mkdirSync(outputDir, {recursive: true});
78+
cpSyncRecursiveIfChanged(tmpOutputDir, outputDir);
79+
codegenLog(`Generated artifacts: ${outputDir}`);
80+
} finally {
81+
fs.rmSync(tmpDir, {recursive: true, force: true});
82+
}
7783
}
7884

7985
function reactNativeCoreLibraryOutputPath(

0 commit comments

Comments
 (0)