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

feat: Add Angular Wizard #741

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open

feat: Add Angular Wizard #741

wants to merge 10 commits into from

Conversation

onurtemizkan
Copy link
Collaborator

@onurtemizkan onurtemizkan commented Dec 12, 2024

Resolves: #672

Adds wizard for Angular projects

Notes:

  • Does not support projects using NGModules, as they are not enabled by default anymore
  • Uses the internal sourcemaps wizard for Sentry CLI configuration
  • Does not create example pages, we can add that in a follow-up PR.

@onurtemizkan onurtemizkan requested a review from Lms24 December 12, 2024 19:26
Copy link

github-actions bot commented Dec 12, 2024

Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against cbb467f

@onurtemizkan onurtemizkan force-pushed the onur/angular-wizard branch 6 times, most recently from 8fc1561 to 542ea8a Compare December 13, 2024 12:12
@Lms24 Lms24 requested a review from andreiborza December 16, 2024 14:26
CHANGELOG.md Outdated
@@ -2,6 +2,7 @@

## Unreleased

- feat: Add Angular Wizard ([#741](https://github.com/getsentry/sentry-wizard/pull/741))
Copy link
Member

Choose a reason for hiding this comment

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

l: could you move this down please? these are ordered by ticket number.

options: WizardOptions,
): Promise<void> {
printWelcome({
wizardName: 'Sentry Remix Wizard',
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
wizardName: 'Sentry Remix Wizard',
wizardName: 'Sentry Angular Wizard',

await installPackage({
packageName: '@sentry/angular@^8',
packageNameDisplayLabel: '@sentry/angular',
alreadyInstalled: hasPackageInstalled('@sentry/angular', packageJson),
Copy link
Member

Choose a reason for hiding this comment

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

m: let's extract the alreadyInstalled flag and log it:

Sentry.setTag('sdk-already-installed', sdkAlreadyInstalled);

Comment on lines 30 to 31
item.imported === '*' &&
item.local === 'Sentry',
Copy link
Member

Choose a reason for hiding this comment

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

l: Should be enough to just check the from here.

): void {
const imports = originalAppConfigMod.imports;
const hasErrorHandler = imports.$items.some(
(item) => item.local === 'ErrorHandler',
Copy link
Member

Choose a reason for hiding this comment

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

m: Can we narrow down here and add && item.from: '@angular/core'? I think this might be too wide.

const imports = originalAppConfigMod.imports;

const hasProvideAppInitializer = imports.$items.some(
(item) => item.local === 'provideAppInitializer',
Copy link
Member

Choose a reason for hiding this comment

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

l: I think we could also narrow down here a bit, although the function is already very specific.

});
}

const hasInject = imports.$items.some((item) => item.local === 'inject');
Copy link
Member

Choose a reason for hiding this comment

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

m: and here, can we narrow down?

const imports = originalAppConfigMod.imports;

const hasAppInitializer = imports.$items.some(
(item) => item.local === 'APP_INITIALIZER',
Copy link
Member

Choose a reason for hiding this comment

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

l: could also narrow here, although might be specific enough already.

Comment on lines 46 to 58
const originalAppModule = await loadFile(appModulePath);

if (hasSentryContent(appModulePath, originalAppModule.$code)) {
return;
}

const updatedAppModuleMod = updateAppModuleMod(
originalAppModule,
dsn,
selectedFeatures,
);

await writeFile(updatedAppModuleMod.$ast, appModulePath);
Copy link
Member

Choose a reason for hiding this comment

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

m: I think we need a try/catch around this and gracefully abort the wizard. A single typo in user's config would crash this.

Comment on lines 95 to 97
clack.log.error(
`Failed to update your app config ${chalk.cyan(appConfigFilename)}`,
);
Copy link
Member

Choose a reason for hiding this comment

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

l/m: In the nuxt wizard, I specifically checked for MagicastError here and logged out what users need to do to their config to make it work. Maybe something to consider here too if possible?

Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

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

Thanks Onur! I left some comments that we should address. One thing we still need to handle is to at least abort gracefully if we detect an NGModule-based project. Right now, I don't think the wizard would handle this very well. What we can do is to heuristically search for an app.module.ts file. The file name is just a convention though (users could name this file however they like) but probably a good enough one.

My gut feeling tells me that a big part (if not the majority) of Angular users will still use NGModules so we should think about finding a way to support these as well. We can take care of this in a follow-up PR though. For now, let's focus on printing good fallback instructions.

Also, let's add a if we encounter a module- or app-config-based setup so that we can gather some data if it's worth investing time into handling NGModules better.

The general path through the wizard should be to always show fallback instructions (link to docs, in-wizard code snippets) of how users can manually do what the wizard fails to do.

So from my PoV let's do the following in this PR:

  • address all review comments

For us to follow up in future PRs (who does it TBD):

  • example page
  • handle NGModules if data agrees with my gut feeling

Comment on lines 57 to 77
if (!installedAngularVersion) {
clack.log.warn('Could not determine installed Angular version.');

return;
}
Copy link
Member

Choose a reason for hiding this comment

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

m: Given that this might be our fault rather than a user-misconfiguration or similar, can we fall back to asking users for the version?

Comment on lines 65 to 69
if (!installedMinVersion) {
clack.log.warn('Could not determine minimum Angular version.');

return;
}
Copy link
Member

Choose a reason for hiding this comment

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

Given the above, we shouldn't fall into this case, so we might as well remove it

Comment on lines +77 to +89
clack.log.warn(
`Angular version ${MIN_SUPPORTED_ANGULAR_VERSION} or higher is required.`,
);
Copy link
Member

Choose a reason for hiding this comment

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

l: Let's print the link to our version compatibility table in the docs (https://docs.sentry.io/platforms/javascript/guides/angular/#angular-version-compatibility) and tell them to find the suitable SDK version instead.

Comment on lines 112 to 114
await traceStep('Inject Sentry to Angular app config', async () => {
await initalizeSentryOnAppModule(dsn, selectedFeatures);
});
Copy link
Member

Choose a reason for hiding this comment

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

I think what we're doing here is not related to the "app config" or "app module" at all, but we rather init Sentry in main.ts right? I'd rather call the the step and function something like app entry point or so. The "app module" makes this sound like it modifies app.module.ts.

});

await traceStep('Setup for sourcemap uploads', async () => {
addSourcemapEntryToAngularJSON();
Copy link
Member

Choose a reason for hiding this comment

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

m: Let's log a message that we modified angular.json. I completely missed this when running the wizard (also related to my comment below).

options.url = sentryUrl;
}

await runSourcemapsWizard(options, 'angular');
Copy link
Member

Choose a reason for hiding this comment

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

l: Generally, running the source maps wizard here seems fine to me. I found the integration quite seamless. However, in the source maps wizard, there's a step where I need to verify that source maps are created. I think, given that within the wizard run, already enabled source maps emission, we might as well just skip this step. This probably requires some kind of new option in the source maps wizard. Feel free to add that as you see fit.


await runSourcemapsWizard(options, 'angular');
});

Copy link
Member

Choose a reason for hiding this comment

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

m: Before we print the outro, let's call runPrettierIfInstalled() to optionally reformat the project files.

Comment on lines 13 to 17
if (!angularJson) {
throw new Error('Could not find in angular.json in your project');
}
Copy link
Member

Choose a reason for hiding this comment

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

l: If we end up here, let's provide users a fallback, which in this case is to manually enable source maps upload. 2 ideas:

  1. we link to docs
  2. we show the snippet that we show in the source maps wizard if there's no pre-selected project

@getsentry getsentry deleted a comment from andreiborza Dec 17, 2024
@getsentry getsentry deleted a comment from andreiborza Dec 17, 2024
Comment on lines 153 to 169
const errorHandlerObject = b.objectExpression([
b.objectProperty(
b.identifier('provide'),
b.identifier('ErrorHandler'),
),
b.objectProperty(
b.identifier('useValue'),
b.identifier('Sentry.createErrorHandler()'),
),
]);

providers.elements.push(
// @ts-expect-error - errorHandlerObject is an objectExpression
errorHandlerObject,
);
Copy link
Member

Choose a reason for hiding this comment

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

m: Angular only permits one ErrorHandler per application. We need to check if users already declared one and if so, let's not add an additional one but link to our docs how to combine their handler with ours: https://docs.sentry.io/platforms/javascript/guides/angular/features/error-handler/

@Lms24
Copy link
Member

Lms24 commented Dec 17, 2024

Just FYI:
image

@andreiborza and I commented on the same line and discussed offline with which suggestion to go with. I just removed the comments to avoid confusion :)

@onurtemizkan
Copy link
Collaborator Author

Thanks for the reviews, @andreiborza, @Lms24!
All the points should be addressed.

this.taskHandle.stdout.pipe(process.stdout);
this.taskHandle.stderr.pipe(process.stderr);
}
// if (opts?.debug) {
Copy link
Member

Choose a reason for hiding this comment

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

l: Is this intentional? We should just remove the if if we don't use it anymore.


if (!isSupportedAngularVersion) {
clack.log.warn(
`Angular version ${MIN_SUPPORTED_ANGULAR_VERSION} or higher is required.`,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
`Angular version ${MIN_SUPPORTED_ANGULAR_VERSION} or higher is required.`,
`Angular version ${chalk.cyan(MIN_SUPPORTED_ANGULAR_VERSION)} or higher is required.`,

clack.log
.warn(`ErrorHandler provider already exists in your app config.
Please refer to the Sentry Angular SDK documentation to combine it manually with Sentry's ErrorHandler.
https://docs.sentry.io/platforms/javascript/guides/angular/features/error-handler/
Copy link
Member

Choose a reason for hiding this comment

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

l: Let's underline the docs link like the other links.

try {
fs.writeFileSync(angularJsonPath, JSON.stringify(angularJson, null, 2));
} catch (error) {
clack.log.error(`Failed to write sourcemap configuration to angular.json`);
Copy link
Member

Choose a reason for hiding this comment

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

m: What should users do in this case? Can we show them a pasteable snippet of what they could do instead?

Comment on lines +53 to +57
const updatedAppEntryMod = updateAppEntryMod(
originalAppEntry,
dsn,
selectedFeatures,
);
Copy link
Member

Choose a reason for hiding this comment

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

m: Could this not already fail? How about moving that down into the try?

try {
await writeFile(updatedAppEntryMod.$ast, appEntryPath);
} catch (error: unknown) {
if (error instanceof MagicastError) {
Copy link
Member

Choose a reason for hiding this comment

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

m: In the nuxt wizard I went back on specifically checking for MagicastErrors because really any error warrants instructing users how to possibly get around it. See https://github.com/getsentry/sentry-wizard/blob/master/src/nuxt/sdk-setup.ts#L122-L149.

),
);

await abort('Exiting Wizard');
Copy link
Member

Choose a reason for hiding this comment

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

m: I think we don't want to abort here if we instruct users on what to do. cc @Lms24

Copy link
Member

Choose a reason for hiding this comment

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

i think we usually just continue, yes

Comment on lines +119 to +125
if (error instanceof MagicastError) {
clack.log.warn(
`Failed to update your app config ${chalk.cyan(
appConfigFilename,
)} automatically.
Please refer to the documentation for manual setup
https://docs.sentry.io/platforms/javascript/guides/angular/#configure`,
Copy link
Member

Choose a reason for hiding this comment

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

m: Same comment as above, I think we should refrain from checking the error type here and always log out alternatives.

),
);

await abort('Exiting Wizard');
Copy link
Member

Choose a reason for hiding this comment

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

m: Not sure if we want to abort here either.

@andreiborza
Copy link
Member

Thanks @onurtemizkan, I added my new review. Mostly LGTM, just some clarifications.

),
);

await abort('Exiting Wizard');
Copy link
Member

Choose a reason for hiding this comment

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

i think we usually just continue, yes

Comment on lines +232 to +236
// Angular wizard handles the angular.json setup itself
!preSelectedTool || preSelectedTool !== 'angular'
? configureAngularSourcemapGenerationFlow
: // Not leaving this as undefined to avoid default. This is expected to be a no-op.
async () => Promise.resolve(),
Copy link
Member

Choose a reason for hiding this comment

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

I see there's a new paramter (skipValidation). Should we use it here instead of creating the noop function?

Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

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

@onurtemizkan sorry for the late notice but would you mind splitting up this PR a bit? It's getting quite large, especially with the many AST modifications we need in comparison to other wizards. I added some suggestions for steps into #672 but feel free to go with reasonable steps as you see fit.

@andreiborza did this a while ago with the Nuxt wizard where he collected the progress in one branch and made smaller PRs against the branch.

Btw, I think the groundwork is done well in this PR, so no need to rewrite things. Individual PRs just make it easier to review. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Wizard for Angular
3 participants