Skip to content

Commit 6d61989

Browse files
authored
fix(yarn-plugin-dynamic-extensions): load user extensions relative to source config file (#3543)
1 parent 356eea3 commit 6d61989

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

.changeset/fresh-rice-begin.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@rnx-kit/yarn-plugin-dynamic-extensions": patch
3+
---
4+
5+
Load user extensions relative to the source config file.

docsite/.yarnrc.yml

-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,5 @@ logFilters:
1414
level: discard
1515
nodeLinker: node-modules
1616
npmRegistryServer: "https://registry.npmjs.org"
17-
dynamicPackageExtensions: false
1817
tsEnableAutoTypes: false
1918
yarnPath: ../.yarn/releases/yarn-4.6.0.cjs

incubator/yarn-plugin-dynamic-extensions/index.js

+17-13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/**
44
* @import { Configuration, Hooks, Manifest, PackageExtensionData, Plugin } from "@yarnpkg/core";
5+
* @import { PortablePath } from "@yarnpkg/fslib";
56
* @typedef {{ cwd: string; manifest: Manifest["raw"]; }} Workspace;
67
*/
78

@@ -14,26 +15,29 @@ exports.name = "@rnx-kit/yarn-plugin-dynamic-extensions";
1415
/** @type {(require: NodeJS.Require) => Plugin<Hooks>} */
1516
exports.factory = (require) => {
1617
const { Project, SettingsType, structUtils } = require("@yarnpkg/core");
18+
const { npath } = require("@yarnpkg/fslib");
1719

1820
/**
1921
* @param {Configuration} configuration
20-
* @param {string} projectRoot
22+
* @param {PortablePath} projectRoot
2123
* @returns {Promise<((ws: Workspace) => PackageExtensionData | undefined) | void>}
2224
*/
2325
async function loadUserExtensions(configuration, projectRoot) {
2426
const packageExtensions = configuration.get(DYNAMIC_PACKAGE_EXTENSIONS_KEY);
25-
if (
26-
typeof packageExtensions !== "string" ||
27-
packageExtensions === "false"
28-
) {
27+
if (typeof packageExtensions !== "string") {
2928
return;
3029
}
3130

3231
const path = require("node:path");
3332
const { pathToFileURL } = require("node:url");
3433

34+
// Make sure we resolve user extensions relative to the source config
35+
const source = configuration.sources.get(DYNAMIC_PACKAGE_EXTENSIONS_KEY);
36+
const sourceDir = source ? npath.dirname(source) : projectRoot;
37+
3538
// On Windows, import paths must include the `file:` protocol.
36-
const url = pathToFileURL(path.resolve(projectRoot, packageExtensions));
39+
const root = npath.fromPortablePath(sourceDir);
40+
const url = pathToFileURL(path.resolve(root, packageExtensions));
3741
const external = await import(url.toString());
3842
return external?.default ?? external;
3943
}
@@ -57,16 +61,16 @@ exports.factory = (require) => {
5761
return;
5862
}
5963

60-
const { workspace } = await Project.find(configuration, projectCwd);
61-
if (!workspace) {
64+
const getUserExtensions = await loadUserExtensions(
65+
configuration,
66+
projectCwd
67+
);
68+
if (!getUserExtensions) {
6269
return;
6370
}
6471

65-
const { npath } = require("@yarnpkg/fslib");
66-
67-
const root = npath.fromPortablePath(projectCwd);
68-
const getUserExtensions = await loadUserExtensions(configuration, root);
69-
if (!getUserExtensions) {
72+
const { workspace } = await Project.find(configuration, projectCwd);
73+
if (!workspace) {
7074
return;
7175
}
7276

0 commit comments

Comments
 (0)