Skip to content

Commit 2fba44c

Browse files
jfirebaughVincentBailly
authored andcommitted
Run the prepare script of git dependencies even if NODE_ENV=production (yarnpkg#7398)
* Run the prepare script of git dependencies even if NODE_ENV=production When installing a git dependency with the `NODE_ENV` environment variable set to `production`, yarn did not run the `prepare` lifecycle script, because `config.production` would default to true, and then `wrapLifecycle` would skip `prepare`: https://github.com/yarnpkg/yarn/blob/b6569538de69e0ccd201f0a33f1f5b52f2656f5b/src/cli/commands/install.js#L1202-L1207 This is contrary to npm's behavior and the intent of `GitFetcher#fetchFromInstallAndPack`, which is to install the dependency with its devDependencies and run all the relevant lifecycle scripts (excluding `prepublish`), regardless of the configuration or environment settings of the parent install command. * Update CHANGELOG.md * Update CHANGELOG.md
1 parent 984e055 commit 2fba44c

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Please add one entry in this file for each change in Yarn's behavior. Use the sa
44

55
## Master
66

7+
- Runs the `prepare` lifecycle of git dependencies even if `NODE_ENV` is set to `production`.
8+
9+
[#7398](https://github.com/yarnpkg/yarn/pull/7398) - [**John Firebaugh**](https://github.com/jfirebaugh)
10+
711
- Fixes the `postversion` lifecycle method not being called when using `--no-git-tag-version`.
812

913
[#7154](https://github.com/yarnpkg/yarn/pull/7154) - [**Hampus Tågerud**](https://github.com/hampustagerud)

__tests__/fetchers.js

+34
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,40 @@ test('GitFetcher.fetch with prepare script', async () => {
125125
expect(await fs.exists(path.join(dir, 'generated', 'prepublish'))).toBe(false);
126126
});
127127

128+
test('GitFetcher.fetch with prepare script, NODE_ENV=production', async () => {
129+
const NODE_ENV = process.env.NODE_ENV;
130+
try {
131+
process.env.NODE_ENV = 'production';
132+
const dir = await mkdir('git-fetcher-with-prepare');
133+
const fetcher = new GitFetcher(
134+
dir,
135+
{
136+
type: 'git',
137+
reference: 'https://github.com/Volune/test-js-git-repo',
138+
hash: '0e56593e326069ed4bcec8126bb48a1891215c57',
139+
registry: 'npm',
140+
},
141+
await Config.create(),
142+
);
143+
await fetcher.fetch();
144+
const name = (await fs.readJson(path.join(dir, 'package.json'))).name;
145+
expect(name).toBe('test-js-git-repo');
146+
const dependencyName = (await fs.readJson(path.join(dir, 'dependency-package.json'))).name;
147+
expect(dependencyName).toBe('beeper');
148+
// The file "prepare.js" is not in "files" list
149+
expect(await fs.exists(path.join(dir, 'prepare.js'))).toBe(false);
150+
// Check the dependency with a bin script was correctly executed
151+
expect(await fs.exists(path.join(dir, 'testscript.output.txt'))).toBe(true);
152+
// Check executed lifecycle scripts
153+
expect(await fs.exists(path.join(dir, 'generated', 'preinstall'))).toBe(true);
154+
expect(await fs.exists(path.join(dir, 'generated', 'install'))).toBe(true);
155+
expect(await fs.exists(path.join(dir, 'generated', 'postinstall'))).toBe(true);
156+
expect(await fs.exists(path.join(dir, 'generated', 'prepublish'))).toBe(false);
157+
} finally {
158+
process.env.NODE_ENV = NODE_ENV;
159+
}
160+
});
161+
128162
test('TarballFetcher.fetch', async () => {
129163
const dir = await mkdir('tarball-fetcher');
130164
const fetcher = new TarballFetcher(

src/fetchers/git-fetcher.js

+1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ export default class GitFetcher extends BaseFetcher {
164164
binLinks: true,
165165
cwd: prepareDirectory,
166166
disablePrepublish: true,
167+
production: false,
167168
},
168169
this.reporter,
169170
),

0 commit comments

Comments
 (0)