From 5bfbd3abcd1554fc7d3ebd40e9c9263f8046d62e Mon Sep 17 00:00:00 2001 From: Oleksii Bulenok Date: Mon, 31 Aug 2026 12:17:43 +0200 Subject: [PATCH] resolve root from babel root --- babel.config.js | 3 ++- plugins/babel-plugin-lexicon-leaf-imports.js | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/babel.config.js b/babel.config.js index 9c164acd95..b9cbc30ef5 100644 --- a/babel.config.js +++ b/babel.config.js @@ -58,7 +58,8 @@ module.exports = function (api) { */ [ './plugins/babel-plugin-lexicon-leaf-imports', - {roots: ['./src/lexicons']}, + // Absolute path: the plugin must not depend on the host process's cwd. + {roots: [require('path').join(__dirname, 'src/lexicons')]}, ], // cannot use `env` field because it will put them after diff --git a/plugins/babel-plugin-lexicon-leaf-imports.js b/plugins/babel-plugin-lexicon-leaf-imports.js index 0c564337d7..91d07c4b1f 100644 --- a/plugins/babel-plugin-lexicon-leaf-imports.js +++ b/plugins/babel-plugin-lexicon-leaf-imports.js @@ -172,9 +172,18 @@ module.exports = function lexiconLeafImports(babel, options = {}) { * Absolute paths of barrel directories (e.g. /src/lexicons). Matched * against the import specifier after resolving it relative to the importing * file, because babel-plugin-module-resolver has already turned '#/lexicons' - * into a relative path by the time this plugin runs. + * into a relative path by the time this plugin runs. Relative entries are + * resolved against Babel's root, not process.cwd(): cwd depends on how the + * host process (Metro worker, Jest, an IDE runner) was launched, and a wrong + * base would silently disable every rewrite. */ - const roots = new Set((options.roots ?? []).map(r => path.resolve(r))) + let roots = null + function resolveRoots(state) { + if (!roots) { + const base = state.file.opts.root ?? state.cwd + roots = new Set((options.roots ?? []).map(r => path.resolve(base, r))) + } + } const debug = !!process.env.BSKY_LEXICON_IMPORTS_DEBUG const stats = {files: 0, rewrites: 0, bails: 0} @@ -244,6 +253,7 @@ module.exports = function lexiconLeafImports(babel, options = {}) { exit(programPath, state) { const filename = state.filename if (!filename) return + resolveRoots(state) const targets = [] for (const stmt of programPath.get('body')) { if (!stmt.isImportDeclaration()) continue