Skip to content

Commit 3b156c9

Browse files
zouguangxianarcanismerceyz
authored
fix(core): determine if a module is a builtin using module.isBuiltin (#5997)
**What's the problem this PR addresses?** <!-- Describe the rationale of your PR. --> `builder plugin` may output `node:process` instead of `process` in the generated file. When executing `yarn.`, it will report an error like: ``` This plugin cannot access the package referenced via node:process which is neither a builtin, nor an exposed entry ``` ** Environment ``` ❯ yarn tsc --version Version 5.3.2 ``` <!-- Link all issues that it closes. (Closes/Resolves #xxxx.) --> ... **How did you fix it?** <!-- A detailed description of your implementation. --> **Checklist** <!--- Don't worry if you miss something, chores are automatically tested. --> <!--- This checklist exists to help you remember doing the chores when you submit a PR. --> <!--- Put an `x` in all the boxes that apply. --> - [x] I have read the [Contributing Guide](https://yarnpkg.com/advanced/contributing). <!-- See https://yarnpkg.com/advanced/contributing#preparing-your-pr-to-be-released for more details. --> <!-- Check with `yarn version check` and fix with `yarn version check -i` --> - [x] I have set the packages that need to be released for my changes to be effective. <!-- The "Testing chores" workflow validates that your PR follows our guidelines. --> <!-- If it doesn't pass, click on it to see details as to what your PR might be missing. --> - [x] I will check that all automated PR checks pass before the PR gets reviewed. --------- Co-authored-by: Maël Nison <nison.mael@gmail.com> Co-authored-by: Kristoffer K. <merceyz@users.noreply.github.com>
1 parent cadd19e commit 3b156c9

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

.yarn/versions/44a33643.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
releases:
2+
"@yarnpkg/cli": patch
3+
"@yarnpkg/core": patch
4+
5+
declined:
6+
- "@yarnpkg/plugin-compat"
7+
- "@yarnpkg/plugin-constraints"
8+
- "@yarnpkg/plugin-dlx"
9+
- "@yarnpkg/plugin-essentials"
10+
- "@yarnpkg/plugin-exec"
11+
- "@yarnpkg/plugin-file"
12+
- "@yarnpkg/plugin-git"
13+
- "@yarnpkg/plugin-github"
14+
- "@yarnpkg/plugin-http"
15+
- "@yarnpkg/plugin-init"
16+
- "@yarnpkg/plugin-interactive-tools"
17+
- "@yarnpkg/plugin-link"
18+
- "@yarnpkg/plugin-nm"
19+
- "@yarnpkg/plugin-npm"
20+
- "@yarnpkg/plugin-npm-cli"
21+
- "@yarnpkg/plugin-pack"
22+
- "@yarnpkg/plugin-patch"
23+
- "@yarnpkg/plugin-pnp"
24+
- "@yarnpkg/plugin-pnpm"
25+
- "@yarnpkg/plugin-stage"
26+
- "@yarnpkg/plugin-typescript"
27+
- "@yarnpkg/plugin-version"
28+
- "@yarnpkg/plugin-workspace-tools"
29+
- "@yarnpkg/builder"
30+
- "@yarnpkg/doctor"
31+
- "@yarnpkg/extensions"
32+
- "@yarnpkg/nm"
33+
- "@yarnpkg/pnpify"
34+
- "@yarnpkg/sdks"

packages/acceptance-tests/pkg-tests-specs/sources/features/plugins.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {mockPluginServer} from './plugins.utility';
88
const COMMANDS_PLUGIN = (name: string, {async = false, printOnBoot = false, thirdParty = false} = {}) => `
99
const factory = ${async ? `async` : ``} r => {
1010
const {Command} = r('clipanion');
11+
const path = r('node:path');
1112
1213
if (${printOnBoot})
1314
console.log('Booting ${name.toUpperCase()}');
@@ -22,6 +23,14 @@ const factory = ${async ? `async` : ``} r => {
2223
this.context.stdout.write('Executing ${name.toUpperCase()}\\n');
2324
}
2425
},
26+
27+
class MyCommandPath extends Command {
28+
static paths = [['${name}', 'path']];
29+
30+
async execute() {
31+
this.context.stdout.write(path.posix.join('a', 'b') + '\\n');
32+
}
33+
},
2534
],
2635
},
2736
};
@@ -77,6 +86,19 @@ describe(`Features`, () => {
7786
});
7887
}));
7988

89+
test(`it should support plugins using builtin modules`, makeTemporaryEnv({
90+
}, async ({path, run, source}) => {
91+
await xfs.writeFilePromise(`${path}/plugin-a.js` as PortablePath, COMMANDS_PLUGIN(`a`));
92+
93+
await xfs.writeFilePromise(`${path}/.yarnrc.yml` as PortablePath, stringifySyml({
94+
plugins: [`./plugin-a.js`],
95+
}));
96+
97+
await expect(run(`a`, `path`)).resolves.toMatchObject({
98+
stdout: `a/b\n`,
99+
});
100+
}));
101+
80102
test(`it should accept asynchronous plugins`, makeTemporaryEnv({
81103
}, async ({path, run, source}) => {
82104
await xfs.writeFilePromise(`${path}/plugin-a.js` as PortablePath, COMMANDS_PLUGIN(`a`, {async: true}));

packages/yarnpkg-core/sources/Configuration.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import camelcase
55
import {isCI, isPR, GITHUB_ACTIONS} from 'ci-info';
66
import {UsageError} from 'clipanion';
77
import {parse as parseDotEnv} from 'dotenv';
8-
import {builtinModules} from 'module';
8+
import {isBuiltin} from 'module';
99
import pLimit, {Limit} from 'p-limit';
1010
import {PassThrough, Writable} from 'stream';
1111
import {WriteStream} from 'tty';
@@ -1265,8 +1265,7 @@ export class Configuration {
12651265
const thirdPartyPlugins = new Map<string, Plugin>([]);
12661266
if (pluginConfiguration !== null) {
12671267
const requireEntries = new Map();
1268-
for (const request of builtinModules)
1269-
requireEntries.set(request, () => miscUtils.dynamicRequire(request));
1268+
12701269
for (const [request, embedModule] of pluginConfiguration.modules)
12711270
requireEntries.set(request, () => embedModule);
12721271

@@ -1284,6 +1283,9 @@ export class Configuration {
12841283

12851284
const pluginRequireEntries = new Map(requireEntries);
12861285
const pluginRequire = (request: string) => {
1286+
if (isBuiltin(request))
1287+
return miscUtils.dynamicRequire(request);
1288+
12871289
if (pluginRequireEntries.has(request)) {
12881290
return pluginRequireEntries.get(request)();
12891291
} else {

0 commit comments

Comments
 (0)