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

test: Avoid race conditions with symlinks #13498

Merged
merged 2 commits into from
Aug 28, 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
32 changes: 15 additions & 17 deletions dev-packages/browser-integration-tests/utils/generatePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Package } from '@sentry/types';
import HtmlWebpackPlugin, { createHtmlTagObject } from 'html-webpack-plugin';
import type { Compiler } from 'webpack';

import { addStaticAsset, addStaticAssetSymlink } from './staticAssets';
import { addStaticAsset, symlinkAsset } from './staticAssets';

const LOADER_TEMPLATE = fs.readFileSync(path.join(__dirname, '../fixtures/loader.js'), 'utf-8');
const PACKAGES_DIR = path.join(__dirname, '..', '..', '..', 'packages');
Expand Down Expand Up @@ -214,7 +214,10 @@ class SentryScenarioGenerationPlugin {
src: 'cdn.bundle.js',
});

addStaticAssetSymlink(this.localOutPath, path.resolve(PACKAGES_DIR, bundleName, bundlePath), 'cdn.bundle.js');
symlinkAsset(
path.resolve(PACKAGES_DIR, bundleName, bundlePath),
path.join(this.localOutPath, 'cdn.bundle.js'),
);

if (useLoader) {
const loaderConfig = LOADER_CONFIGS[bundleKey];
Expand Down Expand Up @@ -245,14 +248,13 @@ class SentryScenarioGenerationPlugin {
const fileName = `${integration}.bundle.js`;

// We add the files, but not a script tag - they are lazy-loaded
addStaticAssetSymlink(
this.localOutPath,
symlinkAsset(
path.resolve(
PACKAGES_DIR,
'feedback',
BUNDLE_PATHS['feedback']?.[integrationBundleKey]?.replace('[INTEGRATION_NAME]', integration) || '',
),
fileName,
path.join(this.localOutPath, fileName),
);
});
}
Expand All @@ -262,26 +264,23 @@ class SentryScenarioGenerationPlugin {
if (baseIntegrationFileName) {
this.requiredIntegrations.forEach(integration => {
const fileName = `${integration}.bundle.js`;
addStaticAssetSymlink(
this.localOutPath,
symlinkAsset(
path.resolve(
PACKAGES_DIR,
'browser',
baseIntegrationFileName.replace('[INTEGRATION_NAME]', integration),
),
fileName,
path.join(this.localOutPath, fileName),
);

if (integration === 'feedback') {
addStaticAssetSymlink(
this.localOutPath,
symlinkAsset(
path.resolve(PACKAGES_DIR, 'feedback', 'build/bundles/feedback-modal.js'),
'feedback-modal.bundle.js',
path.join(this.localOutPath, 'feedback-modal.bundle.js'),
);
addStaticAssetSymlink(
this.localOutPath,
symlinkAsset(
path.resolve(PACKAGES_DIR, 'feedback', 'build/bundles/feedback-screenshot.js'),
'feedback-screenshot.bundle.js',
path.join(this.localOutPath, 'feedback-screenshot.bundle.js'),
);
}

Expand All @@ -295,10 +294,9 @@ class SentryScenarioGenerationPlugin {

const baseWasmFileName = BUNDLE_PATHS['wasm']?.[integrationBundleKey];
if (this.requiresWASMIntegration && baseWasmFileName) {
addStaticAssetSymlink(
this.localOutPath,
symlinkAsset(
path.resolve(PACKAGES_DIR, 'wasm', baseWasmFileName),
'wasm.bundle.js',
path.join(this.localOutPath, 'wasm.bundle.js'),
);

const wasmObject = createHtmlTagObject('script', {
Expand Down
20 changes: 4 additions & 16 deletions dev-packages/browser-integration-tests/utils/staticAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,11 @@ export function addStaticAsset(localOutPath: string, fileName: string, cb: () =>
symlinkAsset(newPath, path.join(localOutPath, fileName));
}

export function addStaticAssetSymlink(localOutPath: string, originalPath: string, fileName: string): void {
const newPath = path.join(STATIC_DIR, fileName);

// Only copy files once
if (!fs.existsSync(newPath)) {
fs.symlinkSync(originalPath, newPath);
}

symlinkAsset(newPath, path.join(localOutPath, fileName));
}

function symlinkAsset(originalPath: string, targetPath: string): void {
export function symlinkAsset(originalPath: string, targetPath: string): void {
try {
fs.unlinkSync(targetPath);
fs.linkSync(originalPath, targetPath);
} catch {
// ignore errors here
// ignore errors here, probably means the file already exists
// Since we always build into a new directory for each test, we can safely ignore this
Comment on lines +29 to +30
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if we are better off violently crashing, so that we catch this case when it happens...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the problem is that this does happen :D tests fail without this. the alternative is to check if the file already exists before doing this, but a) this is in reality more overhead than try-catching this (because it only happens in a handful of tests where we have multiple HTML files), and b) it is still prone to flake because of possible race conditions. IMHO it is safe to do this, because each dir that it is building for is for a single test, where we never have the case/desire to have the same file being different from each other - they can safely be the same one always.

}

fs.linkSync(originalPath, targetPath);
}
Loading