Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(publish), do not fail the build if npm throws EPERM and the version actually published #8789

Merged
merged 4 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions pnpm-lock.yaml

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

32 changes: 30 additions & 2 deletions scopes/pkg/pkg/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Scope } from '@teambit/legacy/dist/scope';
import fsx from 'fs-extra';
import mapSeries from 'p-map-series';
import { join } from 'path';
import ssri from 'ssri';
import execa from 'execa';
import { PkgAspect } from './pkg.aspect';
import { PkgExtensionConfig } from './pkg.main.runtime';
Expand Down Expand Up @@ -68,7 +69,7 @@ export class Publisher {
publishParams.push(...extraArgsSplit);
}
const publishParamsStr = publishParams.join(' ');

const getPkgJson = async () => fsx.readJSON(`${capsule.path}/package.json`);
const componentIdStr = capsule.id.toString();
const errors: string[] = [];
try {
Expand All @@ -82,7 +83,23 @@ export class Publisher {
const errorDetails = typeof err === 'object' && err && 'message' in err ? err.message : err;
const errorMsg = `failed running ${this.packageManager} ${publishParamsStr} at ${cwd}: ${errorDetails}`;
this.logger.error(`${componentIdStr}, ${errorMsg}`);
errors.push(errorMsg);
let isPublished = false;
if (typeof errorDetails === 'string' && errorDetails.includes('EPERM') && tarPath) {
const pkgJson = await getPkgJson();
// sleep 5 seconds
await new Promise((resolve) => setTimeout(resolve, Number(process.env.NPM_WAKE_UP || 5000)));
const integrityOnNpm = await this.getIntegrityOnNpm(pkgJson.name, pkgJson.version);
if (integrityOnNpm && tarPath) {
const tarData = fsx.readFileSync(join(tarFolderPath, tarPath));
// If the integrity of the tarball in the registry matches the local one,
// we consider the package published
isPublished = ssri.checkData(tarData, integrityOnNpm) !== false;
this.logger.debug(
`${componentIdStr}, package ${pkgJson.name} is already on npm with version ${pkgJson.version}`
);
}
}
if (!isPublished) errors.push(errorMsg);
}
let metadata: TaskMetadata = {};
if (errors.length === 0 && !this.options.dryRun) {
Expand All @@ -93,6 +110,17 @@ export class Publisher {
return { component, metadata, errors, startTime, endTime: Date.now() };
}

private async getIntegrityOnNpm(pkgName: string, pkgVersion: string): Promise<string | undefined> {
const args = ['view', `${pkgName}@${pkgVersion}`, 'dist.integrity'];
try {
const results = await execa(this.packageManager, args);
return results.stdout;
} catch (err: unknown) {
this.logger.error(`failed running ${this.packageManager} ${args.join(' ')}: ${err}`, err);
return undefined;
}
}

private getTagFlagForPreRelease(id: ComponentID): string[] {
const preReleaseData = id.getVersionPreReleaseData();
if (!preReleaseData) return [];
Expand Down
1 change: 1 addition & 0 deletions workspace.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@
"@types/react-syntax-highlighter": "15.5.10",
"@types/react-tabs": "2.3.2",
"@types/socket.io-client": "1.4.35",
"@types/ssri": "^7.1.5",
"@types/testing-library__jest-dom": "5.9.5",
"@types/ua-parser-js": "0.7.35",
"@types/url-join": "4.0.0",
Expand Down