Skip to content

Commit 304617c

Browse files
authored
fix(android): fix autolinking under specific hoisting conditions (#2315)
1 parent d3138e9 commit 304617c

File tree

3 files changed

+6
-54
lines changed

3 files changed

+6
-54
lines changed

android/autolink.mjs

+4-54
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// @ts-check
2-
import * as crypto from "node:crypto";
2+
import { getCurrentState } from "@rnx-kit/tools-react-native/cache";
3+
import { loadContext } from "@rnx-kit/tools-react-native/context";
34
import * as fs from "node:fs";
45
import * as path from "node:path";
56
import {
6-
findFile,
77
isMain,
88
readJSONFile,
99
readTextFile,
10-
requireTransitive,
1110
writeTextFile,
1211
} from "../scripts/helpers.js";
1312

@@ -33,45 +32,6 @@ function ensureDirForFile(p) {
3332
fs.mkdirSync(path.dirname(p), { recursive: true, mode: 0o755 });
3433
}
3534

36-
/**
37-
* @param {crypto.Hash} hash
38-
* @param {string[]} files
39-
* @param {string} projectRoot
40-
* @param {"all" | "first-only"} mode
41-
*/
42-
function updateHash(hash, files, projectRoot, mode) {
43-
for (const file of files) {
44-
const p = findFile(file, projectRoot);
45-
if (p) {
46-
hash.update(fs.readFileSync(p));
47-
if (mode === "first-only") {
48-
break;
49-
}
50-
}
51-
}
52-
}
53-
54-
/**
55-
* @param {string} projectRoot
56-
* @returns {string}
57-
*/
58-
function getCurrentState(projectRoot) {
59-
const sha2 = crypto.createHash("sha256");
60-
61-
const configFiles = ["package.json", "react-native.config.js"];
62-
updateHash(sha2, configFiles, projectRoot, "all");
63-
64-
const lockfiles = [
65-
"yarn.lock",
66-
"package-lock.json",
67-
"pnpm-lock.yaml",
68-
"bun.lockb",
69-
];
70-
updateHash(sha2, lockfiles, projectRoot, "first-only");
71-
72-
return sha2.digest("hex");
73-
}
74-
7535
/**
7636
* @param {Config} config
7737
* @returns {AndroidDependencies}
@@ -132,19 +92,9 @@ function loadConfig(json, projectRoot) {
13292
return readJSONFile(json);
13393
}
13494

135-
/** @type {import("@react-native-community/cli")} */
136-
const { loadConfig } = requireTransitive(
137-
["react-native", "@react-native-community/cli"],
138-
projectRoot
139-
);
140-
141-
// The signature of `loadConfig` changed in 14.0.0:
142-
// https://github.com/react-native-community/cli/commit/b787c89edb781bb788576cd615d2974fc81402fc
143-
const argc = loadConfig.length;
144-
// @ts-expect-error TS2345: Argument of type X is not assignable to parameter of type Y
145-
const config = loadConfig(argc === 1 ? { projectRoot } : projectRoot);
146-
95+
const config = loadContext(projectRoot);
14796
const prunedConfig = pruneDependencies(config);
97+
14898
ensureDirForFile(json);
14999
writeTextFile(json, JSON.stringify(prunedConfig, undefined, 2) + "\n");
150100
writeTextFile(stateFile, state);

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
},
9090
"dependencies": {
9191
"@rnx-kit/react-native-host": "^0.5.0",
92+
"@rnx-kit/tools-react-native": "^2.0.0",
9293
"ajv": "^8.0.0",
9394
"cliui": "^8.0.0",
9495
"fast-xml-parser": "^4.0.0",

yarn.lock

+1
Original file line numberDiff line numberDiff line change
@@ -12380,6 +12380,7 @@ __metadata:
1238012380
"@react-native-community/template": "npm:^0.75.0"
1238112381
"@rnx-kit/eslint-plugin": "npm:^0.8.0"
1238212382
"@rnx-kit/react-native-host": "npm:^0.5.0"
12383+
"@rnx-kit/tools-react-native": "npm:^2.0.0"
1238312384
"@rnx-kit/tsconfig": "npm:^2.0.0"
1238412385
"@types/js-yaml": "npm:^4.0.5"
1238512386
"@types/mustache": "npm:^4.0.0"

0 commit comments

Comments
 (0)