Skip to content

Commit

Permalink
Retry and ignore npm errors in dtslint (#979)
Browse files Browse the repository at this point in the history
* Retry and ignore npm errors

* Add changeset

* Remove debug code

* Regenerate pnpm-lock changes
  • Loading branch information
andrewbranch committed Mar 8, 2024
1 parent a0b5f0d commit e32483b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-apes-tan.md
@@ -0,0 +1,5 @@
---
"@definitelytyped/dtslint": patch
---

Retry and ignore npm errors
4 changes: 2 additions & 2 deletions packages/dtslint/package.json
Expand Up @@ -22,8 +22,8 @@
"test": "../../node_modules/.bin/jest --config ../../jest.config.js packages/dtslint"
},
"dependencies": {
"@arethetypeswrong/cli": "0.14.1",
"@arethetypeswrong/core": "0.14.1",
"@arethetypeswrong/cli": "0.15.1",
"@arethetypeswrong/core": "0.15.1",
"@definitelytyped/header-parser": "workspace:*",
"@definitelytyped/typescript-packages": "workspace:*",
"@definitelytyped/typescript-versions": "workspace:*",
Expand Down
38 changes: 31 additions & 7 deletions packages/dtslint/src/checks.ts
Expand Up @@ -242,11 +242,18 @@ export async function checkNpmVersionAndGetMatchingImplementationPackage(
let implementationPackage;
const attw = await import("@arethetypeswrong/core");
const typesPackageVersion = `${packageJson.libraryMajorVersion}.${packageJson.libraryMinorVersion}`;
const packageId = await tryPromise(
attw.resolveImplementationPackageForTypesPackage(packageJson.name, `${typesPackageVersion}.9999`, {
allowDeprecated: true,
}),
);
let packageId;
try {
packageId = await retryNon404Errors(
() => attw.resolveImplementationPackageForTypesPackage(packageJson.name, `${typesPackageVersion}.9999`, {
allowDeprecated: true,
}),
);
} catch (err: any) {
warnings.push(err?.message);
return { warnings, errors };
}

const npmVersionExemptions = await getExpectedNpmVersionFailures();
if (packageId) {
const { packageName, packageVersion, tarballUrl } = packageId;
Expand Down Expand Up @@ -310,6 +317,23 @@ export async function checkNpmVersionAndGetMatchingImplementationPackage(
};
}

function tryPromise<T>(promise: Promise<T>): Promise<T | undefined> {
return promise.catch(() => undefined);
async function retryNon404Errors<T>(action: () => Promise<T>): Promise<T | undefined> {
let lastError: Error | undefined;
for (let i = 0; i < 3; i++) {
try {
return await action();
} catch (err: any) {
if ((err as attw.Npm404Error).kind !== "Npm404Error") {
lastError = err;
if (i < 2) await delay(1000);
} else {
return undefined;
}
}
}
throw new Error(`Skipping attw due to unexpected error fetching implementation package in multiple attempts. Last error: ${lastError?.stack ?? lastError?.message}`);

function delay(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
}
18 changes: 9 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e32483b

Please sign in to comment.