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(nuxt): Improve logs about adding Node option 'import' #13726

Merged
merged 4 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
34 changes: 20 additions & 14 deletions packages/nuxt/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { consoleSandbox } from '@sentry/utils';
import type { SentryNuxtModuleOptions } from './common/types';
import { addSentryTopImport, addServerConfigToBuild } from './vite/addServerConfig';
import { setupSourceMaps } from './vite/sourceMaps';
import { findDefaultSdkInitFile } from './vite/utils';
import { findDefaultSdkInitFile, getStringSuffixDiff } from './vite/utils';

export type ModuleOptions = SentryNuxtModuleOptions;

Expand Down Expand Up @@ -63,22 +63,28 @@ export default defineNuxtModule<ModuleOptions>({
if (clientConfigFile || serverConfigFile) {
setupSourceMaps(moduleOptions, nuxt);
}
nuxt.hooks.hook('nitro:init', nitro => {
if (serverConfigFile && serverConfigFile.includes('.server.config')) {
addServerConfigToBuild(moduleOptions, nuxt, serverConfigFile);

if (serverConfigFile && serverConfigFile.includes('.server.config')) {
addServerConfigToBuild(moduleOptions, nuxt, serverConfigFile);
if (moduleOptions.experimental_basicServerTracing) {
addSentryTopImport(moduleOptions, nuxt);
} else {
if (moduleOptions.debug) {
const serverDirResolver = createResolver(nitro.options.output.serverDir);
const serverConfigPath = serverDirResolver.resolve('sentry.server.config.mjs');

if (moduleOptions.experimental_basicServerTracing) {
addSentryTopImport(moduleOptions, nuxt);
} else {
if (moduleOptions.debug) {
consoleSandbox(() => {
// eslint-disable-next-line no-console
console.log(
`[Sentry] Using your \`${serverConfigFile}\` file for the server-side Sentry configuration. In case you have a \`public/instrument.server\` file, the \`public/instrument.server\` file will be ignored. Make sure the file path in your node \`--import\` option matches the Sentry server config file in your \`.output\` folder and has a \`.mjs\` extension.`,
);
});
const serverConfigRelativePath = `.${getStringSuffixDiff(serverConfigPath, nitro.options.rootDir)}`;
Copy link
Member

Choose a reason for hiding this comment

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

no strong opinions but shouldn't path.relative basically do what you're trying to do here? (I might be missing something though)

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually yes 😅


consoleSandbox(() => {
// eslint-disable-next-line no-console
console.log(
`[Sentry] Using your \`${serverConfigFile}\` file for the server-side Sentry configuration. Make sure to add the Node option \`import\` to the Node command where you deploy and/or run your application. This preloads the Sentry configuration at server startup. You can do this via a command-line flag (\`node --import ${serverConfigRelativePath} [...]\`) or via an environment variable (\`NODE_OPTIONS='--import ${serverConfigRelativePath}' node [...]\`).`,
);
});
}
}
}
}
});
},
});
10 changes: 10 additions & 0 deletions packages/nuxt/src/vite/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,13 @@ export function findDefaultSdkInitFile(type: 'server' | 'client'): string | unde

return filePath ? path.basename(filePath) : undefined;
}

/**
* Get the diff suffix part between two strings.
*
* Example: getStringDiff('abcdef', 'abc') => 'def'
*/
export function getStringSuffixDiff(longerStr: string, shorterStr: string): string {
const commonPrefixLength = [...longerStr].findIndex((char, index) => char !== shorterStr[index]);
return commonPrefixLength === -1 ? '' : longerStr.slice(commonPrefixLength);
}
28 changes: 27 additions & 1 deletion packages/nuxt/test/vite/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from 'fs';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { findDefaultSdkInitFile } from '../../src/vite/utils';
import { findDefaultSdkInitFile, getStringSuffixDiff } from '../../src/vite/utils';

vi.mock('fs');

Expand Down Expand Up @@ -59,3 +59,29 @@ describe('findDefaultSdkInitFile', () => {
expect(result).toBe('sentry.server.config.js');
});
});

describe('getStringDiff', () => {
Copy link
Member

Choose a reason for hiding this comment

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

l:

Suggested change
describe('getStringDiff', () => {
describe('getStringSuffixDiff', () => {

it('should return the suffix of the longer string when there is a common prefix', () => {
expect(getStringSuffixDiff('abcdef', 'abc')).toBe('def');
});

it('should return an empty string when both strings are identical', () => {
expect(getStringSuffixDiff('abc', 'abc')).toBe('');
});

it('should return the entire longer string when the shorter string is empty', () => {
expect(getStringSuffixDiff('abcdef', '')).toBe('abcdef');
});

it('should return the entire longer string when there is no overlap', () => {
expect(getStringSuffixDiff('abcdef', 'ghijkl')).toBe('abcdef');
});

it('should return an empty string when the longer string is empty', () => {
expect(getStringSuffixDiff('', 'abc')).toBe('');
});

it('should return the suffix of the longer string when the shorter string is a prefix', () => {
Copy link
Member

Choose a reason for hiding this comment

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

l (no action required): what happens if the longer string is shorter (but not empty) that the shorter string param? 😅

expect(getStringSuffixDiff('abcdef', 'abcd')).toBe('ef');
});
});
Loading