Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): set npm_package_json environment variable when running scripts #4546

Merged
merged 9 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .yarn/versions/2f94a32c.yml
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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),
});
}),
);
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby/content/advanced/lifecycle-scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions packages/yarnpkg-core/sources/scriptUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down