Skip to content

Commit 9855c52

Browse files
authored
fix: run migrator in a separator process, fix require cache issues during upgrade (#5360)
* fix: run migrator in a separator process, fix require cache issues during upgrade * fix: fix cwd * test: fix test mocks * fix: early return
1 parent 4659869 commit 9855c52

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

packages/@vue/cli/lib/Upgrader.js

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const fs = require('fs')
22
const path = require('path')
33
const {
44
chalk,
5+
execa,
56
semver,
67

78
log,
@@ -12,19 +13,17 @@ const {
1213
isPlugin,
1314
resolvePluginId,
1415

15-
loadModule
16+
loadModule,
17+
resolveModule
1618
} = require('@vue/cli-shared-utils')
1719

1820
const tryGetNewerRange = require('./util/tryGetNewerRange')
1921
const getPkg = require('./util/getPkg')
2022
const PackageManager = require('./util/ProjectPackageManager')
2123

22-
const { runMigrator } = require('./migrate')
23-
2424
function clearRequireCache () {
2525
Object.keys(require.cache).forEach(key => delete require.cache[key])
2626
}
27-
2827
module.exports = class Upgrader {
2928
constructor (context = process.cwd()) {
3029
this.context = context
@@ -108,28 +107,39 @@ module.exports = class Upgrader {
108107

109108
log(`Upgrading ${packageName} from ${installed} to ${targetVersion}`)
110109
await this.pm.upgrade(`${packageName}@~${targetVersion}`)
111-
// as the dependencies have now changed, the require cache must be invalidated
112-
// otherwise it may affect the behavior of the migrator
113-
clearRequireCache()
114110

115111
// The cached `pkg` field won't automatically update after running `this.pm.upgrade`.
116112
// Also, `npm install pkg@~version` won't replace the original `"pkg": "^version"` field.
117113
// So we have to manually update `this.pkg` and write to the file system in `runMigrator`
118114
this.pkg[depEntry][packageName] = `~${targetVersion}`
119-
const noop = () => {}
120-
121-
const pluginMigrator =
122-
loadModule(`${packageName}/migrator`, this.context) || noop
123-
124-
await runMigrator(
125-
this.context,
126-
{
127-
id: packageName,
128-
apply: pluginMigrator,
129-
baseVersion: installed
130-
},
131-
this.pkg
132-
)
115+
116+
const resolvedPluginMigrator =
117+
resolveModule(`${packageName}/migrator`, this.context)
118+
119+
if (resolvedPluginMigrator) {
120+
// for unit tests, need to run migrator in the same process for mocks to work
121+
// TODO: fix the tests and remove this special case
122+
if (process.env.VUE_CLI_TEST) {
123+
clearRequireCache()
124+
await require('./migrate').runMigrator(
125+
this.context,
126+
{
127+
id: packageName,
128+
apply: loadModule(`${packageName}/migrator`, this.context),
129+
baseVersion: installed
130+
},
131+
this.pkg
132+
)
133+
return
134+
}
135+
136+
const cliBin = path.resolve(__dirname, '../bin/vue.js')
137+
// Run migrator in a separate process to avoid all kinds of require cache issues
138+
await execa('node', [cliBin, 'migrate', packageName, '--from', installed], {
139+
cwd: this.context,
140+
stdio: 'inherit'
141+
})
142+
}
133143
}
134144

135145
async getUpgradable (includeNext) {

0 commit comments

Comments
 (0)