From a29aca1a0b7757740a46456be814bd439e972f21 Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Thu, 7 May 2020 18:12:48 +0200 Subject: [PATCH] fix(gatsby-dev-cli): fix "expected manifest" errors (#23871) * fix: ignore package.json changes during publishing * use concrete version instead of `gatsby-dev` dist-tag - in some scenarios, using dist-tag cause "expected manifest" invariant errors * increase npm registry retry limit, on flaky connections default limit was causing lot of interrupted installs --- .../src/local-npm-registry/index.js | 10 ++++++++-- .../src/local-npm-registry/install-packages.js | 12 ++++++++++-- .../src/local-npm-registry/publish-package.js | 18 ++++++++++++++++-- .../src/local-npm-registry/verdaccio-config.js | 2 ++ packages/gatsby-dev-cli/src/watch.js | 9 +++++++++ 5 files changed, 45 insertions(+), 6 deletions(-) diff --git a/packages/gatsby-dev-cli/src/local-npm-registry/index.js b/packages/gatsby-dev-cli/src/local-npm-registry/index.js index 07838627b6f49..334b42d7a9bd6 100644 --- a/packages/gatsby-dev-cli/src/local-npm-registry/index.js +++ b/packages/gatsby-dev-cli/src/local-npm-registry/index.js @@ -53,8 +53,10 @@ exports.publishPackagesLocallyAndInstall = async ({ const versionPostFix = Date.now() + const newlyPublishedPackageVersions = {} + for (let packageName of packagesToPublish) { - await publishPackage({ + newlyPublishedPackageVersions[packageName] = await publishPackage({ packageName, packagesToPublish, root, @@ -65,5 +67,9 @@ exports.publishPackagesLocallyAndInstall = async ({ const packagesToInstall = _.intersection(packagesToPublish, localPackages) - await installPackages({ packagesToInstall, yarnWorkspaceRoot }) + await installPackages({ + packagesToInstall, + yarnWorkspaceRoot, + newlyPublishedPackageVersions, + }) } diff --git a/packages/gatsby-dev-cli/src/local-npm-registry/install-packages.js b/packages/gatsby-dev-cli/src/local-npm-registry/install-packages.js index 44943ee94fe10..ddbf9514bc838 100644 --- a/packages/gatsby-dev-cli/src/local-npm-registry/install-packages.js +++ b/packages/gatsby-dev-cli/src/local-npm-registry/install-packages.js @@ -4,7 +4,11 @@ const fs = require(`fs-extra`) const { promisifiedSpawn } = require(`../utils/promisified-spawn`) const { registryUrl } = require(`./verdaccio-config`) -const installPackages = async ({ packagesToInstall, yarnWorkspaceRoot }) => { +const installPackages = async ({ + packagesToInstall, + yarnWorkspaceRoot, + newlyPublishedPackageVersions, +}) => { console.log( `Installing packages from local registry:\n${packagesToInstall .map(packageAndVersion => ` - ${packageAndVersion}`) @@ -71,7 +75,10 @@ const installPackages = async ({ packagesToInstall, yarnWorkspaceRoot }) => { `yarn`, [ `add`, - ...packagesToInstall.map(packageName => `${packageName}@gatsby-dev`), + ...packagesToInstall.map(packageName => { + const packageVersion = newlyPublishedPackageVersions[packageName] + return `${packageName}@${packageVersion}` + }), `--registry=${registryUrl}`, `--exact`, ], @@ -84,6 +91,7 @@ const installPackages = async ({ packagesToInstall, yarnWorkspaceRoot }) => { console.log(`Installation complete`) } catch (error) { console.error(`Installation failed`, error) + process.exit(1) } } diff --git a/packages/gatsby-dev-cli/src/local-npm-registry/publish-package.js b/packages/gatsby-dev-cli/src/local-npm-registry/publish-package.js index b6c08510dd514..763b2e988e355 100644 --- a/packages/gatsby-dev-cli/src/local-npm-registry/publish-package.js +++ b/packages/gatsby-dev-cli/src/local-npm-registry/publish-package.js @@ -27,6 +27,7 @@ const adjustPackageJson = ({ versionPostFix, packagesToPublish, ignorePackageJSONChanges, + root, }) => { // we need to check if package depend on any other package to will be published and // adjust version selector to point to dev version of package so local registry is used @@ -44,8 +45,19 @@ const adjustPackageJson = ({ monorepoPKGjson.dependencies && monorepoPKGjson.dependencies[packageThatWillBePublished] ) { - // change to "gatsby-dev" dist tag - monorepoPKGjson.dependencies[packageThatWillBePublished] = `gatsby-dev` + const currentVersion = JSON.parse( + fs.readFileSync( + getMonorepoPackageJsonPath({ + packageName: packageThatWillBePublished, + root, + }), + `utf-8` + ) + ).version + + monorepoPKGjson.dependencies[ + packageThatWillBePublished + ] = `${currentVersion}-dev-${versionPostFix}` } }) @@ -133,6 +145,8 @@ const publishPackage = async ({ uncreateTemporaryNPMRC() unadjustPackageJson() + + return newPackageVersion } exports.publishPackage = publishPackage diff --git a/packages/gatsby-dev-cli/src/local-npm-registry/verdaccio-config.js b/packages/gatsby-dev-cli/src/local-npm-registry/verdaccio-config.js index c95574e547d72..2502a5acd6af5 100644 --- a/packages/gatsby-dev-cli/src/local-npm-registry/verdaccio-config.js +++ b/packages/gatsby-dev-cli/src/local-npm-registry/verdaccio-config.js @@ -19,6 +19,8 @@ const verdaccioConfig = { uplinks: { npmjs: { url: `https://registry.npmjs.org/`, + // default is 2 max_fails - on flaky networks that cause a lot of failed installations + max_fails: 10, }, }, } diff --git a/packages/gatsby-dev-cli/src/watch.js b/packages/gatsby-dev-cli/src/watch.js index cd5930f6ddbf1..a5a8cef4b65f8 100644 --- a/packages/gatsby-dev-cli/src/watch.js +++ b/packages/gatsby-dev-cli/src/watch.js @@ -176,6 +176,7 @@ async function watch( let allCopies = [] const packagesToPublish = new Set() let isInitialScan = true + let isPublishing = false const waitFor = new Set() let anyPackageNotInstalled = false @@ -211,6 +212,12 @@ async function watch( ) if (relativePackageFile === `package.json`) { + // package.json files will change during publish to adjust version of package (and dependencies), so ignore + // changes during this process + if (isPublishing) { + return + } + // Compare dependencies with local version const didDepsChangedPromise = checkDepsChanges({ @@ -292,6 +299,7 @@ async function watch( if (isInitialScan) { isInitialScan = false if (packagesToPublish.size > 0) { + isPublishing = true await publishPackagesLocallyAndInstall({ packagesToPublish: Array.from(packagesToPublish), root, @@ -299,6 +307,7 @@ async function watch( ignorePackageJSONChanges, }) packagesToPublish.clear() + isPublishing = false } else if (anyPackageNotInstalled) { // run `yarn` const yarnInstallCmd = [`yarn`]