Przeglądaj źródła

Handle updating dependencies without a semver range prefix (#10393)

* Handle updating dependencies without a semver range specifier

When updating a dependency that does not have a semver range prefix (^, ~, etc.), and not giving a prefix for the update so it should take any prefix from the existing version, we were running into problems with using the undefined value of the existing version.

In other words, when trying to update a dependency with version requirement 1.4.0 (note there is no semver range prefix - we just want an exact version) with arguments such as ‘update mypackage latest’, we would end up calculating a desired version of undefinedlatest, since the semver range prefix ended up being undefined from our regex matches against 1.4.0.

This change handles the case where matching for a semver range prefix returns an undefined prefix.

As an example, change the version requirement of @lumino/widgets in packages/application/package.json to 1.0.0 (note there is no semver range prefix). Then run:

jlpm run update:dependency '@lumino/widgets' latest --dry-run

Note there is an error like:

VersionNotFoundError: Version `undefinedlatest` for package `@lumino/widgets` could not be found


Now apply this change, and you’ll notice the dependency now updates, and correctly stays pinned to a single version:

jupyterlab/packages/application/package.json
@lumino/widgets 1.0.0 -> 1.23.0

* Lint

Co-authored-by: Jeremy Tuloup <jeremy.tuloup@gmail.com>
Jason Grout 3 lat temu
rodzic
commit
46e2b9bc46
1 zmienionych plików z 8 dodań i 6 usunięć
  1. 8 6
      buildutils/src/update-dependency.ts

+ 8 - 6
buildutils/src/update-dependency.ts

@@ -55,12 +55,9 @@ async function getSpecifier(
         `Current version range is not recognized: ${currentSpecifier}`
       );
     }
-    const [, currentSigil] = match;
-    if (currentSigil) {
-      suggestedSigil = currentSigil;
-    }
+    suggestedSigil = match[1];
   }
-  return `${suggestedSigil}${suggestedTag}`;
+  return `${suggestedSigil ?? ''}${suggestedTag}`;
 }
 
 async function getVersion(pkg: string, specifier: string) {
@@ -77,7 +74,12 @@ async function getVersion(pkg: string, specifier: string) {
 
     // Look up the actual version corresponding to the tag
     const { version } = await packageJson(pkg, { version: match[2] });
-    specifier = match[1] + version;
+    specifier = `${match[1] ?? ''}${version}`;
+    if (semver.validRange(specifier) === null) {
+      throw Error(
+        `Could not find valid version range for ${pkg}: ${specifier}`
+      );
+    }
   }
   versionCache.set(key, specifier);
   return specifier;