diff --git a/.yarn/versions/2f94a32c.yml b/.yarn/versions/2f94a32c.yml new file mode 100644 index 000000000000..2d6a48f178d8 --- /dev/null +++ b/.yarn/versions/2f94a32c.yml @@ -0,0 +1,34 @@ +releases: + "@yarnpkg/cli": patch + "@yarnpkg/core": patch + +declined: + - "@yarnpkg/plugin-compat" + - "@yarnpkg/plugin-constraints" + - "@yarnpkg/plugin-dlx" + - "@yarnpkg/plugin-essentials" + - "@yarnpkg/plugin-exec" + - "@yarnpkg/plugin-file" + - "@yarnpkg/plugin-git" + - "@yarnpkg/plugin-github" + - "@yarnpkg/plugin-http" + - "@yarnpkg/plugin-init" + - "@yarnpkg/plugin-interactive-tools" + - "@yarnpkg/plugin-link" + - "@yarnpkg/plugin-nm" + - "@yarnpkg/plugin-npm" + - "@yarnpkg/plugin-npm-cli" + - "@yarnpkg/plugin-pack" + - "@yarnpkg/plugin-patch" + - "@yarnpkg/plugin-pnp" + - "@yarnpkg/plugin-pnpm" + - "@yarnpkg/plugin-stage" + - "@yarnpkg/plugin-typescript" + - "@yarnpkg/plugin-version" + - "@yarnpkg/plugin-workspace-tools" + - "@yarnpkg/builder" + - "@yarnpkg/doctor" + - "@yarnpkg/extensions" + - "@yarnpkg/nm" + - "@yarnpkg/pnpify" + - "@yarnpkg/sdks" diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fe446f1e04e..0add312a3608 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,7 @@ The following changes only affect people writing Yarn plugins: - The patched filesystem now supports `ftruncate`. - The patched filesystem now supports `fchmod`. - Updates the PnP compatibility layer for TypeScript 4.8 Beta +- The `npm_package_json` environment variable is now set by Yarn. ## 3.2.1 diff --git a/packages/acceptance-tests/pkg-tests-specs/sources/script.test.js b/packages/acceptance-tests/pkg-tests-specs/sources/script.test.js index 2a4999b207de..d1a70ef2025f 100644 --- a/packages/acceptance-tests/pkg-tests-specs/sources/script.test.js +++ b/packages/acceptance-tests/pkg-tests-specs/sources/script.test.js @@ -1,4 +1,4 @@ -const {npath, ppath, xfs} = require(`@yarnpkg/fslib`); +const {npath, ppath, xfs, Filename} = require(`@yarnpkg/fslib`); const {isAbsolute, resolve} = require(`path`); const { @@ -157,6 +157,7 @@ describe(`Scripts tests`, () => { expect(env).toMatchObject({ npm_package_name: `helloworld`, npm_package_version: `1.2.3`, + npm_package_json: npath.join(npath.fromPortablePath(path), Filename.manifest), }); }), ); diff --git a/packages/gatsby/content/advanced/lifecycle-scripts.md b/packages/gatsby/content/advanced/lifecycle-scripts.md index fc00b8e9e355..b04e7c6de546 100644 --- a/packages/gatsby/content/advanced/lifecycle-scripts.md +++ b/packages/gatsby/content/advanced/lifecycle-scripts.md @@ -45,6 +45,8 @@ When running scripts and binaries, some environment variables are usually made a - `$npm_package_version` is its version. +- `$npm_package_json` is the absolute path to its `package.json`. + - `$npm_execpath` is the path to the Yarn binary. - `$npm_node_execpath` is the path to the Node binary. diff --git a/packages/yarnpkg-core/sources/scriptUtils.ts b/packages/yarnpkg-core/sources/scriptUtils.ts index 6aa4fdd3ea8a..3004405925ac 100644 --- a/packages/yarnpkg-core/sources/scriptUtils.ts +++ b/packages/yarnpkg-core/sources/scriptUtils.ts @@ -158,6 +158,26 @@ export async function makeScriptEnv({project, locator, binFolder, lifecycleScrip scriptEnv.npm_package_name = structUtils.stringifyIdent(locator); scriptEnv.npm_package_version = version; + + let packageLocation: PortablePath; + if (workspace) { + packageLocation = workspace.cwd; + } else { + const pkg = project.storedPackages.get(locator.locatorHash); + if (!pkg) + throw new Error(`Package for ${structUtils.prettyLocator(project.configuration, locator)} not found in the project`); + + const linkers = project.configuration.getLinkers(); + const linkerOptions = {project, report: new StreamReport({stdout: new PassThrough(), configuration: project.configuration})}; + + const linker = linkers.find(linker => linker.supportsPackage(pkg, linkerOptions)); + if (!linker) + throw new Error(`The package ${structUtils.prettyLocator(project.configuration, pkg)} isn't supported by any of the available linkers`); + + packageLocation = await linker.findPackageLocation(pkg, linkerOptions); + } + + scriptEnv.npm_package_json = npath.fromPortablePath(ppath.join(packageLocation, Filename.manifest)); } const version = YarnVersion !== null